WEBVTT

00:01.040 --> 00:01.640
Hi everyone.

00:01.640 --> 00:02.980
Welcome back to this tutorial.

00:02.990 --> 00:07.130
This is a part two of the reduce function.

00:07.130 --> 00:12.200
Now I'm going to build a use case exploring the reduce function.

00:12.200 --> 00:20.000
We are going to leverage the student database and then we are going to find out how to reduce the student

00:20.030 --> 00:24.830
who has the highest GPA in this whole collection that we have.

00:24.950 --> 00:30.140
So I'm going to create a method called public static.

00:31.390 --> 00:33.070
Optional of student.

00:35.990 --> 00:38.630
So this is going to give you get the.

00:40.080 --> 00:41.670
Highest GPA.

00:47.160 --> 00:48.150
For this.

00:48.660 --> 00:51.870
I'm going to put this in presentation mode so the code will be clear.

00:54.430 --> 00:55.210
Mission mode.

00:55.360 --> 00:59.110
So in here, what we are going to do, the first step is we are going to.

01:00.180 --> 01:06.690
All the student database dot, get all students, go back to that method and then see.

01:07.260 --> 01:13.410
So these are the six students we have as part of this collection in here, the highest grade student

01:13.410 --> 01:15.690
out of all, the highest GPA student.

01:15.690 --> 01:21.390
Out of all these six students is Emily, who has a GPA of 4.0.

01:21.480 --> 01:30.330
Our goal is to how to get to that value and how to return that using the reduce operation or reduce

01:30.330 --> 01:31.020
function.

01:33.370 --> 01:40.690
So the first step is we are going to create the stream and then we are going to use the reduce method.

01:41.320 --> 01:48.730
This method is going to take in two input because it is a binary operator, as you see.

01:48.850 --> 01:53.770
And then I'm going to assign two values S1 and S2.

01:54.370 --> 02:01.660
These are the student one and student two And what we are going to do is that we're going to compare.

02:03.870 --> 02:06.610
S1 dot get GPA.

02:08.620 --> 02:15.210
I'm going to open a remove this, open the curly braces and close the curly braces.

02:16.690 --> 02:17.320
If the.

02:21.090 --> 02:22.350
S1 dot.

02:25.980 --> 02:26.460
EPA.

02:29.670 --> 02:30.600
Don't need this, Right.

02:30.600 --> 02:32.640
So you don't need this here.

02:33.840 --> 02:38.730
Impression dot get GPA is greater than S2.

02:38.760 --> 02:40.080
Dot get GPA.

02:42.820 --> 02:45.280
Then return as one.

02:46.780 --> 02:47.470
Else.

02:52.870 --> 02:58.420
So this is the functionality which is going to give us the desired result.

02:58.450 --> 03:03.760
What I'm going to do, if you take a look at it, it is returning an optional because I do not have

03:03.760 --> 03:05.270
any default value here.

03:05.290 --> 03:07.730
I want I don't want to pass any default value.

03:07.750 --> 03:11.590
I'm just expecting the result as a optional student.

03:11.770 --> 03:13.840
So I'm going to go back here.

03:14.990 --> 03:16.310
And I'm going to call.

03:17.040 --> 03:20.690
I don't have to pass any inputs because this method has a call.

03:22.070 --> 03:24.500
You get all the students, I'm going to return this.

03:25.760 --> 03:30.980
So in here is going to be optional of student.

03:32.220 --> 03:35.460
And then this is going to be student optional.

03:37.070 --> 03:38.450
If I do.

03:39.710 --> 03:41.000
God is present.

03:42.060 --> 03:47.880
And then I'll perform the student optional dot get.

03:49.590 --> 03:50.520
Run this example.

03:50.520 --> 03:53.190
It is going to give me Emily as a result.

03:53.190 --> 03:56.340
As you see, it gave me Emily as a result.

03:56.620 --> 03:58.590
So how is this determining the result?

03:58.590 --> 04:00.780
That might be the question which is running in your mind.

04:00.810 --> 04:05.520
So the reduce operation is going to get the students one by one, right?

04:06.150 --> 04:08.400
Students one by one.

04:08.610 --> 04:11.490
So it is here we have the condition.

04:11.490 --> 04:18.780
This condition is a one which is a key factor here that determines which student has the highest GPA.

04:19.050 --> 04:24.690
And then it compares that with each and every value that is being parsed out of the stream.

04:24.690 --> 04:31.620
And then it maintains the highest, the student with the highest grade, and that student will be returned

04:31.620 --> 04:33.330
towards the end.

04:35.100 --> 04:36.770
That's what is happening behind the scenes.

04:36.780 --> 04:40.100
So this logic looks more clumsy to me.

04:40.110 --> 04:43.650
How can we rewrite it in such a way that it looks more readable?

04:43.910 --> 04:44.970
So I'm gonna.

04:46.370 --> 04:46.690
Does.

04:47.090 --> 04:56.690
What I'm going to do is I'm going to do S1 dot get GPA is greater than S2 dot get GPA.

04:57.520 --> 05:00.950
So I'm going to use a ternary operator question mark.

05:01.860 --> 05:02.520
As one.

05:03.550 --> 05:03.970
Are.

05:05.530 --> 05:06.980
This looks more readable.

05:07.000 --> 05:08.920
Let's run this and then check.

05:09.400 --> 05:11.260
Is it returning the same result?

05:12.040 --> 05:12.600
There you go.

05:12.610 --> 05:14.020
It returned the same result.

05:14.800 --> 05:21.130
So this is one of the use case which is very popular while using the reduce function.

05:21.160 --> 05:29.680
The reduce functions idea is to get the stream and then try to reduce that stream to one single value

05:29.680 --> 05:31.060
that you are looking for.

05:32.620 --> 05:35.440
But thus we came to the end of this tutorial.

05:35.800 --> 05:36.970
Thank you for watching.
