java-ee - JAVA Non-static method cannot be referenced
问题描述
Non-static method cannot be referenced from a static context
operationInfos.stream().collect(Collectors.toMap(OperationThisMonthVO::getSurgeryDate, Function.identity(), surgeryCountMerge));public static final BinaryOperator<OperationCountVO> surgeryCountMerge = (v1, v2) -> { v1.setSurgeryCount(v1.getSurgeryCount() + v2.getSurgeryCount()); return v1;};
我想对operationInfos进行分组,然后算个数,但是爆了这个错。我这个方法不是静态的方法..
问题解答
回答1:toMap要求的参数是Function<? super T,? extends K> keyMapper,那么你把OperationThisMonthVO::getSurgeryDate当做Function,是否符合? super T和? extends K呢?我猜测OperationThisMonthVO是operationInfo的子类而不是父类,所以这样写不行。可以改写成:
toMap(operationInfo -> ((OperationThisMonthVO) operationInfo).getSurgeryDate(), ...)
试试看。
回答2:简单的做法是,先写成标准的 lambda 表达式,再根据 IDE 的提示来优化。
相关文章:
1. java - 怎么处理前台要展示,但是数据库表没有该字段的实体类冗余属性呢?2. boostrap怎么在移动端不显示sidebar?3. android - 使用listview加载不同item,数据填充错误。4. python3.x - python中的虚拟环境怎样设置一直处于激活状态5. javascript - 读js权威指南“作为值得函数”产生了疑惑,求释疑6. javascript - 为什么 process.stdin 会有 write 方法◔ ‸◔?7. python中merge后文件莫名变得非常大8. file - Python如何读文件内容修改后写回原文件9. 请问带渐变背景的进度条如何实现?求给点思路10. python - UTC格式转换成本地时间