Created
September 20, 2018 15:49
-
-
Save mitchross/cebe60e18c9929dfec1f0b99a4e303d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void test() { | |
ArrayList<String> myArrayList = new ArrayList<>(); | |
//Non lambda code | |
for (String s : myArrayList) { | |
switch (s) { | |
case "x": | |
System.out.println("do something"); | |
break; | |
case "y": | |
System.out.println("do something else"); | |
break; | |
case "z": | |
System.out.println("do something else"); | |
break; | |
} | |
} | |
//The statement above is equivalent to the lambda code below | |
myArrayList.forEach(item -> { | |
//applies to everything in loop | |
item.concat("add something"); | |
if (item.equals("x")) { | |
System.out.println("do something"); | |
} | |
if (item.equals("y")) { | |
System.out.println("do something else "); | |
} | |
if (item.equals("z")) { | |
System.out.println("do something else again"); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment