Passing a Reference of a Method That Takes Parameters

 
people.stream()
 
.sorted(Person::ageDifference)

We can replace a lambda expression with a method reference if it directly routes the first parameter as a target of a method call, and the remaining parameters as this method’s arguments. The preceding sample code is equivalent to this:

 
people.stream()
 
.sorted((person1, person2) -> person1.ageDifference(person2))

Get Functional Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.