WEBVTT

00:00.850 --> 00:01.390
Hi everyone.

00:01.390 --> 00:02.650
Welcome back to this tutorial.

00:02.650 --> 00:08.830
In this tutorial, we'll talk about the reduce function which is part of the streams API.

00:08.950 --> 00:11.500
Reduce is a terminal operation.

00:11.710 --> 00:16.780
We have seen one another terminal operation in the previous tutorials, which is the collect.

00:16.780 --> 00:22.240
This is the second terminal operation which we are going to discuss as part of this tutorial.

00:22.540 --> 00:29.530
So this is basically used to reduce the contents of the stream to a single value.

00:29.620 --> 00:31.060
That's what it is going to do.

00:31.060 --> 00:36.520
Let's say you want to find a sum of all the elements in the stream or perform multiplication of all

00:36.520 --> 00:42.610
the elements in the stream, or you want to find out who is the highest grade student out of the student

00:42.610 --> 00:43.360
database.

00:43.390 --> 00:48.160
In those kind of scenarios, you can make use of the reduce function.

00:48.160 --> 00:51.790
So the reduce function takes in two parameters as an input.

00:51.790 --> 00:55.960
The first parameter is the default or the initial value.

00:55.960 --> 00:58.120
And the second parameter is the binary.

00:58.120 --> 00:59.110
Operator.

00:59.140 --> 01:05.960
A quick summary of the binary operator is that binary operator is like a function which takes two inputs

01:05.960 --> 01:10.340
of the same type and returns an output of the same type.

01:10.550 --> 01:12.020
Let's go ahead and code this one.

01:13.840 --> 01:19.420
So have the previous example here want to move away from the presentation mode?

01:20.850 --> 01:24.190
Was this what I'm going to do for the purpose of this tutorial?

01:24.210 --> 01:29.010
I'm going to create a class called Stream Reduce.

01:31.190 --> 01:33.980
Dream, reduce example.

01:34.520 --> 01:38.990
And then I'm going to make this class executable by adding public static void main.

01:39.440 --> 01:40.040
There we go.

01:40.100 --> 01:44.750
Now what I'm going to do, I'm going to create a list of numbers.

01:45.350 --> 01:46.280
List of.

01:47.120 --> 01:48.200
Integers.

01:51.730 --> 01:55.570
And then this integer list is going to have.

01:57.360 --> 01:58.920
Restart as list.

01:58.920 --> 02:01.260
I'm going to add like four numbers in it.

02:01.470 --> 02:04.020
135, seven.

02:05.100 --> 02:07.020
Let me put this in presentation mode.

02:09.950 --> 02:17.250
So what I'm looking for is that I want the multiplication of all the numbers as a result.

02:17.270 --> 02:18.530
That's what I'm looking for.

02:18.560 --> 02:21.800
So how can we achieve that using the reduce method?

02:21.930 --> 02:28.460
I'm going to introduce a method called public static int.

02:31.050 --> 02:31.710
Perform.

02:32.860 --> 02:34.000
Multiplication.

02:35.120 --> 02:37.910
And then pass these numbers as an input.

02:38.000 --> 02:40.670
I'm going to pass this list of.

02:41.440 --> 02:42.370
Integer.

02:44.210 --> 02:45.320
An integer list.

02:46.340 --> 02:50.930
You're going to do integer list.

02:51.700 --> 02:53.020
Dot stream.

02:54.470 --> 02:59.650
With the method that we are going to, the function that we are going to look is the reduce.

02:59.680 --> 03:04.300
If you take a look at the reduce, it has many different variations.

03:04.300 --> 03:12.340
The one which we are going to look for is this one which has the identity and the by function, right?

03:12.340 --> 03:18.490
So what I'm going to do, I'm going to use this, the first parameter is a default value or the initial

03:18.490 --> 03:18.820
value.

03:18.820 --> 03:21.040
I want the initial value to be one.

03:21.040 --> 03:24.430
And then it is a by function.

03:24.880 --> 03:27.220
Basically, if you take a look at it, it is an accumulator.

03:27.220 --> 03:33.850
It is going to accumulate all the result and then send the response as a single value.

03:33.850 --> 03:35.290
That's what it is going to do.

03:35.290 --> 03:40.930
So by function, which is going to take two parameters as an input, I'm just giving it a name as a

03:40.930 --> 03:47.200
comma B, you can give any name you want and then what I'm going to do, I'm going to perform the multiplication

03:47.200 --> 03:51.790
of those two values, and then we are going to return the result.

03:52.690 --> 03:59.860
So what this is going to do is this is going to return a new result for every value that is being passed

03:59.860 --> 04:00.700
from the stream.

04:00.700 --> 04:04.120
So it is going to pass the stream values in this order.

04:04.120 --> 04:06.310
One three.

04:08.970 --> 04:09.640
And seven.

04:10.140 --> 04:18.710
Now, let's talk in detail about how these values A and B are populated with what values in that stream.

04:18.720 --> 04:24.540
So let's talk about the first iteration, meaning it is going to pass the first element in the stream.

04:24.540 --> 04:30.540
What will happen is that a equal to one, which is the default value, The identity value will be assigned

04:30.540 --> 04:33.630
here and be equal to one from stream.

04:36.100 --> 04:37.240
And what is happening here?

04:37.240 --> 04:39.760
It is performing the multiplication operation.

04:39.760 --> 04:40.000
Right.

04:40.000 --> 04:41.410
And the result will be returned.

04:43.370 --> 04:44.990
Result, one is returned.

04:49.550 --> 04:50.720
And what is the next step?

04:50.840 --> 04:52.340
The next step is.

04:54.170 --> 04:58.070
Yay equal to result from previous step.

04:58.100 --> 05:03.500
The result from the previous step is one and be equal to three.

05:05.170 --> 05:12.240
Is again from Dream and the result is going to be it is going to perform the multiplication here and

05:12.240 --> 05:15.040
the result three is returned.

05:19.570 --> 05:20.770
And what is the next step?

05:20.770 --> 05:22.000
The next step is.

05:23.860 --> 05:31.570
The value of E is going to be three from the result and the value of B is going to be five.

05:34.430 --> 05:37.100
So the result is going to be this value.

05:37.100 --> 05:42.950
The B is from stream and the result is going to be 15.

05:43.310 --> 05:43.970
Right.

05:45.250 --> 05:49.180
Result, 15 is returned.

05:49.780 --> 05:50.530
That's good.

05:53.110 --> 05:54.400
So what is the next thing?

05:54.430 --> 06:02.320
The next thing is is going to be 15 because that's a result it produced as part of the multiplication

06:02.320 --> 06:06.730
operation of three into five and B is going to be seven.

06:07.600 --> 06:09.430
The value is coming from stream.

06:09.430 --> 06:17.260
So the B value is always from the stream and the value is a result of the reduce operation for each

06:17.260 --> 06:19.020
and every element in the stream.

06:19.030 --> 06:21.700
The result is going to be one No.

06:21.710 --> 06:22.090
Five.

06:24.560 --> 06:33.200
So this is the background operations that stream performs for us in order to perform the multiplication

06:33.200 --> 06:36.050
of all the values in the string.

06:39.560 --> 06:41.380
So let's go ahead and check it.

06:41.390 --> 06:43.490
Let's run this and then find out the result.

06:44.360 --> 06:52.580
Now what I'm going to do, I'm going to perform a sort of perform multiplication and then I'm going

06:52.580 --> 06:53.990
to pass the integers.

06:54.650 --> 06:58.280
So if I run this, it is going to give me the.

06:59.370 --> 07:02.220
Result, which is the multiplication of all the values.

07:02.220 --> 07:03.680
That's what it did here.

07:03.690 --> 07:07.830
Now here we have given the initial value as one.

07:07.830 --> 07:13.920
Basically, when the first element is received from the stream, it is going to use this as an initial

07:13.920 --> 07:14.340
value.

07:14.340 --> 07:17.010
It is going to perform one into one.

07:17.010 --> 07:18.780
If I pass zero, what will happen?

07:18.780 --> 07:19.380
Right?

07:19.830 --> 07:20.610
We pass zero.

07:20.610 --> 07:27.690
The whole result is going to be zero because if you perform zero into one is going to be zero and all

07:27.690 --> 07:30.750
the elements is going to be multiplied with the zero value.

07:30.780 --> 07:35.280
That's the reason why the final result, whatever we have, we got it as zero.

07:35.520 --> 07:41.940
Now what we will do is I'm going to change it to change it back to its original value.

07:42.180 --> 07:42.870
There you go.

07:42.870 --> 07:48.930
Now, if we don't pass this right, there is another variation which we saw for the reduce method.

07:48.930 --> 07:51.060
If we don't pass it, what will happen?

07:51.940 --> 07:57.760
To perform multiplication without identity.

07:58.840 --> 08:02.020
Is that set a variable name that we have.

08:02.050 --> 08:05.620
So without identity, I'm going to remove this.

08:05.740 --> 08:07.280
So here we got some issue.

08:07.300 --> 08:09.700
Let's take a look at the issue that we got.

08:10.500 --> 08:12.140
So required.

08:12.150 --> 08:17.190
And so the type which it is going to return is INT But what do we have we found optional?

08:17.310 --> 08:21.750
So optional is a object which got released as part of Java eight.

08:22.080 --> 08:27.240
Don't worry about it for now because I'll be covering optional the optional as a separate section in

08:27.240 --> 08:28.170
the future tutorials.

08:28.170 --> 08:36.930
For now we just have to know instead of this we are going to return optional of type integer.

08:39.550 --> 08:40.120
Okay.

08:41.550 --> 08:48.690
So what this is going to do, main reason why this optional is introduced as part of Java eight is to

08:48.690 --> 08:52.830
handle the null values that it is going to return gracefully.

08:53.010 --> 08:53.970
Basically.

08:56.060 --> 08:57.170
It is value.

08:57.290 --> 09:01.790
What I'm going to do instead of this, I'm going to do perform.

09:02.920 --> 09:05.500
Multiplication without the identity.

09:06.010 --> 09:10.060
Then I'm going to pass this integer list as an input to that.

09:10.330 --> 09:16.660
So this is going to return optional of integer.

09:17.230 --> 09:21.160
And then let's say we give it a name called result.

09:21.990 --> 09:26.200
So in here, if you perform, I'm going to bring this one down.

09:26.260 --> 09:32.700
So if you want to check what are all the methods it supports the optional object supports.

09:32.700 --> 09:38.040
For now we're just going to explore a couple of methods and then we'll explore all the different methods

09:38.040 --> 09:44.610
in our future tutorials, which I'm going to cover Optional as a separate section.

09:45.750 --> 09:55.470
Dot get will give you the actual value inside that one is present will give you the actual is that value

09:55.470 --> 09:56.300
present or not?

09:56.310 --> 10:01.320
This is going to give you a boolean and this is going to give you the actual value.

10:02.220 --> 10:04.680
So these are the two methods which you have to know.

10:04.680 --> 10:05.550
For now.

10:05.580 --> 10:08.940
We will talk more about option in the future tutorials.

10:08.970 --> 10:10.530
Now I'm going to run this.

10:12.990 --> 10:16.220
If I run it, say it gave you the same result.

10:16.230 --> 10:20.730
But if you take a look at it, this value is returned as true.

10:20.760 --> 10:23.220
Let's say I'm going to pass a.

10:24.950 --> 10:27.200
Empty list and then we'll see what happens.

10:30.010 --> 10:30.910
But one.

10:32.180 --> 10:32.910
Gonna pass.

10:35.010 --> 10:36.390
Create a new list.

10:37.470 --> 10:38.790
This new.

10:40.330 --> 10:40.740
Else.

10:43.460 --> 10:45.230
In this year one.

10:46.200 --> 10:47.060
In its fastest.

10:48.730 --> 10:50.260
So I'm going to do.

10:51.620 --> 10:52.130
Result.

10:52.160 --> 10:53.480
One dot is present.

10:54.110 --> 10:54.860
Run this.

10:56.620 --> 10:58.540
We run it, we take a look at it.

10:58.720 --> 11:00.430
It returns the value as false.

11:00.430 --> 11:02.260
So if you pass.

11:04.090 --> 11:07.360
A list with values, then it is going to perform this.

11:07.360 --> 11:10.120
Otherwise it won't even perform it.

11:10.120 --> 11:12.850
But it will still return that optional object.

11:13.470 --> 11:20.600
You can check whether that optional has any value or not by using the Is present method.

11:20.610 --> 11:24.890
But your question might be this is this might be running in your mind.

11:24.900 --> 11:29.550
If I do get what will happen, this is going to give you exception.

11:31.840 --> 11:32.610
See here.

11:32.620 --> 11:34.150
No such element exception.

11:34.150 --> 11:40.420
So it is always recommended to check whether optional has a value or not.

11:41.740 --> 11:44.860
The option is a value or not and then perform the get method.

11:44.860 --> 11:49.330
So ideally it should be result one dot.

11:51.220 --> 11:51.880
Present.

11:51.970 --> 11:52.780
It is present.

11:52.780 --> 11:53.680
Then perform this.

11:53.680 --> 11:59.800
Otherwise do not even perform so that you can avoid that no value present exception.

12:03.340 --> 12:04.460
That you can avoid this exception.

12:04.480 --> 12:08.650
When I run this, see whether is it still giving me that exception?

12:10.400 --> 12:10.940
There you go.

12:10.970 --> 12:12.510
The exception isn't even there.

12:12.550 --> 12:16.740
So this is one way of using the reduce operation.

12:16.760 --> 12:21.040
So with the initial value and without the initial value.

12:21.050 --> 12:27.290
But this is again, the accumulator in both the scenarios is just a buy function or a binary operator,

12:27.290 --> 12:33.050
which takes two inputs and then results returns the result of the same type.

12:34.930 --> 12:35.230
But this.

12:35.230 --> 12:36.910
We came to the end of this tutorial.

12:36.940 --> 12:38.050
Thank you for watching.
