Group by in Java 8
By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this operation as we did in the SQL (Structured Query Language). This GROUP BY is one of the SQL aggregate operations in SQL, and this is used when we are using aggregate functions in SQL. So, we can do similarly in Java8 now. In Java 8 the method is represented as “Collectors.groupingBy()”. Using this Collectors.groupingBy() in the Java programming language is quite useful.
How will this group in Java 8 be useful?
As mentioned, Java8 is directly allowing the programmers, or else we can tell the developers to do GROUP BY operation by Collectors.groupingBy(). This method helps the developers to group the records on certain things, how and what we mentioned. It means that it gives the result of how we used it in the program.
How does it work?
Suppose we take a group of people who are living in different countries. Like some people are living in India, Tokyo, Singapore, Paris, Australia and other countries. Now the task is to do on the information given we have to group that group of people who are living the respective countries. In a Java programming language, this can be done by using the loop, checking each person in which country he is living and putting the result on the HashMap List with the same country they are living. But when we use Collectors.groupingBy() method in Java8, we can get the information much more clearly, and we do not need to check everything by using the loop as mentioned before point. In Java8 collector and also stream provides groupingBy() method to do these kinds of operations. By using the method, we can perform tasks like concurrently grouping the objects by using concurrent collectors.
Simply we can say that these method Collectors.groupingBy() in Java8 enables the developer for data categorizing. This way of categorizing the data is the most useful way and also the most efficient way for categorizing the data in Java8. Because of this method, Collectors.groupingBy() has a substantial value. This Collector.groupingBy() method works in the most efficient way, especially when this method is combined with the overloaded versions of grouping().
How to group objects in Java 8?
The helpful thing about Java8 in this grouping method is that it made it much easier to do the work. We need to iterate over the loop by checking each statement and by putting the result into the HashMap List every time for every iteration, which makes the task complex in JavaSE 6 or 7. So when we compare the approaches that are done in Java8 and JavaSE 6 or 7, we can estimate that many tasks become easy in Java8. In Java8, the code is also not that much difficult to write it may take 5 to 6 lines of code which do the actual task. To avoid the NullPointerException when we are writing the program, we need to check the null condition everywhere in the program. Just by changing the grouping criterion, we can create multiple groups in the program and then we can execute them. The important thing is to pass the grouping criterion to the Collector.
Let us know some overloaded methods of grouping.
- One of them is with a classification function as the method parameter.
static <A, B> Collector<A, ? , List <A>>>
groupingBy(Function< ? super A, ? extends B> classifier)
- Now with a classification function and a second collector as method parameters.
static <A, B, C, D> Collector <T, ?, Map<E, F>>
groupingBy(Function< ? super A, ? extends B> classifier,
Collector<? super A, C, D> downstream)
- Here we use a supplier method with classification function as method parameters.
static <A, B, C, D, F extends Map<E, D>> Collector< A, ?, D>
groupingBy(Function< ? super A, ? extends B> classifier,
Supplier<F> mapFactory, Collector < ? super A, C, D> downstream)
Program for grouping the objects
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Grouping {
public static void main(String[] args)
{
List<String> g
= Arrays.asList("Welcome", "to", "India");
// by grouping() method
Map<String, Long> result
= g.stream().collect(
Collectors.groupingBy(
Function.identity(),
Collectors.counting()));
System.out.println(result);
}
}
Output:
