WEBVTT

00:00.290 --> 00:00.830
Hi everyone.

00:00.830 --> 00:02.180
Welcome back to this tutorial.

00:02.180 --> 00:10.100
In this tutorial we will code and explore the max by with grouping by and min by with grouping by.

00:10.130 --> 00:17.870
And the use case that we are going to create is that we are going to calculate the top student in each

00:17.870 --> 00:21.350
grade and the least student in each grade.

00:22.030 --> 00:22.590
Let's cut.

00:22.600 --> 00:30.550
Let's cut the top student first, and then we'll cut the least student public required.

00:30.910 --> 00:33.520
And then it is going to be calculate.

00:34.580 --> 00:34.990
Top.

00:36.120 --> 00:36.500
EP.

00:38.680 --> 00:38.980
Okay.

00:38.980 --> 00:45.760
And then we're going to do student database dot, get all students and then call the string method.

00:45.790 --> 00:46.810
We're going to do that.

00:46.810 --> 00:52.150
And followed by that, what we are going to do, we are going to do the collect operation.

00:52.180 --> 00:58.420
The collect operation is going to take group by and then we are going to pass a key because we want

00:58.420 --> 01:02.380
to know the top student in each grade, right?

01:02.380 --> 01:06.910
So the key is going to be student colon, colon grade level.

01:06.970 --> 01:10.990
And then followed by that, we are going to do a max by.

01:12.300 --> 01:12.690
Right.

01:12.690 --> 01:14.250
So this is again a collector.

01:14.250 --> 01:22.110
And then we are going to give comparator dot comparing and we are going to pass the student colon.

01:22.110 --> 01:22.770
Colon.

01:23.740 --> 01:26.140
Let me put this down here so that it will be clear.

01:26.170 --> 01:26.890
Student Colon.

01:26.890 --> 01:27.370
Colon.

01:27.370 --> 01:28.480
Get GPA.

01:29.970 --> 01:31.550
So this is what we are going to do.

01:31.600 --> 01:33.190
Let's review this code again.

01:33.210 --> 01:38.520
Here we are performing grouping by along with Max, by if we take a look at it.

01:38.520 --> 01:41.400
This is the two argument version of grouping by.

01:41.430 --> 01:46.950
So we are passing the grade level and then in the comparing we are passing the GPA.

01:46.950 --> 01:50.520
So this is going to give you a map.

01:52.390 --> 01:56.200
Map of integer, which is a grid.

01:56.200 --> 02:00.760
And then if you take a look at the max by max by will always return optional.

02:00.790 --> 02:03.640
That's going to be optional.

02:06.440 --> 02:06.800
Didn't.

02:08.590 --> 02:14.530
And then we are going to give the name as turn map optional.

02:15.190 --> 02:15.880
There you go.

02:16.000 --> 02:17.350
Let's print this one.

02:23.150 --> 02:24.260
And print this.

02:25.490 --> 02:29.000
Let's execute this and then check the result.

02:29.030 --> 02:29.630
What is that?

02:29.630 --> 02:30.680
It is going to have.

02:34.080 --> 02:34.650
This.

02:35.570 --> 02:38.560
The sprinting for the second grade.

02:38.570 --> 02:43.550
Jenny is the student who is having the top grade, which is 3.8.

02:43.580 --> 02:48.830
For the third grade, Emily is a top grade student who has a 4.0.

02:48.860 --> 02:56.720
For the fourth grade student, the student is James and the level is 3.9.

02:58.140 --> 03:03.990
But if you take a look at the result, the values are wrapped inside the optional right?

03:03.990 --> 03:06.420
So there is a way we can avoid this.

03:07.470 --> 03:10.020
I'm going to implement the same thing here.

03:11.880 --> 03:17.670
And I'm going to wrap this whole max by using collecting.

03:17.670 --> 03:21.180
And then there is a method called collecting.

03:21.180 --> 03:25.950
And then and I'm going to pass this as an input to this one.

03:26.130 --> 03:32.490
And then what we are going to do, we are going to use optional.

03:33.420 --> 03:36.420
Let me put this below so that the code will be clear.

03:36.450 --> 03:37.470
Optional colon.

03:37.470 --> 03:37.770
Colon.

03:37.770 --> 03:38.370
Get.

03:38.610 --> 03:43.440
So what this is going to do, this is going to wrapping it here.

03:43.440 --> 03:44.790
That is not right.

03:45.930 --> 03:47.430
Then let's put it here.

03:49.370 --> 03:52.790
And then this is going to return a student.

03:53.710 --> 03:54.670
The place of this.

03:54.670 --> 03:55.600
Let's give it a name.

03:55.600 --> 04:02.560
So what we are doing is instead of wrapping the student inside the optional this collecting, and then

04:02.560 --> 04:08.470
what it is going to do, this is going to get the student of the student is available and then assign

04:08.470 --> 04:09.750
that as an value.

04:09.760 --> 04:16.360
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

04:16.360 --> 04:16.900
one.

04:17.690 --> 04:22.130
When I come on this one, Let's validate this by running this example.

04:23.260 --> 04:23.900
There you go.

04:23.920 --> 04:29.680
It removed the optional object and then it gave you the actual object itself.

04:30.410 --> 04:33.650
Now we have coded the top use case.

04:33.650 --> 04:36.530
Let's go ahead and code the least use case.

04:37.390 --> 04:39.670
I'm going to just copy and paste it here.

04:40.000 --> 04:49.090
The example is the same here it is going to be calculate least GPA in the place of max by I'm going

04:49.090 --> 04:50.500
to use min by.

04:51.520 --> 04:52.860
Because of Maxpay.

04:52.870 --> 04:53.950
I'm going to use Min Buy.

04:54.580 --> 04:55.480
That's it.

04:55.510 --> 04:59.680
If I run it, it is going to give you the student with who has the least GPA.

04:59.770 --> 05:00.970
If you take a look at it.

05:02.310 --> 05:03.000
Here.

05:03.120 --> 05:04.710
We are still printing this.

05:05.160 --> 05:06.420
I'm going to come on this.

05:06.450 --> 05:08.370
I'm going to call this method, right?

05:08.490 --> 05:11.040
I'm still calling the top here.

05:11.850 --> 05:14.370
That's the reason why you have the top over there.

05:14.370 --> 05:15.570
But we don't want that.

05:15.570 --> 05:17.460
We want the least student.

05:17.490 --> 05:23.430
If I run this, it is going to print the student who has the least GPA in each and every grade.

05:23.460 --> 05:28.260
So for the second grade, Adam is a student who has the least GPA as.

05:29.170 --> 05:30.100
3.6.

05:30.100 --> 05:33.280
And for the third grade it is like it is day.

05:33.310 --> 05:36.900
Who has the GPA as 3.9 for the fourth grade?

05:36.910 --> 05:38.500
This grade level is.

05:39.820 --> 05:43.650
Four and the 3.5 on the student name is Sofia.

05:43.870 --> 05:48.280
So this is all about grouping the students based on their grade level.

05:48.310 --> 05:54.400
Basically, we have coded the use case of grouping the grouping by along with the min by.

05:54.520 --> 05:57.310
And then we have integrated the collecting.

05:57.310 --> 06:02.300
And then also in this use case with this, we came to the end of this tutorial.

06:02.320 --> 06:03.520
Thank you for watching.
