WEBVTT

00:00.410 --> 00:00.950
Hi everyone.

00:00.950 --> 00:02.210
Welcome back to this tutorial.

00:02.210 --> 00:06.890
In this tutorial we will code and explore the grouping by collector.

00:07.280 --> 00:13.010
This is equivalent to performing a group by class in a SQL.

00:14.100 --> 00:20.310
The basic idea is that it is going to group the elements based on a property that is being passed to

00:20.310 --> 00:21.930
the group by collector.

00:22.800 --> 00:27.660
The output of the group by is always going to be a map of key value pairs.

00:28.800 --> 00:31.770
There are three different versions of grouping by.

00:32.100 --> 00:37.050
We have the one parameter version which takes in a classifier as an input.

00:37.050 --> 00:43.740
Basically that is a one which determines what is a classification value that needs to be applied to

00:43.740 --> 00:45.270
that stream input.

00:45.570 --> 00:47.430
Next is a two parameter version.

00:47.430 --> 00:52.710
The first argument is going to be a classifier, and the second argument is a downstream, which is

00:52.710 --> 00:56.730
nothing but another collector that can be of any type of collector.

00:56.730 --> 00:58.560
What were we discussing until now?

01:01.160 --> 01:06.440
The third one is the three parameter version, which takes in a classifier as a first input, and the

01:06.440 --> 01:12.710
second one is going to be a supplier which is used to override the default map factory.

01:12.710 --> 01:16.460
And then the third one is again going to be the downstream.

01:16.460 --> 01:23.420
Let's explore the one argument grouping by as part of this tutorial and then we will focus on the other

01:23.420 --> 01:26.710
group by versions in the future tutorials.

01:26.720 --> 01:28.190
Let's go ahead and code this one.

01:29.730 --> 01:31.920
Have the IntelliJ open in here.

01:31.920 --> 01:35.310
What I'm going to do, I'm going to create a class called.

01:36.560 --> 01:37.010
Green.

01:39.960 --> 01:41.850
Grouping by example.

01:43.320 --> 01:48.090
And then let's make this class executable by adding public static void main.

01:51.620 --> 01:58.010
We are going to make use of the same student database class and the use case that we are going to code.

01:58.010 --> 02:02.150
It's going to be we want to group the students based on their gender.

02:02.150 --> 02:03.140
How do we do it?

02:03.680 --> 02:05.750
Going to be public, static, void.

02:07.140 --> 02:07.590
Group.

02:09.710 --> 02:11.150
Students by.

02:12.370 --> 02:12.940
Gender.

02:13.120 --> 02:14.380
That's what we are going to do.

02:14.650 --> 02:23.200
The first call is going to be student database dot, get all students dot stream, and then we're going

02:23.200 --> 02:24.310
to call the collect method.

02:24.310 --> 02:27.190
In the collect method, we are going to make use of the.

02:28.510 --> 02:29.680
Grouping by.

02:31.750 --> 02:33.730
So let's import the static import.

02:33.910 --> 02:34.380
Here we go.

02:34.390 --> 02:34.990
You have this.

02:34.990 --> 02:37.800
Collectors are grouping by static import option available.

02:37.810 --> 02:40.120
I'm going to select that in here.

02:40.120 --> 02:43.140
If you take a look at it, there are different versions, right?

02:43.150 --> 02:47.200
What I'm going to do, the first one is going to be print, colon.

02:47.200 --> 02:47.950
Colon.

02:48.400 --> 02:50.140
Let's import the student class.

02:52.140 --> 02:53.190
Get gender.

02:54.050 --> 02:59.990
So what this is going to do, this is basically going to group the student students based on the gender.

03:00.080 --> 03:02.840
The output is going to be map of.

03:02.930 --> 03:07.490
So the gender what is the type of it is of type string, Right.

03:07.760 --> 03:14.270
So it's going to be string and it's going to give you a list of students.

03:14.270 --> 03:15.560
That's what it's going to do.

03:16.070 --> 03:17.210
List of.

03:18.890 --> 03:19.400
Goodness.

03:20.950 --> 03:23.260
And then let's import this one to.

03:25.810 --> 03:30.580
Then import the map, which is part of the Java.util package and let's give it a name.

03:30.580 --> 03:33.760
It's going to be student map.

03:35.450 --> 03:37.550
And what I'm going to do, I'm going to print it here.

03:39.830 --> 03:40.080
Map.

03:41.330 --> 03:45.470
Now the next step is we are going to make a call to this method, actually.

03:46.580 --> 03:49.130
Okay, let's run this and then check the result.

03:52.810 --> 03:53.380
So there you go.

03:53.380 --> 04:00.290
So we have the female key and male key and it has all the list of students in it.

04:00.310 --> 04:09.310
The female has Jenny, Emily and Sophia, and the male has Adam, Dave and James.

04:09.400 --> 04:14.050
So this is one way of grouping the students based on their.

04:15.400 --> 04:16.020
Gender.

04:16.030 --> 04:18.220
Now there isn't another version.

04:18.220 --> 04:22.540
So we can group by the students using some customized values.

04:22.540 --> 04:27.670
Here we have used a property, whatever that is part of the student class, right?

04:27.670 --> 04:30.940
Let's say you want to give some customized key values.

04:30.940 --> 04:37.030
It is also possible what I'm going to do, I'm going to copy this whole thing here and then I'm going

04:37.030 --> 04:39.340
to name this one as customized.

04:40.470 --> 04:41.580
Grouping by.

04:42.240 --> 04:49.530
And the use case that I'm going to code here is that I want to group the students based on their GPA.

04:49.830 --> 04:53.520
If you go here, if you go to the get all students.

04:54.650 --> 04:55.250
Method.

04:55.280 --> 04:59.420
It has all the students right from different genders.

04:59.450 --> 05:02.720
I want to categorize the students based on their GPA.

05:03.020 --> 05:07.480
If the GPA is 3.8, I want the key to be outstanding.

05:07.490 --> 05:14.960
If the GPA is less than 3.53.8, then I want them to categorize as average.

05:14.960 --> 05:16.560
So the use cases.

05:16.580 --> 05:22.190
If the GPA is greater than or equal to 3.8, 3.8, then they are outstanding students.

05:22.220 --> 05:29.330
If the GPA is less than or equal less than 3.8, then those students are going to be average students.

05:29.930 --> 05:35.900
So in the grouping by what I'm going to do, I'm going to create a lambda student.

05:37.140 --> 05:43.980
Student dot get GPA if it is greater than or equal to 3.8.

05:45.000 --> 05:50.420
And then what I'm going to do, I'm going to assign the keys.

05:51.750 --> 05:52.640
Outstanding.

05:52.650 --> 05:56.970
If not, they are going to be in the average category.

05:58.750 --> 06:04.540
Okay, What I'm going to do, I'm going to comment this one and then I'm going to make the call to this

06:04.540 --> 06:11.080
customized grouping so that it won't be confused when we are going to check the result in the console.

06:11.080 --> 06:13.270
So we are making the call to this one.

06:13.570 --> 06:15.280
Let's revisit this one again.

06:15.280 --> 06:20.230
So the student get all student method is going to return you a list of six students.

06:20.230 --> 06:25.720
And then we are calling this Stream method, which is going to create a stream of students.

06:27.020 --> 06:34.320
And then what we are doing is we are grouping the student by using a customized implementation of key.

06:34.340 --> 06:40.250
So this string is going to get outstanding or average based on their GPA.

06:40.460 --> 06:46.730
If the GPA of the student is greater than or equal to 3.8, then those students will fall and fall under

06:46.730 --> 06:48.620
the category of outstanding.

06:48.650 --> 06:52.750
Otherwise, those students will fall under the category of average.

06:52.760 --> 06:55.430
Let's run this and then check the result.

06:57.330 --> 06:58.020
There you go.

06:58.050 --> 06:59.010
It ran.

06:59.280 --> 07:02.940
The result has the average key and outstanding key.

07:02.970 --> 07:06.030
Let's review the of the students who are under the average.

07:06.030 --> 07:12.570
If you take a look at the average students, they have the 3.6 and Sofia has 3.5.

07:12.600 --> 07:20.370
So for the outstanding students, if you take a look at it, the GPA as three is 3.8 and 4.0 and 3.9

07:20.370 --> 07:21.600
and 3.9.

07:21.630 --> 07:28.620
So this is one another way of grouping the students based on your customized implementation of the grouping

07:28.620 --> 07:30.450
by implementation.

07:31.770 --> 07:33.090
This is all about the approach.

07:33.090 --> 07:38.850
One, let's discuss about the multiple level of grouping and some more advanced concepts in the future

07:38.850 --> 07:39.730
tutorials.

07:39.750 --> 07:41.940
With this, we came to the end of this tutorial.

07:41.970 --> 07:43.080
Thank you for watching.
