WEBVTT

00:00.400 --> 00:01.000
Hi everyone.

00:01.000 --> 00:02.290
Welcome back to this tutorial.

00:02.290 --> 00:08.500
In this tutorial we will code the classic stream map Reduce pattern using the streams API.

00:09.790 --> 00:10.330
The first step.

00:10.330 --> 00:15.670
What I'm going to do, I'm going to create a class called Dream Map.

00:17.350 --> 00:22.540
Reduce example class, and then I'm going to make this class executable.

00:24.480 --> 00:30.450
So in order to explain this use case, what I'm going to do, I'm going to add one additional parameter

00:30.450 --> 00:33.930
for our student object in the student class.

00:33.930 --> 00:41.610
What I'm going to do, I'm going to add a new attribute called private int notebooks.

00:41.760 --> 00:48.660
So this attribute, meaning this property represents how many notebooks each in every individual meaning

00:48.660 --> 00:51.030
each and every student has.

00:51.450 --> 00:54.690
I'm going to generate a constructor with all these values.

00:54.720 --> 00:57.060
I'm going to press Command N in Mac.

00:57.090 --> 01:03.420
In the case of Windows, you can right click and then you get this option right click and then click

01:03.420 --> 01:03.930
on Generate.

01:03.930 --> 01:05.340
It will give you this option.

01:05.610 --> 01:08.580
I'm going to click on constructor.

01:08.730 --> 01:14.160
Basically, I'm going to add notebooks also as part of the constructor so that it will be easy to build

01:14.160 --> 01:15.420
objects out of it.

01:15.450 --> 01:16.200
There you go.

01:16.230 --> 01:18.600
I have the notebooks that got added here.

01:19.560 --> 01:23.690
The next step is I'm going to update the student database with those values.

01:23.720 --> 01:25.790
Right now, we just have this.

01:25.820 --> 01:29.000
What I'm going to do, I'm going to add some random numbers to it.

01:29.240 --> 01:30.140
11.

01:31.780 --> 01:32.680
And then.

01:32.950 --> 01:33.640
Well.

01:35.490 --> 01:37.620
Here for this student, I can add ten.

01:38.600 --> 01:42.500
But the student again had zero nine for the student.

01:42.500 --> 01:43.640
I can add like.

01:47.360 --> 01:51.470
And for this student, we are going to add, I believe I should not.

01:52.370 --> 01:53.720
Zero for the student.

01:53.720 --> 01:54.800
It's going to be 14.

01:54.980 --> 02:01.190
So all these numbers represent how many number of notebooks each and every student has.

02:01.430 --> 02:09.050
The use case that we are going to code here is that what is the total number of notebooks that this

02:09.050 --> 02:10.300
whole collection has?

02:10.310 --> 02:17.060
Meaning What is the total number of perform the summation of all the notebooks that each and every student

02:17.060 --> 02:17.370
has.

02:17.390 --> 02:22.160
That's what we are going to code as part of this use case.

02:22.190 --> 02:25.550
Now let me put this in presentation mode.

02:27.630 --> 02:33.240
I'm going to create a method called private static int.

02:34.180 --> 02:35.240
Number of.

02:36.050 --> 02:36.940
Notebooks.

02:36.950 --> 02:38.330
That's what we are going to do.

02:38.630 --> 02:44.270
The next step is I'm going to get all the students from the student database, student database, dot,

02:44.300 --> 02:45.650
get all students.

02:45.650 --> 02:46.630
That's good.

02:46.640 --> 02:49.490
And the next step is we are going to map.

02:49.610 --> 02:51.920
That's the next step which we are going to do.

02:51.920 --> 02:55.070
But before that we need to call the stream method.

02:56.610 --> 02:57.150
Dream.

02:57.390 --> 02:58.140
Here we go.

02:59.220 --> 03:02.310
We have to map all the notebooks, right?

03:02.310 --> 03:03.890
Meaning we need to get this number.

03:03.900 --> 03:07.110
Then only we will be able to perform the summation on it.

03:08.480 --> 03:12.710
So what we do for that is that we'll call the map method.

03:12.710 --> 03:13.940
And then.

03:15.420 --> 03:17.790
So this stream is going to return us.

03:18.820 --> 03:19.870
Dream of student.

03:21.830 --> 03:26.600
And the next step is we are going to perform Student Colon.

03:26.600 --> 03:26.870
Colon.

03:26.870 --> 03:32.810
We are going to use the method reference syntax, and then we are trying to access the number of notebooks.

03:32.810 --> 03:36.830
And one more interesting thing is we did not generate the getter and setter method.

03:36.830 --> 03:42.170
That's the reason why we are not able to access it and it is of scope private.

03:42.200 --> 03:46.700
Now what I'm going to do, I'm going to generate getters and setters.

03:48.900 --> 03:49.740
There you go.

03:49.890 --> 03:51.520
The getters and setters are there.

03:51.540 --> 03:54.300
Now let's try to use that.

03:55.960 --> 03:58.450
Let's see whether do we have that recommendation for that?

03:58.450 --> 04:00.580
Get number of notebooks.

04:00.580 --> 04:01.110
There we go.

04:01.120 --> 04:07.270
We have the get number of notebooks, which is going to give you the access to the notebooks value.

04:07.780 --> 04:12.250
So what this is going to do, this is going to create the.

04:13.300 --> 04:16.090
Dream of integer.

04:16.330 --> 04:17.680
That's what it is going to do.

04:17.710 --> 04:18.850
What is the next step?

04:18.850 --> 04:22.630
So we are going to code the classic stream MapReduce.

04:22.630 --> 04:26.770
Now we start the stream and then we perform the map operation.

04:26.770 --> 04:28.330
Next is a reduce operation.

04:28.330 --> 04:34.570
The reduce operation is going to perform the summation of all the values in the stream.

04:34.600 --> 04:35.920
How can we achieve that?

04:35.950 --> 04:39.880
The way to do it is by passing zero.

04:39.880 --> 04:41.710
Let's keep that one as an initial value.

04:41.740 --> 04:47.140
Here, we can use the zero as an initial value because we are performing the summation, right?

04:47.140 --> 04:53.470
So summation, it is not going to change the result to zero because it is going to add zero plus whatever

04:53.470 --> 04:55.690
value that the stream is going to pass.

04:55.690 --> 05:00.280
And the next step is we all know it's an accumulator we are going to pass.

05:00.850 --> 05:07.150
It is going to take two values A and B and then perform A plus B.

05:08.410 --> 05:08.920
Right?

05:09.140 --> 05:09.700
Yep.

05:09.700 --> 05:10.660
That is good.

05:10.690 --> 05:14.020
Now, this is going to give us the result.

05:14.960 --> 05:16.940
And number of.

05:18.750 --> 05:19.500
Notebooks.

05:21.520 --> 05:21.970
Great.

05:21.970 --> 05:25.120
And then what we will do, we will return this.

05:26.260 --> 05:27.220
The number of notebooks.

05:28.090 --> 05:31.540
The next step is let's print this value, let's call this method and then check.

05:34.240 --> 05:35.590
A number of notebooks.

05:36.960 --> 05:37.950
Going to be.

05:41.730 --> 05:43.050
And call this method.

05:44.130 --> 05:45.830
Run this example and then check.

05:48.970 --> 05:49.840
There you go.

05:49.870 --> 05:51.040
What is the result?

05:51.070 --> 05:53.350
The result it gave is 71.

05:53.380 --> 05:57.850
Now I'm going to show you an alternative approach to perform this.

05:58.980 --> 06:03.420
If you remember, we have this integer class, which is part of the.

06:04.400 --> 06:04.940
Java.

06:06.660 --> 06:09.090
Itself, which comes with the Java Library itself.

06:09.570 --> 06:11.250
Integer, Colon.

06:11.250 --> 06:11.910
Colon.

06:12.720 --> 06:13.130
There you go.

06:13.140 --> 06:14.670
You have this max method.

06:14.680 --> 06:16.200
You have this sum method.

06:16.440 --> 06:19.770
You can use the sum method.

06:19.920 --> 06:22.820
This is going to perform the summation for you.

06:22.830 --> 06:26.580
So this is as similar to this operation.

06:27.620 --> 06:28.520
Let's run this and check.

06:28.520 --> 06:31.660
So the result that it returned previously was 71.

06:31.670 --> 06:34.550
Now let's run and check whether is it returning the same result.

06:36.790 --> 06:37.450
There you go.

06:37.460 --> 06:39.880
It printed the result as 71.

06:40.150 --> 06:43.780
So this is one another way of performing the summation.

06:43.780 --> 06:51.010
But if you take a look at the integer class, it doesn't have anything like multiplication.

06:51.880 --> 06:52.330
Right.

06:52.330 --> 07:00.490
So for those kind of use cases, we still have to implement the reduce function by our own customized

07:00.490 --> 07:01.060
pattern.

07:02.050 --> 07:05.170
Okay, So now let's go ahead and enhance this use case.

07:05.170 --> 07:13.690
Let's say I want one only the students who are in the third or fourth grade to have I mean, I want

07:13.690 --> 07:20.350
the students number of notebooks collection for the students who have the grade level as three and above.

07:20.440 --> 07:24.430
So in order to perform that, what I can do is I can add a filter.

07:24.430 --> 07:26.140
So it's so handy, right?

07:26.530 --> 07:27.490
So student.

07:28.180 --> 07:31.720
Student dot get grade level.

07:34.550 --> 07:37.040
Its grade level greater than or equal to three.

07:37.040 --> 07:41.600
So this is going to give you all the students whose grade level is greater than or equal to three,

07:41.600 --> 07:43.490
and then that is going to perform.

07:43.490 --> 07:50.330
So how many number of notebooks the students who are present, who are sitting in the grade three and

07:50.330 --> 07:53.810
four, It is going to give you the summation of all those notebooks.

07:54.850 --> 07:55.170
There you go.

07:55.210 --> 07:56.400
The number is 48.

07:56.820 --> 07:58.980
Let's say you want to add one more filter.

07:59.160 --> 08:05.010
So I want to just perform Student whose gender is female.

08:05.700 --> 08:06.660
Dot get.

08:07.960 --> 08:11.080
Gender dot equals.

08:13.600 --> 08:13.930
Remove.

08:14.380 --> 08:19.280
If you go to the students database class, it has the gender as male and female.

08:19.300 --> 08:22.840
I just want to know how many notebooks.

08:24.070 --> 08:29.980
Of the students who were in a grade level greater than or equal to three and their gender is female.

08:31.430 --> 08:34.610
On this example, we take a look at it.

08:34.940 --> 08:36.440
Result is 24.

08:36.890 --> 08:38.240
Result is 25.

08:39.040 --> 08:47.260
So this is one famous pattern of implementing the map filter and reduce example.

08:48.640 --> 08:48.940
With this.

08:48.940 --> 08:50.620
We came to the end of this tutorial.

08:50.650 --> 08:51.820
Thank you for watching.
