Group By in Java 8

The Collectors.groupingBy() method in Java 8 now permits developers to perform GROUP BY operation directly. GROUP BY is a SQL aggregate operation that is quite useful. It enables you to categorise records based on specified criteria. In Java, how do you group by? For example, if you have a list of people, how do you organise them by city? Is it better to visit London, Paris, or Tokyo? We could accomplish this with a for loop, checking each individual, and placing them on a list of HashMaps with the same city, but in Java 8, you don't have to hack your way around like that; you have a far simpler alternative. To accomplish this, you can use the groupingBy() method provided by Stream and Collector.

It has a substantial value because it is one of the most frequent ways to aggregate data, especially when combined with the different overloaded versions of groupingBy(), which also allows you to group items concurrently by utilising concurrent Collectors.

How to group objects in Java 8?

Example

Here is a Java 8 and older version sample program for grouping objects based on their characteristics. First, we'll look at how we could achieve this in the pre-Java 8 environment, and then we'll look at a Java 8 group example. Looking at both techniques, it's clear that Java 8 has made your job a lot easier.

To build a group of objects from a list in Java SE 6 or 7, you must iterate over the list, inspect each element, and place them into their own list. To store these groupings, you'll also need a Map. When you get the initial item for a new group, you make a list and add it to the list, but if the group already exists in Map, you just fetch it and add your element to it.

The code is not difficult to write, but it does take 5 to 6 lines, and you must check for null references everywhere to avoid a NullPointerException. In Java 8, you retrieve the stream from the list and use a Collector to group them in one line of code. It's as simple as passing the grouping condition to the collector and it is complete.

By simply modifying the grouping condition, you can create multiple groups. To establish a group of different criteria in the previous edition, you had to write the same 5 to 6 lines of code.

You may even use the new Stream API to perform aggregate functions like sum(), count(), max(), and min() in specific groups, as shown in my previous streams examples. Individual groups are, after all, just a list of objects on which you may access the stream by invoking the stream() method. In other words, you can now accomplish SQL-style grouping in Java without having to use a loop.

Grouping Objects Java Program

GroupByDemo.java

Output:

Employees grouped by cities before Java 8 : {Delhi=[Dipak(Delhi,21), Sumit(Delhi,21), Karan(Delhi,23)], Banglore=[Rajesh(Banglore,23), Niraj(Banglore,31)], Udaipur=[Mona(Udaipur,23)]}
Employees grouped by cities in Java 8: {Delhi=[Dipak(Delhi,21), Sumit(Delhi,21), Karan(Delhi,23)], Banglore=[Rajesh(Banglore,23), Niraj(Banglore,31)], Udaipur=[Mona(Udaipur,23)]}
Employees grouped by age in Java 8: {21=[Dipak(Delhi,21), Sumit(Delhi,21)], 23=[Karan(Delhi,23), Mona(Udaipur,23), Rajesh(Banglore,23)], 31=[Niraj(Banglore,31)]}

We are categorising a list of Employee objects by their city in the above example. Three employee from Delhi, two from Udaipur, and one from Banglore are on our list. After sorting them by city, you can see that they each have their own list; there is one employee in the Udaipur list, three in the Delhi list, and two in the Banglore list. The groups formed by the Java 7 and Java 8 examples are identical.

Later, it is divided into three groups based on their age, and as shown we have three groups for distinct age groups, 21, 23, and 31.