We would like to know how to compare comparator created by inner class and Lambda.
import java.util.ArrayList; import java.util.List; import java.util.Comparator; /*from w w w . j a v a 2s.co m*/ public class Main { public static void main(String[] args) { // setup List<Integer> list = new ArrayList<Integer>(); list.add(4); list.add(3); // classic way list.sort(new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { return a - b; } }); System.out.println(list); // lambda way, the shortest way list.sort((a, b) -> a - b); System.out.println(list); } }
The code above generates the following result.