Methods Reference and Constructor Reference

Example 1:

This is a lambda expression :

Consumer<String> c = s -> System.out.println(s);

Can be written as below using Method reference :

Consumer<String> c = System.out::println;

Example 2 :

Comparator<Integer> c1 = (i1,i2) -> Integer.compare(i1, i2);

Can be written as below in Method Reference :

Comparator<Integer> c2 = Integer::compare;

Leave a Comment