Method Reference - HolmesJJ/OOP-FP GitHub Wiki
Simplify existing methods in lambda expressions through method reference. The method reference is a Lambda expression, where the operator of the method reference is the double colon "::".
Print each element in the ArrayList
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
/* Before Java 8 */
for (Integer e : list) {
System.out.println(e);
}
/* From Java 8 */
list.forEach(e -> System.out.println(e));
// The content of the lambda expression just passes the parameter e to the println() method,
// so it can be further simplified
list.forEach(System.out::println);
-
System.out::println
is different fromSystem.out.println()
-
System.out::println
can be regarded as a short form of lambda expressione -> System.out.println(e)