Substitute for groupingReduce ?
Brian Goetz
brian.goetz at oracle.com
Wed Mar 20 12:48:14 PDT 2013
The quick summary here is that groupingReduce got folded into
groupingBy, which became possible by loosening the specification of
Collector to allow reduce to be defined as a Collector. That made a lot
of variants in Collectors go away.
On 3/20/2013 3:36 PM, Brian Goetz wrote:
> I think you're missing a reduce function in that call? I see a
> classifying function and a merge function.
>
> But here are some possible answers:
>
> Cities by name:
> Map<String, List<City>> citiesByName
> = cities.stream().collect(groupingBy(City::getName));
>
> Total population by name:
> Map<String, Integer> cityPopsByName
> = cities.stream()
> .collect(groupingBy(City::getName,
> reducing(City::getPop, Integer::sum));
>
> Biggest city by name:
> Comparator<City> bySize = Comparators.comparing(City::getPop);
> Map<String, City> biggestCityByName
> = cities.stream()
> .collect(groupingBy(City::getName,
> reducing(greaterOf(bySize)));
>
>
>
> On 3/20/2013 3:24 PM, Michael Nascimento wrote:
>> Hi guys,
>>
>> I had this before:
>>
>> Map<String, City> cityByName = cities.stream().collect(groupingReduce(
>> city -> city.getName(), throwingMerger()));
>>
>> What is the proper replacement?
>>
>> Regards,
>> Michael
>>
>
More information about the lambda-dev
mailing list