Is there an aggregateBy method in the stream Java 8 api?

aggregation, java, java-8

Solution

The aggregate operation can be done using the `Collectors` class. So in the video, the example would be equivalent to :

Map<String, Integer> map = 
    documents.stream().collect(Collectors.groupingBy(Document::getAuthor, Collectors.summingInt(Document::getPageCount)));

The `groupingBy` method will give you a `Map<String, List<Document>>`. Now you have to use a downstream collector to sum all the page count for each document in the `List` associated with each key.

This is done by providing a downstream collector to `groupingBy`, which is `summingInt`, resulting in a `Map<String, Integer>`.

They give basically the same example in the documentation where they compute the sum of the employees' salary by department.

I think that they removed this operation and created the `Collectors` class instead to have a useful class that contains a lot of reductions that you will use commonly.

Problem

Run across this very interesting but one year old presentation by Brian Goetz - in the slide linked he presents an `aggregateBy()` method supposedly in the Stream API, which is supposed to aggregate the elements of a list (?) to a map (given a default initial value and a method manipulating the value (for duplicate keys also) - see next slide in the presentation). Apparently there is no such method in the Stream API. Is there another method that does something analogous in Java 8 ?

Original source