Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code the classic stream map Reduce pattern using the streams API.
The first step.
What I'm going to do, I'm going to create a class called Dream Map.
Reduce example class, and then I'm going to make this class executable.
So in order to explain this use case, what I'm going to do, I'm going to add one additional parameter
for our student object in the student class.
What I'm going to do, I'm going to add a new attribute called private int notebooks.
So this attribute, meaning this property represents how many notebooks each in every individual meaning
each and every student has.
I'm going to generate a constructor with all these values.
I'm going to press Command N in Mac.
In the case of Windows, you can right click and then you get this option right click and then click
on Generate.
It will give you this option.
I'm going to click on constructor.
Basically, I'm going to add notebooks also as part of the constructor so that it will be easy to build
objects out of it.
There you go.
I have the notebooks that got added here.
The next step is I'm going to update the student database with those values.
Right now, we just have this.
What I'm going to do, I'm going to add some random numbers to it.
11.
And then.
Well.
Here for this student, I can add ten.
But the student again had zero nine for the student.
I can add like.
And for this student, we are going to add, I believe I should not.
Zero for the student.
It's going to be 14.
So all these numbers represent how many number of notebooks each and every student has.
The use case that we are going to code here is that what is the total number of notebooks that this
whole collection has?
Meaning What is the total number of perform the summation of all the notebooks that each and every student
has.
That's what we are going to code as part of this use case.
Now let me put this in presentation mode.
I'm going to create a method called private static int.
Number of.
Notebooks.
That's what we are going to do.
The next step is I'm going to get all the students from the student database, student database, dot,
get all students.
That's good.
And the next step is we are going to map.
That's the next step which we are going to do.
But before that we need to call the stream method.
Dream.
Here we go.
We have to map all the notebooks, right?
Meaning we need to get this number.
Then only we will be able to perform the summation on it.
So what we do for that is that we'll call the map method.
And then.
So this stream is going to return us.
Dream of student.
And the next step is we are going to perform Student Colon.
Colon.
We are going to use the method reference syntax, and then we are trying to access the number of notebooks.
And one more interesting thing is we did not generate the getter and setter method.
That's the reason why we are not able to access it and it is of scope private.
Now what I'm going to do, I'm going to generate getters and setters.
There you go.
The getters and setters are there.
Now let's try to use that.
Let's see whether do we have that recommendation for that?
Get number of notebooks.
There we go.
We have the get number of notebooks, which is going to give you the access to the notebooks value.
So what this is going to do, this is going to create the.
Dream of integer.
That's what it is going to do.
What is the next step?
So we are going to code the classic stream MapReduce.
Now we start the stream and then we perform the map operation.
Next is a reduce operation.
The reduce operation is going to perform the summation of all the values in the stream.
How can we achieve that?
The way to do it is by passing zero.
Let's keep that one as an initial value.
Here, we can use the zero as an initial value because we are performing the summation, right?
So summation, it is not going to change the result to zero because it is going to add zero plus whatever
value that the stream is going to pass.
And the next step is we all know it's an accumulator we are going to pass.
It is going to take two values A and B and then perform A plus B.
Right?
Yep.
That is good.
Now, this is going to give us the result.
And number of.
Notebooks.
Great.
And then what we will do, we will return this.
The number of notebooks.
The next step is let's print this value, let's call this method and then check.
A number of notebooks.
Going to be.
And call this method.
Run this example and then check.
There you go.
What is the result?
The result it gave is 71.
Now I'm going to show you an alternative approach to perform this.
If you remember, we have this integer class, which is part of the.
Java.
Itself, which comes with the Java Library itself.
Integer, Colon.
Colon.
There you go.
You have this max method.
You have this sum method.
You can use the sum method.
This is going to perform the summation for you.
So this is as similar to this operation.
Let's run this and check.
So the result that it returned previously was 71.
Now let's run and check whether is it returning the same result.
There you go.
It printed the result as 71.
So this is one another way of performing the summation.
But if you take a look at the integer class, it doesn't have anything like multiplication.
Right.
So for those kind of use cases, we still have to implement the reduce function by our own customized
pattern.
Okay, So now let's go ahead and enhance this use case.
Let's say I want one only the students who are in the third or fourth grade to have I mean, I want
the students number of notebooks collection for the students who have the grade level as three and above.
So in order to perform that, what I can do is I can add a filter.
So it's so handy, right?
So student.
Student dot get grade level.
Its grade level greater than or equal to three.
So this is going to give you all the students whose grade level is greater than or equal to three,
and then that is going to perform.
So how many number of notebooks the students who are present, who are sitting in the grade three and
four, It is going to give you the summation of all those notebooks.
There you go.
The number is 48.
Let's say you want to add one more filter.
So I want to just perform Student whose gender is female.
Dot get.
Gender dot equals.
Remove.
If you go to the students database class, it has the gender as male and female.
I just want to know how many notebooks.
Of the students who were in a grade level greater than or equal to three and their gender is female.
On this example, we take a look at it.
Result is 24.
Result is 25.
So this is one famous pattern of implementing the map filter and reduce example.
With this.
We came to the end of this tutorial.
Thank you for watching.