سؤال

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 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

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));

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.

نصائح أخرى

try to remove the () on getTradeDate

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

Here is a nice little overview: http://www.java8.org/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top