Skip to content

Instantly share code, notes, and snippets.

@mitchross
Created September 20, 2018 15:49
Show Gist options
  • Save mitchross/cebe60e18c9929dfec1f0b99a4e303d7 to your computer and use it in GitHub Desktop.
Save mitchross/cebe60e18c9929dfec1f0b99a4e303d7 to your computer and use it in GitHub Desktop.
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