WEBVTT

00:00.120 --> 00:00.680
Hi everyone.

00:00.680 --> 00:01.280
Welcome back.

00:01.280 --> 00:07.480
In this lecture we are going to be exploring an advanced custom gatherer using another example.

00:07.480 --> 00:09.640
So in this case I'm going to comment out this one.

00:09.680 --> 00:15.960
So in this section I'm going to uncomment this code here I'm going to be showing how to demonstrate

00:16.000 --> 00:17.400
a traditional grouping.

00:17.400 --> 00:22.400
So in this case if you go to this particular functionality what this particular functionality does is

00:22.400 --> 00:25.680
that we are going to be grouping the movies by decade.

00:25.680 --> 00:31.640
So in this case, based on the release year, we are going to be grouping the movies by decades.

00:31.640 --> 00:36.760
So in this case we stream on the movies and then we do the grouping by, we pass in the movie.

00:36.920 --> 00:42.680
And the key is going to be like 1940s, 1950s, 1960s, something like that.

00:42.680 --> 00:49.320
So we are going to be grouping the movie by decade, and then we are going to be sorting the decades

00:49.320 --> 00:51.480
for consistent output.

00:51.480 --> 00:52.680
So that's what we are going to do.

00:52.720 --> 00:55.880
Let me execute this program and then show you what the result is.

00:55.920 --> 00:59.600
And then we are going to be coding the same logic using the gatherer.

00:59.640 --> 01:01.850
So in this case you execute this.

01:02.210 --> 01:04.250
So this is how the result is going to be.

01:04.250 --> 01:06.450
So here decade 1940s.

01:06.490 --> 01:14.090
These are the two movies that falls under that decade 1970s 1990s to 2020 tens.

01:14.090 --> 01:18.170
So this is a concept we are going to be coding using the gatherer.

01:18.170 --> 01:19.610
So here is a code.

01:19.610 --> 01:22.730
So the code is we are doing two kind of operations.

01:22.730 --> 01:24.610
First operation is grouping them.

01:24.610 --> 01:28.170
And second operation is basically sorting them okay.

01:28.370 --> 01:31.570
So now we are going to implement the same logic using the gatherer.

01:31.610 --> 01:36.330
Let's go to the top I have a function readily available but the implementation is empty.

01:36.370 --> 01:38.210
We are going to go ahead and code them.

01:38.210 --> 01:41.450
So in this case this is a custom gatherer implementation.

01:41.450 --> 01:43.490
So first thing right.

01:43.490 --> 01:46.530
So we are going to be creating a gatherer like this.

01:46.890 --> 01:51.570
Let's take an example that we created for the custom simple custom gatherer.

01:51.570 --> 01:53.690
And the idea is going to be pretty much the same.

01:53.690 --> 01:57.650
But you need to clearly define what you want okay.

01:57.690 --> 02:03.180
So in this case the input is going to be movie and then I'm going to be maintaining a map, right.

02:03.220 --> 02:04.980
So this is what we need first.

02:05.060 --> 02:11.700
The first thing is we are going to be requiring a map of decades and the list of movies tied to that

02:11.700 --> 02:12.060
one.

02:12.060 --> 02:15.980
So let's go ahead and define that as a second argument.

02:16.180 --> 02:16.460
Okay.

02:16.500 --> 02:18.460
This is going to be the state.

02:18.460 --> 02:23.780
If you all can remember, this is the one which is going to be iterating through the movies and then

02:23.820 --> 02:26.500
continuously building this state for us.

02:26.700 --> 02:28.940
And what the output is going to be.

02:28.940 --> 02:35.020
The output is going to be a map of entries, which is going to be the output something like this.

02:35.020 --> 02:39.380
So for decade 2000, these are the movies for decade 1990s.

02:39.420 --> 02:41.180
These are the movies, something like that.

02:41.180 --> 02:41.660
Right.

02:41.660 --> 02:45.380
So I'm going to be creating a map dot entry.

02:45.620 --> 02:50.340
And this is going to be the string comma list of movie.

02:50.580 --> 02:51.020
Okay.

02:51.060 --> 02:55.380
So this is the actual gatherer that we are going to be coding actually.

02:55.980 --> 03:01.680
So the movie is a input and this is the output, and this is the internal state where we are going to

03:01.680 --> 03:06.720
be maintaining the movie decade along with the movies as a list.

03:06.760 --> 03:07.040
Okay.

03:07.080 --> 03:08.840
So in this case I'm going to give it a name.

03:09.040 --> 03:13.000
The name is going to be movie movies by decade.

03:13.040 --> 03:13.480
Okay.

03:13.520 --> 03:20.200
So this is a gatherer and I'm going to be using a new type of gatherer, which is gatherer dot of sequential,

03:20.240 --> 03:25.840
because we want to be sequentially processing these things so that we can group by decades.

03:25.840 --> 03:29.280
So in this case, the copilot is giving me all the recommendations.

03:29.280 --> 03:32.560
So I'm going to type in gatherer dot of sequential.

03:32.720 --> 03:36.000
And then the very first thing is we are going to be creating an HashMap.

03:36.000 --> 03:38.960
So this HashMap is basically the representation of this one.

03:38.960 --> 03:40.400
That's the initial state.

03:40.400 --> 03:43.400
So HashMap colon colon new okay.

03:43.560 --> 03:47.840
So this is the one where we are going to be maintaining the state okay.

03:48.040 --> 03:53.000
So the next argument is going to be the state which is a map right.

03:53.000 --> 03:54.920
So this is the one which is going to hold the state.

03:54.920 --> 03:58.930
And then the movie and followed by that we'll have the downstream.

03:58.970 --> 03:59.210
Okay.

03:59.250 --> 04:01.010
So these are the three things that we have.

04:01.170 --> 04:07.570
And if we take a look at it, the logic of this internal integrator is basically very similar to what

04:07.570 --> 04:09.010
we have over here.

04:09.050 --> 04:12.850
Whatever that we are doing here, we are exactly doing this over here.

04:12.850 --> 04:19.170
But except that this map, which is the internal state that's going to hold the decade key, and then

04:19.170 --> 04:23.970
it's going to be adding the new movie to that list, and then we are going to be returning it.

04:23.970 --> 04:24.290
True.

04:24.450 --> 04:26.890
So remember this true is very important.

04:26.930 --> 04:31.050
This is very similar to what we did in our simple custom gatherer.

04:31.050 --> 04:34.970
So now let's go down and then the next option what is the next option.

04:34.970 --> 04:40.530
So if we go and take a look at the gatherer of sequential we have the supplier which is the initializer.

04:40.530 --> 04:42.490
And next one is the integrator.

04:42.530 --> 04:44.690
The next one is a by consumer.

04:44.690 --> 04:49.770
So this is the one which is going to be performing the sorting logic for us okay.

04:49.810 --> 04:54.650
So in here once this map is ready now we are going to be accessing that map.

04:54.770 --> 04:54.970
Right.

04:55.010 --> 04:58.180
So we have the map and downstream Okay.

04:58.220 --> 05:00.740
And then for each entry in the map.

05:00.740 --> 05:06.020
So at this layer we have the decades and also the movies.

05:06.020 --> 05:08.340
That's part of the decade already computed.

05:08.380 --> 05:08.620
Right.

05:08.660 --> 05:13.980
So because this hash map as you can see it does compute dot if present, then we are going to be adding

05:13.980 --> 05:14.660
the movie.

05:14.660 --> 05:16.420
So what does compute dot if present?

05:16.420 --> 05:21.980
If you are really new to this function, you can actually ask the coding assistant what does this line

05:22.020 --> 05:22.460
do.

05:22.500 --> 05:23.300
And press enter.

05:28.180 --> 05:31.620
So this line groups movies by decade using a hash map.

05:31.620 --> 05:32.900
Here is what it does.

05:32.940 --> 05:38.140
Map dot compute if present when you when this executes looks up for the decade key in the map.

05:38.260 --> 05:42.660
If the key doesn't exist, it creates a new array list using the lambda function.

05:42.660 --> 05:45.420
Returns the existing or newly created list.

05:45.460 --> 05:50.260
Add movies adds the current movie to its list associated with that decade.

05:50.260 --> 05:57.710
So let's say in this case, if a processing movie is 1994 decade key would be 1990s if 1990s doesn't

05:57.710 --> 06:03.990
exist in the map, it creates map, dot, put and then add that list and then add some movie to that

06:03.990 --> 06:04.270
list.

06:04.310 --> 06:07.150
It does three steps in one single line.

06:07.150 --> 06:09.310
That's what the compute if present is.

06:09.350 --> 06:15.230
So now we have the map readily available with the hydrated data, with everything that's needed and

06:15.230 --> 06:16.670
grouped by the decade.

06:16.750 --> 06:17.190
Right.

06:17.230 --> 06:19.550
And I don't want to do the print here.

06:19.750 --> 06:25.230
Let's make sure we are going to be doing the map dot entry set and stream on it.

06:25.230 --> 06:27.390
And then we are going to be doing the sorting right.

06:27.390 --> 06:31.990
So we need to make sure the result is sorted by year decades.

06:32.110 --> 06:32.910
So sorted.

06:33.070 --> 06:36.270
So this is where we do the map dot entry thing.

06:36.270 --> 06:38.990
So entry dot comparing by key.

06:38.990 --> 06:42.390
So this is going to automatically sort the result in ascending order.

06:42.670 --> 06:46.270
And then we do the for each downstream dot push.

06:46.470 --> 06:51.070
Remember downstream dot push is how the data gets sent down.

06:51.070 --> 06:53.590
So this is our custom gatherer thing.

06:53.590 --> 06:57.240
We are missing our braces and then we're missing it over here.

06:57.240 --> 07:04.000
So this is the gatherer, which does the grouping and sorting in a single gatherer.

07:04.000 --> 07:09.920
It might look a little complex to start with, but as you work more on this one, you can actually get

07:09.920 --> 07:15.120
used to these concepts and start creating gatherers and identifying the patterns, whether gatherer

07:15.120 --> 07:22.200
is a right fit for your use case, or using the traditional way of using the other stream API operators.

07:22.200 --> 07:24.000
So now we have gatherer created.

07:24.000 --> 07:25.120
What is the next step?

07:25.120 --> 07:28.360
The next step is to create a stream and use this gatherer.

07:28.360 --> 07:30.640
So in this case movies dot stream.

07:30.840 --> 07:34.720
And then we're going to pass gatherer pass a gatherer over there.

07:34.760 --> 07:37.720
In this case it's going to be the movies by decade.

07:37.720 --> 07:39.920
And after that we are going to be printing this logic.

07:39.960 --> 07:40.680
So there you go.

07:40.880 --> 07:44.960
The copilot already gave me the recommendation to print it in this format.

07:44.960 --> 07:46.560
So here we have the decades.

07:46.760 --> 07:53.000
The entry is going to be the key, which is the 1940s, 1950s, 2000, 20 tens.

07:53.160 --> 07:59.300
And then we have the result printed in a way that we have the movie title followed by the release year.

07:59.420 --> 08:01.500
So now let's go ahead and execute this program.

08:01.500 --> 08:08.620
So we have implemented the gatherer dot of sequential approach of creating your own custom gatherer.

08:08.660 --> 08:10.300
Let me recap this code one more time.

08:10.380 --> 08:15.300
So here in this code the HashMap is the basically a state initializer.

08:15.500 --> 08:21.100
This is the one which is going to stream on all the movies, and then build that decade map for you.

08:21.100 --> 08:22.700
So that's what we are doing over here.

08:22.900 --> 08:26.980
Once this is done, then we are going to be sorting the result.

08:27.020 --> 08:29.340
That's what the third argument does.

08:29.380 --> 08:35.380
If we go to gatherer dot sequential, let me put this in the right so that we can compare and then understand

08:35.380 --> 08:36.620
what this code is doing.

08:36.740 --> 08:41.060
So in this case here we have the initializer which is this one.

08:41.060 --> 08:46.700
And then integrator is where we are actually writing the logic of maintaining the state using this map

08:46.700 --> 08:47.940
that we have initialized.

08:47.940 --> 08:52.860
And then we are actually building that map of grouping them by decade.

08:52.860 --> 08:55.670
But at this point the result is not sorted.

08:55.670 --> 09:01.270
That's when we are implementing the sorting logic over here to ensure that the final result is sorted,

09:01.270 --> 09:05.070
so that you can see the result in a nicely sorted manner.

09:05.190 --> 09:08.110
So with this, let's go ahead and execute this program.

09:09.470 --> 09:10.710
Let's rerun this one.

09:13.110 --> 09:15.870
So here is a custom gatherer implementation result.

09:15.950 --> 09:18.430
So the result looks good to me.

09:18.430 --> 09:23.270
And if you take a look at the traditional way of grouping that result also looks good to me.

09:23.430 --> 09:30.430
So we have successfully learned how to build your custom gatherer using gatherer of sequential.

09:30.590 --> 09:36.910
And also we learned about how to initialize a state, how to manage a state using the custom gatherer.

09:37.070 --> 09:39.550
I hope you all have a pretty good idea about how this works.

09:39.710 --> 09:44.910
And again, as I mentioned before, the same code can be completely written by your AI assistant.

09:44.950 --> 09:48.310
All you got to do is use one of the prompts in this case.

09:48.310 --> 09:51.870
For this example, the prompt is going to be prompt number nine.

09:51.910 --> 09:53.590
With this we came to the end of this lecture.

09:53.590 --> 09:54.590
Thank you for watching.
