Thursday, September 11, 2014

Lambda : List with Lambda expressions

import java.util.ArrayList;
import java.util.List;

public class LambdaComparator {
       public static void main(String[] args) {
              List strings = new ArrayList<>();      
              strings.add("actor");
              strings.add("bat");
              strings.add("do");
              strings.add("i");
              strings.sort((a, b)-> Integer.compare(a.length(), b.length()));
              strings.forEach(System.out::println);
       }
}

Output:

i
do
bat
actor

No comments:

Post a Comment