Hi everyone.
Welcome back.
In this lecture we are going to be exploring an advanced custom gatherer using another example.
So in this case I'm going to comment out this one.
So in this section I'm going to uncomment this code here I'm going to be showing how to demonstrate
a traditional grouping.
So in this case if you go to this particular functionality what this particular functionality does is
that we are going to be grouping the movies by decade.
So in this case, based on the release year, we are going to be grouping the movies by decades.
So in this case we stream on the movies and then we do the grouping by, we pass in the movie.
And the key is going to be like 1940s, 1950s, 1960s, something like that.
So we are going to be grouping the movie by decade, and then we are going to be sorting the decades
for consistent output.
So that's what we are going to do.
Let me execute this program and then show you what the result is.
And then we are going to be coding the same logic using the gatherer.
So in this case you execute this.
So this is how the result is going to be.
So here decade 1940s.
These are the two movies that falls under that decade 1970s 1990s to 2020 tens.
So this is a concept we are going to be coding using the gatherer.
So here is a code.
So the code is we are doing two kind of operations.
First operation is grouping them.
And second operation is basically sorting them okay.
So now we are going to implement the same logic using the gatherer.
Let's go to the top I have a function readily available but the implementation is empty.
We are going to go ahead and code them.
So in this case this is a custom gatherer implementation.
So first thing right.
So we are going to be creating a gatherer like this.
Let's take an example that we created for the custom simple custom gatherer.
And the idea is going to be pretty much the same.
But you need to clearly define what you want okay.
So in this case the input is going to be movie and then I'm going to be maintaining a map, right.
So this is what we need first.
The first thing is we are going to be requiring a map of decades and the list of movies tied to that
one.
So let's go ahead and define that as a second argument.
Okay.
This is going to be the state.
If you all can remember, this is the one which is going to be iterating through the movies and then
continuously building this state for us.
And what the output is going to be.
The output is going to be a map of entries, which is going to be the output something like this.
So for decade 2000, these are the movies for decade 1990s.
These are the movies, something like that.
Right.
So I'm going to be creating a map dot entry.
And this is going to be the string comma list of movie.
Okay.
So this is the actual gatherer that we are going to be coding actually.
So the movie is a input and this is the output, and this is the internal state where we are going to
be maintaining the movie decade along with the movies as a list.
Okay.
So in this case I'm going to give it a name.
The name is going to be movie movies by decade.
Okay.
So this is a gatherer and I'm going to be using a new type of gatherer, which is gatherer dot of sequential,
because we want to be sequentially processing these things so that we can group by decades.
So in this case, the copilot is giving me all the recommendations.
So I'm going to type in gatherer dot of sequential.
And then the very first thing is we are going to be creating an HashMap.
So this HashMap is basically the representation of this one.
That's the initial state.
So HashMap colon colon new okay.
So this is the one where we are going to be maintaining the state okay.
So the next argument is going to be the state which is a map right.
So this is the one which is going to hold the state.
And then the movie and followed by that we'll have the downstream.
Okay.
So these are the three things that we have.
And if we take a look at it, the logic of this internal integrator is basically very similar to what
we have over here.
Whatever that we are doing here, we are exactly doing this over here.
But except that this map, which is the internal state that's going to hold the decade key, and then
it's going to be adding the new movie to that list, and then we are going to be returning it.
True.
So remember this true is very important.
This is very similar to what we did in our simple custom gatherer.
So now let's go down and then the next option what is the next option.
So if we go and take a look at the gatherer of sequential we have the supplier which is the initializer.
And next one is the integrator.
The next one is a by consumer.
So this is the one which is going to be performing the sorting logic for us okay.
So in here once this map is ready now we are going to be accessing that map.
Right.
So we have the map and downstream Okay.
And then for each entry in the map.
So at this layer we have the decades and also the movies.
That's part of the decade already computed.
Right.
So because this hash map as you can see it does compute dot if present, then we are going to be adding
the movie.
So what does compute dot if present?
If you are really new to this function, you can actually ask the coding assistant what does this line
do.
And press enter.
So this line groups movies by decade using a hash map.
Here is what it does.
Map dot compute if present when you when this executes looks up for the decade key in the map.
If the key doesn't exist, it creates a new array list using the lambda function.
Returns the existing or newly created list.
Add movies adds the current movie to its list associated with that decade.
So let's say in this case, if a processing movie is 1994 decade key would be 1990s if 1990s doesn't
exist in the map, it creates map, dot, put and then add that list and then add some movie to that
list.
It does three steps in one single line.
That's what the compute if present is.
So now we have the map readily available with the hydrated data, with everything that's needed and
grouped by the decade.
Right.
And I don't want to do the print here.
Let's make sure we are going to be doing the map dot entry set and stream on it.
And then we are going to be doing the sorting right.
So we need to make sure the result is sorted by year decades.
So sorted.
So this is where we do the map dot entry thing.
So entry dot comparing by key.
So this is going to automatically sort the result in ascending order.
And then we do the for each downstream dot push.
Remember downstream dot push is how the data gets sent down.
So this is our custom gatherer thing.
We are missing our braces and then we're missing it over here.
So this is the gatherer, which does the grouping and sorting in a single gatherer.
It might look a little complex to start with, but as you work more on this one, you can actually get
used to these concepts and start creating gatherers and identifying the patterns, whether gatherer
is a right fit for your use case, or using the traditional way of using the other stream API operators.
So now we have gatherer created.
What is the next step?
The next step is to create a stream and use this gatherer.
So in this case movies dot stream.
And then we're going to pass gatherer pass a gatherer over there.
In this case it's going to be the movies by decade.
And after that we are going to be printing this logic.
So there you go.
The copilot already gave me the recommendation to print it in this format.
So here we have the decades.
The entry is going to be the key, which is the 1940s, 1950s, 2000, 20 tens.
And then we have the result printed in a way that we have the movie title followed by the release year.
So now let's go ahead and execute this program.
So we have implemented the gatherer dot of sequential approach of creating your own custom gatherer.
Let me recap this code one more time.
So here in this code the HashMap is the basically a state initializer.
This is the one which is going to stream on all the movies, and then build that decade map for you.
So that's what we are doing over here.
Once this is done, then we are going to be sorting the result.
That's what the third argument does.
If we go to gatherer dot sequential, let me put this in the right so that we can compare and then understand
what this code is doing.
So in this case here we have the initializer which is this one.
And then integrator is where we are actually writing the logic of maintaining the state using this map
that we have initialized.
And then we are actually building that map of grouping them by decade.
But at this point the result is not sorted.
That's when we are implementing the sorting logic over here to ensure that the final result is sorted,
so that you can see the result in a nicely sorted manner.
So with this, let's go ahead and execute this program.
Let's rerun this one.
So here is a custom gatherer implementation result.
So the result looks good to me.
And if you take a look at the traditional way of grouping that result also looks good to me.
So we have successfully learned how to build your custom gatherer using gatherer of sequential.
And also we learned about how to initialize a state, how to manage a state using the custom gatherer.
I hope you all have a pretty good idea about how this works.
And again, as I mentioned before, the same code can be completely written by your AI assistant.
All you got to do is use one of the prompts in this case.
For this example, the prompt is going to be prompt number nine.
With this we came to the end of this lecture.
Thank you for watching.