Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the grouping by collector.
This is equivalent to performing a group by class in a SQL.
The basic idea is that it is going to group the elements based on a property that is being passed to
the group by collector.
The output of the group by is always going to be a map of key value pairs.
There are three different versions of grouping by.
We have the one parameter version which takes in a classifier as an input.
Basically that is a one which determines what is a classification value that needs to be applied to
that stream input.
Next is a two parameter version.
The first argument is going to be a classifier, and the second argument is a downstream, which is
nothing but another collector that can be of any type of collector.
What were we discussing until now?
The third one is the three parameter version, which takes in a classifier as a first input, and the
second one is going to be a supplier which is used to override the default map factory.
And then the third one is again going to be the downstream.
Let's explore the one argument grouping by as part of this tutorial and then we will focus on the other
group by versions in the future tutorials.
Let's go ahead and code this one.
Have the IntelliJ open in here.
What I'm going to do, I'm going to create a class called.
Green.
Grouping by example.
And then let's make this class executable by adding public static void main.
We are going to make use of the same student database class and the use case that we are going to code.
It's going to be we want to group the students based on their gender.
How do we do it?
Going to be public, static, void.
Group.
Students by.
Gender.
That's what we are going to do.
The first call is going to be student database dot, get all students dot stream, and then we're going
to call the collect method.
In the collect method, we are going to make use of the.
Grouping by.
So let's import the static import.
Here we go.
You have this.
Collectors are grouping by static import option available.
I'm going to select that in here.
If you take a look at it, there are different versions, right?
What I'm going to do, the first one is going to be print, colon.
Colon.
Let's import the student class.
Get gender.
So what this is going to do, this is basically going to group the student students based on the gender.
The output is going to be map of.
So the gender what is the type of it is of type string, Right.
So it's going to be string and it's going to give you a list of students.
That's what it's going to do.
List of.
Goodness.
And then let's import this one to.
Then import the map, which is part of the Java.util package and let's give it a name.
It's going to be student map.
And what I'm going to do, I'm going to print it here.
Map.
Now the next step is we are going to make a call to this method, actually.
Okay, let's run this and then check the result.
So there you go.
So we have the female key and male key and it has all the list of students in it.
The female has Jenny, Emily and Sophia, and the male has Adam, Dave and James.
So this is one way of grouping the students based on their.
Gender.
Now there isn't another version.
So we can group by the students using some customized values.
Here we have used a property, whatever that is part of the student class, right?
Let's say you want to give some customized key values.
It is also possible what I'm going to do, I'm going to copy this whole thing here and then I'm going
to name this one as customized.
Grouping by.
And the use case that I'm going to code here is that I want to group the students based on their GPA.
If you go here, if you go to the get all students.
Method.
It has all the students right from different genders.
I want to categorize the students based on their GPA.
If the GPA is 3.8, I want the key to be outstanding.
If the GPA is less than 3.53.8, then I want them to categorize as average.
So the use cases.
If the GPA is greater than or equal to 3.8, 3.8, then they are outstanding students.
If the GPA is less than or equal less than 3.8, then those students are going to be average students.
So in the grouping by what I'm going to do, I'm going to create a lambda student.
Student dot get GPA if it is greater than or equal to 3.8.
And then what I'm going to do, I'm going to assign the keys.
Outstanding.
If not, they are going to be in the average category.
Okay, What I'm going to do, I'm going to comment this one and then I'm going to make the call to this
customized grouping so that it won't be confused when we are going to check the result in the console.
So we are making the call to this one.
Let's revisit this one again.
So the student get all student method is going to return you a list of six students.
And then we are calling this Stream method, which is going to create a stream of students.
And then what we are doing is we are grouping the student by using a customized implementation of key.
So this string is going to get outstanding or average based on their GPA.
If the GPA of the student is greater than or equal to 3.8, then those students will fall and fall under
the category of outstanding.
Otherwise, those students will fall under the category of average.
Let's run this and then check the result.
There you go.
It ran.
The result has the average key and outstanding key.
Let's review the of the students who are under the average.
If you take a look at the average students, they have the 3.6 and Sofia has 3.5.
So for the outstanding students, if you take a look at it, the GPA as three is 3.8 and 4.0 and 3.9
and 3.9.
So this is one another way of grouping the students based on your customized implementation of the grouping
by implementation.
This is all about the approach.
One, let's discuss about the multiple level of grouping and some more advanced concepts in the future
tutorials.
With this, we came to the end of this tutorial.
Thank you for watching.