I would give a few examples here for those who find the Oracle documentation a bit more complicated. Imagine you need a reference to an instance of Comparator:
.sorted(String::compareTo)
String :: compareTo is identical:
(String a, String b) -> a.compareTo(b);
Since, as John explained, the method reference will be converted to a lambda that will expect 2 parameters. The actual arbitrary object passed in the stream as the first argument, and one more parameter (since the comparator expects int compare(T o1, T o2) ). Another case:
.map(Employee::getSalary)
In this case, the card expects: Function. The function requires an implementation of R apply(T var1) - a method with 1 argument. In this case, the only parameter that will pass the lambda is the actual arbitrary object - an instance on Employee.
To summarize - depending on the context of the compilation time, a method reference to an arbitrary object will always be โtransformedโ into a lambda, which expects this object to become the first parameter + any number of parameters that the target method requires in the same corresponding order.
source share