Collectors: Grouping Operations

In this lesson, we will look at the grouping operations that can be performed through the APIs provided in the Collectors class.

Grouping operations

Grouping operations are one of the most important features of streams because they can help you complete a task, which otherwise would have taken a lot of coding, in just 2-3 lines of code.

Let’s say, for example, we have a list of Employee objects. We need to group all our employees based on their countries of residence. Or, say we need to find the average age/salary of all employees in a particular country. These kinds of operations can be done very easily with grouping APIs provided in the Collectors class.

Let’s explore these APIs in detail.

1) Collectors.groupingBy()

This method groups the input elements according to the ...