How to use the groupingBy collector in Java streams

Working with the Java Streaming API can help you in many ways but here I would like to show you how to use the Collectors groupingBy method. This helps you to collect the elements from the stream in groups or buckets.

The setup

We have an Employee class that holds the name, the department, the position and the salary among other properties. The Department class is pretty simple and just has a name in this example. The position of an employee is modeled with an enum that has values for CEO, Software-Developer, Controller and so on. The EmployeeService class tries to simulate a certain datasource – such as a database for example – that holds the data about the company with all the employees, departments and so on. The findAll method returns sixty employees working in three departments, which are HR, Controlling and IT for now.

Using Collectors.groupingBy()

With that setup in mind let’s have a look at the following questions. Suppose we have an ArrayList of employees and want to group them by their department. We just stream the list and collect the elements with the groupingBy method and supply the getDepartment method from the Employee class as a classification function. We end up with a Map containing the departments as keys and the corresponding employees as values for those keys.

We can add more groupingBy methods by chaining them together like so. If we not only want to group by department but by department and employee position we can add another groupingBy method call. Have a look at the parameterization of the map that is returned and it is easy to understand how the result looks like.

Finally we can answer this question: What are the minimum, maximum and the sum of all employee salaries in our departments? Additionally we use the summarizingInt collector with the groupingBy method. Since a salary obviously may not be an integer value this is not perfectly accurate but I just want to give you a quick idea of what’s possible here. This time we end up with an IntSummaryStatistics instance for each department and can easily check out the calculation results.

Check out other Collectors

There are many other helpful Collector methods – just have a look at the JavaDoc and check out what may be useful for you.