前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >逆序排序遇到`Not-static method cannot be referenced from a static context`

逆序排序遇到`Not-static method cannot be referenced from a static context`

作者头像
十毛
发布2019-05-14 15:01:34
1.8K0
发布2019-05-14 15:01:34
举报

逆序排序中的方法引用

问题

在Stream API中或其他排序方法中

代码语言:javascript
复制
LinkedHashMap<String, Long> sortedKeywords = keywordCount.entrySet().stream()
        .sorted(Comparator.comparingLong(Map.Entry::getValue).reversed())
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));

IDEA会提示错误:Not-static method cannot be referenced from a static context

问题说明

  • stream中的顺序排列 使用Comparator.comparingLong(Map.Entry::getValue)完成顺序排列
代码语言:javascript
复制
// 正序排序没有任何问题
LinkedHashMap<String, Long> sortedKeywords = keywordCount.entrySet().stream()
        .sorted(Comparator.comparingLong(Map.Entry::getValue))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
  • 逆序排列 Comparator.comparingLong(Map.Entry::getValue).reversed()则会出现错误提示Not-static method cannot be referenced from a static context
代码语言:javascript
复制
// 逆序排序就会IDEA就会提示编译错误:`Not-static method cannot be referenced from a static context`
LinkedHashMap<String, Long> sortedKeywords = keywordCount.entrySet().stream()
        .sorted(Comparator.comparingLong(Map.Entry::getValue).reversed())
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));

上面这个提示是有误导性质的,实际上是因为Java无法推断类型,所以只要明确类型信息就可以(这一点确实很奇怪,因为在我们看来,类型信息明明是已经存在的。而且使用别的类型,IDEA也会报错,也侧面证明,类型信息确实可以推断出来。进一步的问题排查还需要了解更多Java的类型推断

  • 编译还会生成如下错误信息
代码语言:javascript
复制
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project tenbot: Compilation failure: Compilation failure:
[ERROR] /E:/Workspace/tenbot/src/main/java/com/tencent/iask/tenbot/util/HanLpUtil.java:[38,49] 不兼容的类型: 无法推断类型变量 T
[ERROR]     (参数不匹配; 方法引用无效
[ERROR]       无法将 接口 java.util.Map.Entry<K,V>中的 方法 getValue应用到给定类型
[ERROR]         需要: 没有参数
[ERROR]         找到: java.lang.Object
[ERROR]         原因: 实际参数列表和形式参数列表长度不同)
[ERROR] /E:/Workspace/tenbot/src/main/java/com/tencent/iask/tenbot/util/HanLpUtil.java:[38,53] 方法引用无效
[ERROR]   无法从静态上下文中引用非静态 方法 getValue()
  • 修复错误:增加类型信息 Comparator.comparingLong(Map.Entry<String, Long>::getValue).reversed()
代码语言:javascript
复制
LinkedHashMap<String, Long> sortedKeywords = keywordCount.entrySet().stream()
        .sorted(Comparator.comparingLong(Map.Entry<String, Long>::getValue).reversed())
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));

问题解释

这里实际上是Java的类型推断的问题,不过跟javac的一个bug混淆了,就生成这么一个有误导性质的错误提示。如果是在Java 9中则会提示参数不匹配; 方法引用无效

ps: 为什么会存在类型推断错误?思考了半天也没弄明白,希望高人解答

参考

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 逆序排序中的方法引用
    • 问题
      • 问题说明
        • 问题解释
          • 参考
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档