Lambda Expression and generic defined only in method
You can't use a lambda expression for a functional interface, if the method in the functional interface has type parameters. See section §15.27.3 in JLS8:
A lambda expression is compatible [..] with a target type T if T is a functional interface type (§9.8) and the expression is congruent with the function type of [..] T. [..] A lambda expression is congruent with a function type if all of the following are true:
- The function type has no type parameters.
- [..]
Using method reference, i found other way to pass the argument:
List<String> list = Arrays.asList("a", "b", "c");
sort(list, Comparable::<String>compareTo);
Just point compiler the proper version of generic Comparator with
(Comparator<String>)
So the answer will be
sort(list, (Comparator<String>)(a, b) -> a.compareTo(b));