The [groupingBy(classifier, downstream)](<https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#groupingBy-java.util.function.Function->)
collector allows the collection of [Stream](<https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html>)
elements into a [Map](<https://docs.oracle.com/javase/8/docs/api/java/util/Map.html>)
by classifying each element in a group and performing a downstream operation on the elements classified in the same group.
A classic example of this principle is to use a [Map](<https://docs.oracle.com/javase/8/docs/api/java/util/Map.html>)
to count the occurrences of elements in a [Stream](<https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html>)
. In this example, the classifier is simply the identity function, which returns the element as-is. The downstream operation counts the number of equal elements, using [counting()](<https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#counting-->)
.
Stream.of("apple", "orange", "banana", "apple")
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.forEach(System.out::println);
The downstream operation is itself a collector ([Collectors.counting()](<https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#counting-->)
) that operates on elements of type String and produces a result of type Long
. The result of the collect
method call is a Map<String, Long>
.
This would produce the following output:
banana=1 orange=1 apple=2