How to use Java 8 Stream Collectors.groupingBy() Examples

How to use Java 8 Stream Collectors.groupingBy()


In the tutorial, Grokonez will show how to use Grouping By APIs of Java Stream Collectors by examples:

  • Explore Stream GroupingBy Signatures
  • Combine groupingBy API with others Reduction Operations

Now let’s do more details!

Related posts:
Java Stream.Collectors APIs Examples
Java 8 Stream Reduce Examples

Stream GroupingBy Signatures

1. groupingBy(classifier)

-> It returns a Collector implementing a group by operation on input elements of type T,
grouping elements according to a classifier function, and returning the results in a Map.

– Type Parameters:
+ T – the type of the input elements
+ K – the type of the keys

– Parameters:
+ classifier: the classifier function mapping input elements to keys

– Returns: a Collector implementing the cascaded group-by operation

Example:

2. groupingBy(classifier, downstream)

-> Returns a Collector implementing a cascaded “group by” operation on input elements of type T,
grouping elements according to a classification function, and then performing a reduction operation
on the values associated with a given key using the specified downstream Collector.

– Type Parameters:
+ A: the intermediate accumulation type of the downstream collector
+ D: the result type of the downstream reduction

– Parameters:
+ classifier: a classifier function mapping input elements to keys
+ downstream: a Collector implementing the downstream reduction

Example:

3. groupingBy(classifier, mapFactory, downstream)

-> Returns a Collector implementing a cascaded “group by” operation on input elements of type T,
grouping elements according to a classification function, and then performing a reduction operation
on the values associated with a given key using the specified downstream Collector.

– Parameters:
+ mapFactory: a function which, when called, produces a new empty Map of the desired type

– Example:

4. GroupingBy Concurrent

We have 3 signatures for Java Stream Collectors GroupingBy Concurrent:

groupingByConcurrent(Function classifier)
groupingByConcurrent(Function classifier, Collector downstream)
groupingByConcurrent(Function classifier, Supplier mapFactory, Collector downstream)

We use groupingByConcurrent as the similar to the groupingBy. But groupingByConcurrent can help us to leverages multi-core architectures to improving performance.
It returns a concurrent, unordered Collector implementing the group-by operation.

Note: mapFactory produces a new empty ConcurrentMap of the desired type.

Example:

Java Stream Collectors GroupingBy Examples

Setup Models

– Create Employee.java class:

– Init Employee List:

Nested GroupingBy

-> Output:

Summing Results after GroupingBy

-> Output:

Getting Max/Min Values after GroupingBy

– Getting Max salaries per each department:

-> Output:

– Getting Min salaries per each department:

-> Output:

Summarizing Result after GroupingBy Example

-> Output:

Mapping Results after GroupingBy

-> Output:

Partitioning Results after GroupingBy

-> Output:

Conclusion

We had learned how to use Java Stream Collectors GroupingBy APIs by examples:

  • Explain how to use GroupingBy APIs with many variant signatures
  • Apply GroupingBy APIs with other Stream Reduction Operations: Min/Max, Summarizing, Mapping, Partitioning

Thanks so much! Happy Learning!

By grokonez | April 25, 2019.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*