Java 8

Lambda Expressions

 

Strem API 

( ) -> { }
()>( ) -> { }

Before ...

Anonymous Classes


public static void main(String[] args) {

    List<Integer> listOfInt = Arrays.asList(1,2,3,100,300,250,50,60,30,12,5,7,9);


    listOfInt.sort(new Comparator<Integer>() {
      @Override
      public int compare(Integer o1, Integer o2) {
        return o1.compareTo(o2);
      }
    });

     for(Integer obj: listOfInt){
      System.out.println(obj);
    }

}

Today ...

Lambda Expression 

public static void main(String[] args) {

    List<Integer> listOfInt = Arrays.asList(1,2,3,100,300,250,50,60,30,12,5,7,9);

    listOfInt.sort((Integer a, Integer b)-> { return a.compareTo(b); });

    listOfInt.stream().forEach(a -> System.out.println(a));
}

Confused !!!

What the heck is this

( ) -> { ... }

expression all about?

Java 8

By Nikunj Parmar