I quite don't understand how it is valid to write an expression such as:
Comparator<String> stringComp = Comparator.nullsLast(String::compareTo);
Because:
nullsLast
accepts aComparator
as argument- the functional method of
Comparator
iscompare(T, T)
, which accepts two arguments compareTo
accepts only one argument- Without the double-colon operator, I would write:
Comparator<String> stringComp = Comparator.nullsLast((s1, s2) -> s1.compareTo(s2));
So does it mean that, when using the double-colon operator on a method accepting only one argument, it is inferred that the method should be called on the first argument of the lambda, using the second argument of the lambda as parameter? It is very surprising to me.