Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and learn about the max by and min by collector.
The max by and min by collector takes in a comparator as an input and returns the optional as an output.
The maximal collector is always used in conjunction with a comparator and returns the max element based
on the property passed to the comparator.
The Minbar collector is again used in conjunction with the comparator.
It returns the smallest element based on the property passed to the comparator.
Let's go ahead and code this one.
Have the IntelliJ open in here.
What I'm going to do is I'm going to create a new class called Dreams.
Min by.
Max by example.
And let's make this class executable by adding public static void main.
The first.
Function that we are going to explore is a min by basically the min by collector.
So I'm going to have public static.
It's going to be a optional, as I mentioned in the presentation, the min by and max by always return
the optional.
So in here it is going to return optional of type student and then.
Going to be.
I'm going to name this function as min by.
Let's import this.
And put this one to.
Now the first call is going to be the input, which is the student database dot.
Get all students and this is going to return you, all the students, and we are going to start the
stream.
So the stream method is going to give us the stream of students and we are going to perform the collect
operation.
So in the collect operation we are going to use the min by.
Which is part of the collectors because the method name here is also min by the IntelliJ is getting
confused.
I'm going to name this one as min by underscore.
Example.
Here.
If you take a look at it, If I do a static import.
Having some issues there.
What I'm going to do, I'm going to type in min by.
That's just this.
There you go.
You got the static method import option available.
Click on that.
So it is going to be Collectors dot min by.
And in here it is going to take in an input as a comparator, right?
So comparator dot comparing there is an comparing option in here.
We are going to pass the student GPA.
Basically the idea is to get the student who has the least GPA in this whole list collection.
We're going to return this.
So if we go to the students, the least student is 3.5 and the name of the student is Sophia.
Let's run this example.
So then I'm going to call Min by example method.
This is going to give you the result as a.
Current, which is going to be Sofia.
There you go.
Sofia is a student who has the least grade out of the student collection that we have here, which is
the list of students.
Now let's explore the max by example.
I'm going to just copy this and then put it here and rename this method as Max by example and the place
of Min by.
I'm going to use Max by.
There you go.
It gave me the recommendation already.
I'm going to select that.
And then if I run this example and pass the max by example as an input to this method, basically this
is going to.
Return the student who has the highest grade, which is Emily.
Let's go to the student collection and verify that.
Go to the student collection.
Emily is the one which has the highest grade of all the students.
So this is all about performing the min by and max by collector operation using the collect method.
With this we came to the end of this tutorial.
Thank you for watching.