Hi everyone.
Welcome back.
Let's now explore one of the most powerful built in gatherers, which is fold.
We use it to combine or accumulate elements in a stream while maintaining an internal state.
So when we say stateful reduction, it means the fold operation keeps track of running state.
For example, some count or concatenation as elements pass through the stream.
This is especially useful when you want to compute a single result, like a total duration of all the
movies in the stream.
Our total cost or even build a string from multiple elements.
If you have used the reduce function that's part of the streams before, this concept will feel similar,
but there is a key difference.
Reduce is a terminal operation, meaning once you call it, your stream ends and you get one final result.
But fold is an intermediate operation, meaning it happens inside the stream pipeline.
You can actually continue transforming or filtering forward.
Now let's break down the syntax.
The syntax for fold takes in a supplier and a buy function.
The supplier provides the initial state like zero for summing or an empty string for concatenation and
buy function defines how each element should update the state.
For instance.
Accumulation plus value.
The simple structure gives you complete control over how data is accumulated while the stream flows.
So in this case here we have the code example.
In this example we are calculating the total duration of all the movies.
So in this case we use a gather.
So the movie is going to be the collection.
And we are gathering all the movies in the collection.
So we have the initial state which is zero over here, and then the actual movie.
So what we are doing here is that when the first element is processed, we process the zero plus the
movie duration.
But after that we are going to be continuously getting all the movies and adding the duration to it,
and then we call the find first to get the final result.
If there are no movies in the movies collection, then we will get the value as zero.
So this is how we are gathering the duration.
Basically, in this case, we are performing a stateful operation of maintaining the state of what the
duration was.
When each and every movie is being gathered by this gather operator.
I hope the explanation is clear.
Let's go ahead and explore this in action by getting back to IntelliJ.
So I'm back in IntelliJ now.
So in this case we are going to be exploring the fold operation.
So let's go ahead and comment out this window sliding so that our console looks cleaner.
So let's go to the demonstrate fold.
So in here what I'm going to do.
So I'm going to be performing the same operation.
So in this case movies dot stream.
And we are going to be calling the gather operator here.
So gather dot fold and the fold.
You can see it takes in a supplier.
So the very first thing is we basically provide our initial state.
The initial duration is going to be zero.
And after that we can provide the bi function.
So the bi function takes in two input.
And then it's going to be giving you an output which is in this case is integer.
So in this case now we are going to be calling the find first this is going to be an optional integer.
If it's not present then we are going to be calling or else the value is going to be zero okay there
we go.
There we go.
So in this case we are doing the stream of movie.
And here this is going to be maintaining the state.
So this particular operation is the one which is going to stream through all the movies and then compute
the duration of all the movies by running this logic.
Once that is done, it's going to be calling the find first, and then it's going to return the total
duration.
So I'm going to be assigning this to a variable.
So I'm going to name this one as total duration.
There you go.
So this is going to give you the duration over there.
Now I'm going to be printing the value.
So in this case, total duration of all the movies is this minutes.
So this is good.
Let's go ahead and execute the program.
Let's go to the top and let's execute this program.
So here you can see that the total duration of all the minutes is 173 two minutes.
So you can actually do a total of all these things that will give you this result.
Okay.
Now let's explore some other example in the same fold operation.
Let's go to this one.
So what I'm going to do is I'm going to be calculating the average rating okay.
So I'm going to pretty much copy the similar code.
But I'm going to change things a little bit.
So in this case what we are going to calculate is a average rating.
And in here instead of duration what we are going to do is we are going to be calculating the rating
over here okay.
And this is going to be of type double.
That's why I see this error.
So because rating is of type double.
So in this case we can change the supplier to represent a double.
There we go.
And this is also going to be a double.
So what this is going to do this is going to give you an average rating.
Right.
This is going to give you an addition of all the ratings.
So what do we need to do.
So I'm going to do a division of movie dot size.
So this is going to give you the average rating of all the movies that we have in our collection.
So let's go ahead and print this value also.
So average rating of all the movies is string dot format.
And this is the average rating okay.
So now the next use case we can code multiple use cases based on the fold operation.
So now I want to just concatenate all the titles okay.
So what I'm going to do I'm going to do something similar.
But in this case it's not going to be the rating here.
It's going to be the movie title okay.
So in this case movie title.
And this is going to be an empty string as a initial state okay.
So I'm going to change this one to a variable with the name accumulator.
So accumulator.
So there is a little logic I need to write actually.
So if the value is empty.
If the accumulator value is empty, which is going to be empty for the very first time, right?
If it's empty, I'm going to be adding the movie title.
Otherwise I'm going to be adding the accumulator plus the movie title.
Okay.
So this is going to give me the movie titles as comma separated values.
So in this case this is also going to be empty string.
We don't want the division over there.
So this is going to be all titles okay.
All titles.
And another thing about our copilot is that let's say this code logic is very complex for you to understand.
So what I'm going to do is I'm going to highlight this code and this particular inline chat will open.
So let's open that one.
And then you can ask explain this code to me.
Right.
When you do that it's going to give you an explanation of what that logic is so that you will be able
to understand what it actually does.
So here.
Okay.
So here if you take a look at it, the fold operations collapses the entire stream of movies into one
result.
By this is the initial value, which is the empty string.
And then here we are actually performing the accumulator.
So bills a comma separated list.
So the terminal operation produces exactly one output regardless of the input size.
It maintains the accumulated straight across this elements.
And in this case if you take a look at it let's say if you have three movies it transforms it to this
particular single string value.
This is different from scan which would emit intermediate results.
So here there is no emission of intermediate result.
It folds the overall list of elements into one single output and then emits that value.
Okay.
So what we have done is we have coded the average rating and all titles.
Let me go ahead and print that one also.
So in this case over there all titles okay.
So now let's go ahead and execute this particular function.
This is going to print all the three functionalities that we have coded using the fold operator.
So in this case, the total duration of all the movies is this, and the average rating is 8.82 stars
and all movie titles, you can see that it's all comma separated nicely.
So if you go to the top, let me explore the code one more time.
Let's go to the fold operation.
So this is a total duration of all the movies where we are simply performing summation of all the durations.
So here we are also performing the same summation concept, but we divided the whole result by the movies
dot size to get the average rating.
And then if you go here.
So here we are performing the string concatenation operation where we are taking each title and then
having them separated by comma.
That's what this one is.
So this is where the folding operation is really handy.
If you want to perform an intermediate operation with state and then produce a final result, then fold
is an excellent option for that.
This marks the end of this lecture.
Thank you for watching.