加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 泛型和比较器

发布时间:2020-12-15 05:18:29 所属栏目:Java 来源:网络整理
导读:我正在研究泛型,发现以下代码在比较方法时给出了编译时错误. 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 AT { String m1() { retur
我正在研究泛型,发现以下代码在比较方法时给出了编译时错误.

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);
    }
}

有人可以帮助我理解这种行为;我该如何解决这个问题呢?

解决方法

如果在比较方法中指定了确切的泛型类型,则代码将进行编译.

Comparator<A<String>> target =
    Comparator.<A<String>,String>comparing(A::m1).thenComparing(A::m1);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读