Saturday, 19 August 2017

How Java Lambdas Expressions affect the code in simple way

Let us start with some basic examples. Here, we will see how lambda expressions affect the code in a simple way. Having a list of players, the “for loop”, as programmers often refers to the for statement, can be translated in Java SE 8 as below:

String[] s1 = {"Anant", "Nilan", "Dolagobinda", "Babu", "Rupeli", "Sanjay", "Akshay", "Santosh"};
List<String> players =  Arrays.asList(s1);
      
// Old looping
for (String player : players) {
     System.out.print(player + "; ");
}
      

// Using lambda expression and functional operations
players.forEach((player) -> System.out.print(player + "; "));

// Using double colon operator in Java 8
players.forEach(System.out::println);

No comments:

Post a Comment