循环推理java lambda groupingby [英] cyclic inference java lambda groupingby

查看:163
本文介绍了循环推理java lambda groupingby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是lambdas的新手并且在这行代码中与我做错了相混淆:

I'm new to lambdas and confused with what I'm doing wrong in this line of code:

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

由于循环推理,IntelliJ无法编译。

IntelliJ won't compile because of a cyclic inference.

推荐答案

经过一段时间的努力我已经解决了,希望这对其他人也有用

After a bit of pain I've worked it out and hopefully this will be useful to others as well

你不能使用HashMap或ArrayList - 只需使用接口Map和List,代码应为:

You mustn't use HashMap or ArrayList - just use the interfaces Map and List, code should read:

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

请注意,当groupingBy中的任何参数不匹配时,可能会导致此相当通用的消息宣言中的预期结果。

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.

这篇关于循环推理java lambda groupingby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆