Hi everyone.
Welcome back to this tutorial.
This is a part two of the reduce function.
Now I'm going to build a use case exploring the reduce function.
We are going to leverage the student database and then we are going to find out how to reduce the student
who has the highest GPA in this whole collection that we have.
So I'm going to create a method called public static.
Optional of student.
So this is going to give you get the.
Highest GPA.
For this.
I'm going to put this in presentation mode so the code will be clear.
Mission mode.
So in here, what we are going to do, the first step is we are going to.
All the student database dot, get all students, go back to that method and then see.
So these are the six students we have as part of this collection in here, the highest grade student
out of all, the highest GPA student.
Out of all these six students is Emily, who has a GPA of 4.0.
Our goal is to how to get to that value and how to return that using the reduce operation or reduce
function.
So the first step is we are going to create the stream and then we are going to use the reduce method.
This method is going to take in two input because it is a binary operator, as you see.
And then I'm going to assign two values S1 and S2.
These are the student one and student two And what we are going to do is that we're going to compare.
S1 dot get GPA.
I'm going to open a remove this, open the curly braces and close the curly braces.
If the.
S1 dot.
EPA.
Don't need this, Right.
So you don't need this here.
Impression dot get GPA is greater than S2.
Dot get GPA.
Then return as one.
Else.
So this is the functionality which is going to give us the desired result.
What I'm going to do, if you take a look at it, it is returning an optional because I do not have
any default value here.
I want I don't want to pass any default value.
I'm just expecting the result as a optional student.
So I'm going to go back here.
And I'm going to call.
I don't have to pass any inputs because this method has a call.
You get all the students, I'm going to return this.
So in here is going to be optional of student.
And then this is going to be student optional.
If I do.
God is present.
And then I'll perform the student optional dot get.
Run this example.
It is going to give me Emily as a result.
As you see, it gave me Emily as a result.
So how is this determining the result?
That might be the question which is running in your mind.
So the reduce operation is going to get the students one by one, right?
Students one by one.
So it is here we have the condition.
This condition is a one which is a key factor here that determines which student has the highest GPA.
And then it compares that with each and every value that is being parsed out of the stream.
And then it maintains the highest, the student with the highest grade, and that student will be returned
towards the end.
That's what is happening behind the scenes.
So this logic looks more clumsy to me.
How can we rewrite it in such a way that it looks more readable?
So I'm gonna.
Does.
What I'm going to do is I'm going to do S1 dot get GPA is greater than S2 dot get GPA.
So I'm going to use a ternary operator question mark.
As one.
Are.
This looks more readable.
Let's run this and then check.
Is it returning the same result?
There you go.
It returned the same result.
So this is one of the use case which is very popular while using the reduce function.
The reduce functions idea is to get the stream and then try to reduce that stream to one single value
that you are looking for.
But thus we came to the end of this tutorial.
Thank you for watching.