您的位置:首页技术文章
文章详情页

java-ee - JAVA Non-static method cannot be referenced

【字号: 日期:2023-10-19 15:56:49浏览:50作者:猪猪

问题描述

Non-static method cannot be referenced from a static contextjava-ee - JAVA Non-static method cannot be referenced

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 的提示来优化。

标签: java
相关文章: