WEBVTT

00:00.620 --> 00:01.190
Hi everyone.

00:01.190 --> 00:02.510
Welcome back to this tutorial.

00:02.510 --> 00:07.460
In this tutorial we will code and learn about the max by and min by collector.

00:09.360 --> 00:17.250
The max by and min by collector takes in a comparator as an input and returns the optional as an output.

00:18.960 --> 00:26.520
The maximal collector is always used in conjunction with a comparator and returns the max element based

00:26.520 --> 00:29.280
on the property passed to the comparator.

00:30.330 --> 00:34.630
The Minbar collector is again used in conjunction with the comparator.

00:34.650 --> 00:40.410
It returns the smallest element based on the property passed to the comparator.

00:40.440 --> 00:42.270
Let's go ahead and code this one.

00:42.990 --> 00:44.880
Have the IntelliJ open in here.

00:44.880 --> 00:50.310
What I'm going to do is I'm going to create a new class called Dreams.

00:51.330 --> 00:51.990
Min by.

00:52.860 --> 00:54.900
Max by example.

00:56.590 --> 01:01.300
And let's make this class executable by adding public static void main.

01:04.170 --> 01:04.950
The first.

01:05.860 --> 01:10.510
Function that we are going to explore is a min by basically the min by collector.

01:11.140 --> 01:14.190
So I'm going to have public static.

01:14.200 --> 01:20.410
It's going to be a optional, as I mentioned in the presentation, the min by and max by always return

01:20.410 --> 01:21.460
the optional.

01:21.680 --> 01:26.470
So in here it is going to return optional of type student and then.

01:28.140 --> 01:28.770
Going to be.

01:28.770 --> 01:31.200
I'm going to name this function as min by.

01:31.950 --> 01:33.150
Let's import this.

01:35.360 --> 01:36.290
And put this one to.

01:37.700 --> 01:42.890
Now the first call is going to be the input, which is the student database dot.

01:44.130 --> 01:49.920
Get all students and this is going to return you, all the students, and we are going to start the

01:49.920 --> 01:50.670
stream.

01:50.910 --> 01:56.640
So the stream method is going to give us the stream of students and we are going to perform the collect

01:56.640 --> 01:57.480
operation.

01:57.480 --> 02:01.680
So in the collect operation we are going to use the min by.

02:04.700 --> 02:11.450
Which is part of the collectors because the method name here is also min by the IntelliJ is getting

02:11.450 --> 02:11.930
confused.

02:11.930 --> 02:14.990
I'm going to name this one as min by underscore.

02:15.890 --> 02:16.700
Example.

02:18.400 --> 02:18.790
Here.

02:18.790 --> 02:21.610
If you take a look at it, If I do a static import.

02:22.990 --> 02:24.150
Having some issues there.

02:24.160 --> 02:26.710
What I'm going to do, I'm going to type in min by.

02:29.290 --> 02:30.760
That's just this.

02:31.150 --> 02:31.490
There you go.

02:31.510 --> 02:34.000
You got the static method import option available.

02:34.000 --> 02:34.900
Click on that.

02:34.900 --> 02:37.870
So it is going to be Collectors dot min by.

02:38.720 --> 02:42.520
And in here it is going to take in an input as a comparator, right?

02:42.530 --> 02:47.300
So comparator dot comparing there is an comparing option in here.

02:47.300 --> 02:50.060
We are going to pass the student GPA.

02:51.260 --> 02:58.190
Basically the idea is to get the student who has the least GPA in this whole list collection.

02:59.060 --> 03:00.110
We're going to return this.

03:01.810 --> 03:08.650
So if we go to the students, the least student is 3.5 and the name of the student is Sophia.

03:10.490 --> 03:12.620
Let's run this example.

03:13.720 --> 03:17.710
So then I'm going to call Min by example method.

03:18.280 --> 03:21.370
This is going to give you the result as a.

03:23.030 --> 03:24.830
Current, which is going to be Sofia.

03:25.700 --> 03:26.730
There you go.

03:26.780 --> 03:32.780
Sofia is a student who has the least grade out of the student collection that we have here, which is

03:32.780 --> 03:34.220
the list of students.

03:34.310 --> 03:37.310
Now let's explore the max by example.

03:37.310 --> 03:45.440
I'm going to just copy this and then put it here and rename this method as Max by example and the place

03:45.440 --> 03:46.130
of Min by.

03:46.130 --> 03:48.110
I'm going to use Max by.

03:48.230 --> 03:48.590
There you go.

03:48.590 --> 03:50.500
It gave me the recommendation already.

03:50.510 --> 03:51.710
I'm going to select that.

03:52.650 --> 04:01.950
And then if I run this example and pass the max by example as an input to this method, basically this

04:01.950 --> 04:02.970
is going to.

04:04.380 --> 04:08.390
Return the student who has the highest grade, which is Emily.

04:08.400 --> 04:10.680
Let's go to the student collection and verify that.

04:10.920 --> 04:12.000
Go to the student collection.

04:12.030 --> 04:15.810
Emily is the one which has the highest grade of all the students.

04:16.580 --> 04:24.900
So this is all about performing the min by and max by collector operation using the collect method.

04:24.920 --> 04:27.030
With this we came to the end of this tutorial.

04:27.050 --> 04:28.280
Thank you for watching.
