Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the max by with grouping by and min by with grouping by.
And the use case that we are going to create is that we are going to calculate the top student in each
grade and the least student in each grade.
Let's cut.
Let's cut the top student first, and then we'll cut the least student public required.
And then it is going to be calculate.
Top.
EP.
Okay.
And then we're going to do student database dot, get all students and then call the string method.
We're going to do that.
And followed by that, what we are going to do, we are going to do the collect operation.
The collect operation is going to take group by and then we are going to pass a key because we want
to know the top student in each grade, right?
So the key is going to be student colon, colon grade level.
And then followed by that, we are going to do a max by.
Right.
So this is again a collector.
And then we are going to give comparator dot comparing and we are going to pass the student colon.
Colon.
Let me put this down here so that it will be clear.
Student Colon.
Colon.
Get GPA.
So this is what we are going to do.
Let's review this code again.
Here we are performing grouping by along with Max, by if we take a look at it.
This is the two argument version of grouping by.
So we are passing the grade level and then in the comparing we are passing the GPA.
So this is going to give you a map.
Map of integer, which is a grid.
And then if you take a look at the max by max by will always return optional.
That's going to be optional.
Didn't.
And then we are going to give the name as turn map optional.
There you go.
Let's print this one.
And print this.
Let's execute this and then check the result.
What is that?
It is going to have.
This.
The sprinting for the second grade.
Jenny is the student who is having the top grade, which is 3.8.
For the third grade, Emily is a top grade student who has a 4.0.
For the fourth grade student, the student is James and the level is 3.9.
But if you take a look at the result, the values are wrapped inside the optional right?
So there is a way we can avoid this.
I'm going to implement the same thing here.
And I'm going to wrap this whole max by using collecting.
And then there is a method called collecting.
And then and I'm going to pass this as an input to this one.
And then what we are going to do, we are going to use optional.
Let me put this below so that the code will be clear.
Optional colon.
Colon.
Get.
So what this is going to do, this is going to wrapping it here.
That is not right.
Then let's put it here.
And then this is going to return a student.
The place of this.
Let's give it a name.
So what we are doing is instead of wrapping the student inside the optional this collecting, and then
what it is going to do, this is going to get the student of the student is available and then assign
that as an value.
So what I'm going to do, I'm going to copy this and then put it here and then I'm going to print optional
one.
When I come on this one, Let's validate this by running this example.
There you go.
It removed the optional object and then it gave you the actual object itself.
Now we have coded the top use case.
Let's go ahead and code the least use case.
I'm going to just copy and paste it here.
The example is the same here it is going to be calculate least GPA in the place of max by I'm going
to use min by.
Because of Maxpay.
I'm going to use Min Buy.
That's it.
If I run it, it is going to give you the student with who has the least GPA.
If you take a look at it.
Here.
We are still printing this.
I'm going to come on this.
I'm going to call this method, right?
I'm still calling the top here.
That's the reason why you have the top over there.
But we don't want that.
We want the least student.
If I run this, it is going to print the student who has the least GPA in each and every grade.
So for the second grade, Adam is a student who has the least GPA as.
3.6.
And for the third grade it is like it is day.
Who has the GPA as 3.9 for the fourth grade?
This grade level is.
Four and the 3.5 on the student name is Sofia.
So this is all about grouping the students based on their grade level.
Basically, we have coded the use case of grouping the grouping by along with the min by.
And then we have integrated the collecting.
And then also in this use case with this, we came to the end of this tutorial.
Thank you for watching.