WEBVTT

00:00.470 --> 00:01.010
Hi everyone.

00:01.010 --> 00:02.330
Welcome back to this tutorial.

00:02.330 --> 00:07.190
In this tutorial we will code and explore the partitioning by collector.

00:07.580 --> 00:12.770
The partitioning by collector is also a kind of grouping by collector.

00:14.010 --> 00:20.640
The partitioning by accepts a predicate as an input and the output is going to be a map.

00:22.080 --> 00:27.480
And the key of the return type is always going to be a Boolean.

00:28.550 --> 00:35.270
And then there are two different versions of partitioning by one with a one argument version which accepts

00:35.270 --> 00:38.780
a predicate as an input, and the another one is a predicate.

00:38.780 --> 00:42.760
And the downstream, which is the two parameter version.

00:42.770 --> 00:44.420
Let's go ahead and code this one.

00:46.440 --> 00:47.250
What I'm going to do.

00:47.280 --> 00:50.850
I'm going to create a class called Streams.

00:53.240 --> 00:59.150
Partitioning by sample, and then I'm going to make this class executable.

00:59.180 --> 01:01.400
By adding public static void main.

01:03.440 --> 01:05.030
Understand presentation mode.

01:05.940 --> 01:09.630
And then what I'm going to do, I'm going to create an example.

01:10.570 --> 01:14.110
I'm going to create a method, basically public static void.

01:15.930 --> 01:17.640
Partitioning by.

01:18.650 --> 01:22.400
Want to give it a name as one, because I'm going to quote a couple of examples here.

01:23.830 --> 01:28.750
And let's call the student database class and get all the students.

01:29.780 --> 01:34.640
Call the stream method and then perform the collect method.

01:35.120 --> 01:38.630
So we are going to explore the partitioning by.

01:38.810 --> 01:39.590
Right.

01:40.850 --> 01:41.960
Partitioning.

01:43.570 --> 01:49.720
Thanks to this partitioning by is are going to do a static import of this one from the collectors.

01:49.750 --> 01:52.960
As you see, we have the partitioning by here.

01:54.190 --> 01:55.880
Let's create a predicate here.

01:55.900 --> 01:59.020
The predicate is going to be because we all know partitioning.

01:59.020 --> 02:04.060
If you go and take a look at it, it accepts there are two different versions, but it accepts.

02:05.420 --> 02:05.750
The.

02:07.060 --> 02:08.050
Predicate as an input.

02:08.050 --> 02:09.310
Let's create a predicate.

02:11.210 --> 02:14.870
The predicate is going to be of type student.

02:15.290 --> 02:18.440
I'm going to create a predicate.

02:21.630 --> 02:24.210
This predicate is going to be.

02:27.800 --> 02:29.020
Student dot.

02:30.500 --> 02:35.000
Didn't dot get greater than or equal to 3.8.

02:36.600 --> 02:40.140
So I'm going to parse this predicate here.

02:41.000 --> 02:44.450
The output that it is going to create is going to be a map.

02:44.480 --> 02:48.590
If you go to the partitioning by, it's going to create a map of type boolean.

02:48.620 --> 02:49.870
You cannot override that.

02:49.880 --> 02:50.450
Right.

02:50.450 --> 02:52.730
So it's going to be map.

02:53.780 --> 02:56.620
To import the class which is of type Java.util.

02:58.110 --> 03:05.160
The key is going to be Boolean and the result is going to be a list of prints.

03:06.270 --> 03:06.960
Basically student.

03:10.780 --> 03:14.620
So I'm going to name this one as partitioning map.

03:15.740 --> 03:16.340
Right.

03:16.460 --> 03:19.070
And then let's print this one out.

03:19.790 --> 03:21.530
Partitioning map equal to.

03:22.990 --> 03:24.100
I'm going to call this method.

03:26.010 --> 03:33.960
So the output is going to be whatever that matches this predicate is going to be under the true key.

03:33.990 --> 03:38.190
Whatever the doesn't match, this one is going to be under the false key.

03:38.310 --> 03:41.340
Let's execute this and validate the result.

03:42.250 --> 03:45.280
If you take a look at it, there are two keys.

03:45.280 --> 03:48.730
One is the false key and another one is the true key.

03:48.760 --> 03:55.240
So in the false key, whoever has the GPA less than or equal to 3.8, if you take a look at the GPA

03:55.240 --> 03:56.950
of this one, it is 3.6.

03:56.950 --> 03:59.320
And the GPA of this one is 3.5.

03:59.350 --> 04:06.550
Under the true category, whatever that has the GPA greater than or equal to 3.8, all those things

04:06.550 --> 04:08.530
falls under the true key.

04:10.280 --> 04:13.130
So this is the one argument version.

04:13.130 --> 04:15.740
Let's take a look at the two argument version.

04:16.960 --> 04:19.090
Gonna name this one as partitioning.

04:19.450 --> 04:21.130
Underscore two.

04:22.150 --> 04:24.220
Auditioning underscore two in here.

04:24.220 --> 04:25.210
What we are going to do.

04:25.240 --> 04:27.820
It is going to take in a second argument.

04:27.850 --> 04:31.930
The second argument is a downstream basically, this is again another collector.

04:31.930 --> 04:37.240
So what I'm going to do, I'm going to override this by passing the two set collector.

04:37.570 --> 04:42.040
So what's going to happen instead of this list we are going to have set?

04:43.300 --> 04:43.930
Imported.

04:45.110 --> 04:45.680
There you go.

04:45.890 --> 04:48.380
And then we need to.

04:49.330 --> 04:50.620
What's happening here?

04:50.800 --> 04:53.440
This should not be any issues, actually.

04:53.470 --> 04:55.720
So in here we are passing the two set.

04:55.750 --> 04:57.820
I believe we need to provide one more.

04:59.840 --> 05:01.130
Some issues here.

05:01.990 --> 05:02.960
Is this a comma?

05:03.290 --> 05:03.920
Yep.

05:04.340 --> 05:05.360
You have it.

05:07.010 --> 05:07.520
Okay.

05:08.800 --> 05:10.360
Uh, we should have the comma here.

05:10.360 --> 05:12.130
We should not close that, actually.

05:13.680 --> 05:17.350
Because partitioning by is the one which is going to take two arguments, right?

05:17.370 --> 05:21.540
The first argument is a predicate, and second argument is a downstream.

05:21.540 --> 05:24.210
Basically, there is nothing but a collector, right?

05:24.210 --> 05:26.220
So to set is one of the collector.

05:26.570 --> 05:31.620
Then what I'm going to do, I'm going to call this method, I'm going to comment this one so that it

05:31.620 --> 05:33.060
can avoid the confusion.

05:33.060 --> 05:36.270
The result is going to be the same here In this use case.

05:36.270 --> 05:41.730
It is just that we are overriding the default type of list to to set.

05:41.760 --> 05:43.860
You can pass any kind of collector here.

05:45.020 --> 05:46.790
Gonna run this one.

05:48.180 --> 05:53.510
But on this one, it is going to give me the output as similar to the previous scenario, but it is

05:53.660 --> 05:55.610
of type set instead of a list.

05:56.000 --> 05:58.760
So this is all about the partitioning by.

05:58.790 --> 06:00.950
With this we came to the end of this tutorial.

06:00.980 --> 06:02.120
Thank you for watching.
