In this video of code decode we have demonstrated how we can do Group By in java 8 same as we do in SQL using Group By Clause in SQL Queries. Its java 8 Group By coding interview questions of java 8 using collectors and grouping by concept.
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
openinapp.co/udemycourse
Course Description Video :
yt.openinapp.co/dmjvd
Why do we need Grouping Collector?
Given a stream of objects, there are scenarios where these objects need to be grouped based on a certain distinguishing characteristic they posses.
E.G. -
Create a map with key as Age and value as list of employees in that age group.
This concept of grouping is the same as the ‘group by’ clause in SQL which takes an attribute, or a calculated value derived from attribute(s), to divide the retrieved records in distinct groups.
Generally what we used to do to group by is -
iterating over each object, checking which group the object being examined falls in, and then adding that object in its correct group. The group itself is held together using a Collection instance.
grouping collector with single parameter
The classification function passed to groupingBy() method is the method specified as "Employee.getAge()".
As the end result of applying the grouping collector for achieving this we want a Map with keys as age and corresponding values as List of employees of that age.
2 parameter Collectors.groupingBy()- uses a user specified Collector to collect grouped elements
Whereas the 1st variant always returned a List containing the elements of a group, the 2nd variant of grouping collector provides the flexibility to specify how the grouped elements need to be collected using a second parameter which is a Collector.
So, instead of just storing the groups in resultant Map as Lists, we can instead store them in say Sets,
(Collectors.groupingBy(Employee.getAger(), Collectors.toSet()));
Most Asked Core Java Interview Questions and Answers : • Core Java frequently a...
Advance Java Interview Questions and Answers : • Advance Java Interview...
Java 8 Interview Questions and Answers : • Java 8 Interview Quest...
Hibernate Interview Questions and Answers : • Hibernate Interview Qu...
Spring Boot Interview Questions and Answers : • Advance Java Interview...
Angular Playlist : • Angular Course Introdu...
SQL Playlist : • SQL Interview Question...
GIT : • GIT
Subscriber and Follow Code Decode
Subscriber Code Decode : ru-vid.com?...
Linkedin : / codedecodeyoutube
Instagram : / codedecode25
#codedecode #java8collectorsAndGroupingby #java8interviewquestionsandanswers
22 мар 2022