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. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””2. docker-compose中volumes的問題3. boot2docker無法啟動4. nignx - docker內nginx 80端口被占用5. docker安裝后出現Cannot connect to the Docker daemon.6. dockerfile - 為什么docker容器啟動不了?7. java - SSH框架中寫分頁時service層中不能注入分頁類8. node.js - antdesign怎么集合react-redux對input控件進行初始化賦值9. 關于docker下的nginx壓力測試10. docker容器呢SSH為什么連不通呢?

網公網安備