Wednesday, July 29, 2015

Java functional programming rant: Online Tutorials taking the easy way out

I'm trying to figure out how to replace this bit of Java code:

for(String str: list){
    if(someCondition(str)){
       list2.add(doSomeThing(str));
    }
    else{
        list2.add(doSomethingElse(str));
    }
}
with an equivalent using Java 8's functional programming techniques (lambda expressions, etc.).

Spent a couple of hours searching for online tutorials, but they all seem to take the easy way out when they show how to migrate from "old Java" to "new Java" - the examples they offer include if statement without else, if statement just checking for null, etc. Fortunately, this entry on StackExchange may have what I'm looking for:


Replacing if-else from for loops with streams

No comments:

Post a Comment