Java – generics and comparators
•
Java
I was studying generics and found that the following code gave compile - time errors when comparing methods
class A<T> { String m1() { return null; } } class B { void test() { Comparator<A<String>> target = Comparator.comparing(A::m1).thenComparing(A::m1); } }
Someone can help me understand this behavior; How can I solve this problem?
Solution
If the exact generic type is specified in the comparison method, the code is compiled
Comparator<A<String>> target = Comparator.<A<String>,String>comparing(A::m1).thenComparing(A::m1);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码