Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will explore the group by which accepts two parameter as an input in the previous
tutorial.
If you take a look at it, we just passed the one parameter version.
We are going to explore the two parameter version in this tutorial.
The first use case that we are going to code, we are going to create a two level grouping.
Going to create a method called public static void.
Level.
Grouping, and then I'm going to give it as one because we'll be coding multiple examples on the same
two level, on the same two parameter version of grouping.
Why?
So I'm going to call the get all students and then the next call is going to be stream and the next
call is going to be collect.
So we want to collect or we want to group the students based on their grade level.
So it's going to be student colon, colon grade level.
So this is the classifier for this we have to give the grouping by.
Let's close it here.
So this is similar to the previous version that we have here in here, what we are going to do, we
are going to add the second parameter.
If you go ahead and take a look at the grouping by it, accept it, accept the first parameter, which
is a classifier, and the second parameter which is a downstream.
So this downstream can be any type of grouping by implementation or any type of collector implementation.
Basically what I'm going to do again in this example, I'm going to do another grouping by basically
what it is.
What is this going to happen is that this is going to create another level of map.
So here we have the map and the value values or list of students.
But in this use case, it is going to be a map of string.
And again, another map here.
So in here, what we are going to do, I'm going to group the students with this same category.
There you go.
So let's take a look at the output.
It's going to be map of string, which is going to hold the grade level.
And then the values are again, going to be map of string.
And list of student.
So this is called.
Global mapping.
So this is called two level mapping.
Let's see.
There are some issues here.
Let's go ahead and take a look at it.
This is going to be a student map.
This is good here in here.
What is going on?
Let's give the name as student map.
The map of it would be a string.
Let's go and check what is a grade level or the grade level is.
And so in that case it is going to be integer.
There you go.
So let's revisit this example again.
So in this example, we are performing two level grouping.
The first grouping is at the grade level and the second grouping is again going to be similar to the
one which we coded here.
It is going to tag each and every student as outstanding or average based on their GPA.
Let's go ahead and run this example.
Once you look at the result it will make it will give you more idea or more understanding of what's
going on here.
I'm going to run this example.
And we are not printing anything here.
That's the reason why we don't see anything here.
I'm going to print it here.
Run this example again.
There you go.
You have the result here.
Let's let's review the result here.
So here it has the first level of map, which has a key as a grade level and the second level of map,
which is tagging each and every student in each and every grade as average and outstanding.
If you take a look at it in the second grade, there is one average student who is Adam and there is
one outstanding student who is Jenny.
That's the same case with the third grade and fourth grade.
In the fourth grade, there is no average student because their GPA is 4.0 and 3.9, which is matching
the outstanding key.
Let's explore one more example on this same topic.
So here we have used grouping by rate.
We can also use.
Some other.
I just copied the method from there and put it here.
Let's use some other collector here.
The collector is going to be.
Summing int.
So summing int is one collector which we have explored in one of the previous tutorial.
I'm going to use the summing int.
What it is going to accept is that it is going to take student colon colon notebooks.
So here it is going to be.
A map of integer.
An integer, right?
Because this summing int is again going to return an integer and this grade level is already mapping
to an integer.
So this is another way of using the two parameter grouping by.
Method.
So the first one is a classifier, which is going to be the key for the total.
Output map.
And then this determines what is a value that it is going to have.
Let's call this method and check the result.
Want to take a look at it.
I'm going to run this.
Run.
The result?
The two has 23 and three.
Third grade has 19 books and fourth grade has 29 books.
And if you want to group them based on based on their name, you can do that too.
If we do, a get name is going to give you the key as.
Thus because it's going to be a string, because getName is of type string, right?
If I run this example, it is going to have name and their appropriate notebooks count.
There you go.
Adam as a notebook.
Adam as 11 notebooks.
Emily has ten.
The following students have their appropriate notebook count mapped here, and it is grouped as a map
here.
So this is all about the two parameter grouping by.
With this.
We came to the end of this tutorial.
Thank you for watching.