Java 8 added support for Lambda Expression . So what exactly are Lambda Expressions ?
Lambda Expressions are nothing but Anonymous Functions . Anonymous functions are methods which does not have any names , they make the code more concise and readable ,Suppose you want to sort a list of person based on their age , you can write following code using anonymous class syntax :
Collections.sort(list, new ComparatorNow with the help of lambda expressions , this code can be reduced to one single line of code :() { @Override public int compare(Person p1, Person p2) { return (p1.getAge() > p2.getAge()) ? 1 : 0 ; }});
Collections.sort(list,(p1,p2 ) -> (p1.getAge() > p2.getAge()) ? 1 : 0 );its syntax is : (${Parameters}) -> {${method body}} 1 ) If the method takes only one parameter , then we do not need to use parenthesis. 2 ) We do not need to specify the type of parameter , it will be inferred automatically by java . so we do not need to write (Person p1 , Person p2 ) instead of (p1,p2). 3 ) The Interface must be Functional Interface . A functional interface is a kind of interface which contains only one single abstract method .In this example , Collections.sort method takes a Comparator as parameter , Comparator Interface has only one single method , so you can directly implement it without specifying its name using lambda . 4 ) You can write method body with multiple statements also like this :
Collections.sort(list,(p1,p2 ) ->{ if ( p1.getAge() > p2.getAge()) return 1 ; else return 0 ; });5 ) If you want to use variable declared in the enclosing method , you have to declare that as final . 6 ) You can create a Runnable using Lambda like this :
Runnable r = () -> System.out.println("Hello World !! ");Hers's the complete java program example for Lamda Expressions in java .
package java8Test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class LambdaTest { public static void main(String[] args) { List<Person> list = generateList(); //try to sort with age using anonymous classes way Collections.sort(list, new Comparator<Person>() { @Override public int compare(Person p1, Person p2) { return (p1.getAge() > p2.getAge()) ? 1 : 0 ; } }); // method variable must be final to be used in lambda expression final int x = 0 ; //you can create a Runnable like this also using lambda expression //Runnable r = () -> System.out.println("thread running "); //sorting with Age using lambda expression Collections.sort(list,(p1,p2 ) -> (p1.getAge() > p2.getAge()) ? 1 : 0 ); Collections.sort(list,(p1,p2 ) ->{ System.out.println(x); if ( p1.getAge() > p2.getAge()) return 1 ; else return 0 ; }); for(Person p : list) System.out.println(p.getName() + " " + p.getAge() + " " + p.getSalary()); Collections.sort(list,(p1,p2 ) -> (p1.getSalary() > p2.getSalary()) ? 1 : 0 ); for(Person p : list) System.out.println(p.getName() + " " + p.getAge() + " " + p.getSalary()); Collections.sort(list,(p1,p2 ) -> p1.getName().compareTo(p2.getName())); for(Person p : list) System.out.println(p.getName() + " " + p.getAge() + " " + p.getSalary()); } private static List<Person> generateList() { List<Person> mylist = new ArrayList<>(); Person p1 = new Person(); p1.setAge(25); p1.setName("ABHishek"); p1.setSalary(5250); Person p2 = new Person(); p2.setAge(26); p2.setName("pqww"); p2.setSalary(5252); Person p3 = new Person(); p3.setAge(29); p3.setName("ewewe"); p3.setSalary(6525); mylist.add(p1); mylist.add(p2); mylist.add(p3); return mylist; } }Post Comments And Suggestions !!!