我正在研究泛型
,发现以下代码在比较方法
处出现编译时错误。
Multiple markers at this line - Cannot infer type argument(s) for comparing(Function) - The type A does not define m1(Object) that is applicable here
class A<T> {
String m1() {
return null;
}
}
class B {
void test() {
Comparator<A<String>> target = Comparator.comparing(A::m1).thenComparing(A::m1);
}
}
有人可以帮助我理解这种行为吗?我该如何解决这个问题?
最佳答案
如果您在 comparing
方法中指定确切的泛型类型,代码将编译。
Comparator<A<String>> target =
Comparator.<A<String>, String>comparing(A::m1).thenComparing(A::m1);
关于java - 泛型和比较器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44625657/