简体   繁体   English

循环推理java lambda groupingby

[英]cyclic inference java lambda groupingby

I'm new to lambdas and confused with what I'm doing wrong in this line of code: 我是lambdas的新手,并且在这行代码中与我做错了相混淆:

HashMap<Date, ArrayList<Trade>> groupTrades = allTrades.stream().collect(Collectors.groupingBy(Trade::getTradeDate()));

IntelliJ won't compile because of a cyclic inference. 由于循环推断,IntelliJ将无法编译。

After a bit of pain I've worked it out and hopefully this will be useful to others as well 经过一些痛苦之后,我已经解决了这个问题,希望这对其他人也有用

You mustn't use HashMap or ArrayList - just use the interfaces Map and List, code should read: 你不能使用HashMap或ArrayList - 只需使用接口Map和List,代码应该是:

Map<Date, List<Trade>> groupTrades = allTrades.stream().collect(Collectors.groupingBy(Trade::getTradeDate));

Note that this rather generic message could be caused when any of the parameters in the groupingBy don't match what is expected in the declaration of the Map. 请注意,当groupingBy中的任何参数与Map声明中的预期值不匹配时,可能会导致此相当通用的消息。

try to remove the () on getTradeDate 尝试删除getTradeDate上的()

HashMap<Date, ArrayList<Trade>> groupTrades = allTrades.stream().collect(Collectors.groupingBy(Trade::getTradeDate));

Here is a nice little overview: http://www.java8.org/ 这是一个很好的小概述: http//www.java8.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM