Hi everyone.
Welcome back to this tutorial.
In this tutorial, we'll talk about the reduce function which is part of the streams API.
Reduce is a terminal operation.
We have seen one another terminal operation in the previous tutorials, which is the collect.
This is the second terminal operation which we are going to discuss as part of this tutorial.
So this is basically used to reduce the contents of the stream to a single value.
That's what it is going to do.
Let's say you want to find a sum of all the elements in the stream or perform multiplication of all
the elements in the stream, or you want to find out who is the highest grade student out of the student
database.
In those kind of scenarios, you can make use of the reduce function.
So the reduce function takes in two parameters as an input.
The first parameter is the default or the initial value.
And the second parameter is the binary.
Operator.
A quick summary of the binary operator is that binary operator is like a function which takes two inputs
of the same type and returns an output of the same type.
Let's go ahead and code this one.
So have the previous example here want to move away from the presentation mode?
Was this what I'm going to do for the purpose of this tutorial?
I'm going to create a class called Stream Reduce.
Dream, reduce example.
And then I'm going to make this class executable by adding public static void main.
There we go.
Now what I'm going to do, I'm going to create a list of numbers.
List of.
Integers.
And then this integer list is going to have.
Restart as list.
I'm going to add like four numbers in it.
135, seven.
Let me put this in presentation mode.
So what I'm looking for is that I want the multiplication of all the numbers as a result.
That's what I'm looking for.
So how can we achieve that using the reduce method?
I'm going to introduce a method called public static int.
Perform.
Multiplication.
And then pass these numbers as an input.
I'm going to pass this list of.
Integer.
An integer list.
You're going to do integer list.
Dot stream.
With the method that we are going to, the function that we are going to look is the reduce.
If you take a look at the reduce, it has many different variations.
The one which we are going to look for is this one which has the identity and the by function, right?
So what I'm going to do, I'm going to use this, the first parameter is a default value or the initial
value.
I want the initial value to be one.
And then it is a by function.
Basically, if you take a look at it, it is an accumulator.
It is going to accumulate all the result and then send the response as a single value.
That's what it is going to do.
So by function, which is going to take two parameters as an input, I'm just giving it a name as a
comma B, you can give any name you want and then what I'm going to do, I'm going to perform the multiplication
of those two values, and then we are going to return the result.
So what this is going to do is this is going to return a new result for every value that is being passed
from the stream.
So it is going to pass the stream values in this order.
One three.
And seven.
Now, let's talk in detail about how these values A and B are populated with what values in that stream.
So let's talk about the first iteration, meaning it is going to pass the first element in the stream.
What will happen is that a equal to one, which is the default value, The identity value will be assigned
here and be equal to one from stream.
And what is happening here?
It is performing the multiplication operation.
Right.
And the result will be returned.
Result, one is returned.
And what is the next step?
The next step is.
Yay equal to result from previous step.
The result from the previous step is one and be equal to three.
Is again from Dream and the result is going to be it is going to perform the multiplication here and
the result three is returned.
And what is the next step?
The next step is.
The value of E is going to be three from the result and the value of B is going to be five.
So the result is going to be this value.
The B is from stream and the result is going to be 15.
Right.
Result, 15 is returned.
That's good.
So what is the next thing?
The next thing is is going to be 15 because that's a result it produced as part of the multiplication
operation of three into five and B is going to be seven.
The value is coming from stream.
So the B value is always from the stream and the value is a result of the reduce operation for each
and every element in the stream.
The result is going to be one No.
Five.
So this is the background operations that stream performs for us in order to perform the multiplication
of all the values in the string.
So let's go ahead and check it.
Let's run this and then find out the result.
Now what I'm going to do, I'm going to perform a sort of perform multiplication and then I'm going
to pass the integers.
So if I run this, it is going to give me the.
Result, which is the multiplication of all the values.
That's what it did here.
Now here we have given the initial value as one.
Basically, when the first element is received from the stream, it is going to use this as an initial
value.
It is going to perform one into one.
If I pass zero, what will happen?
Right?
We pass zero.
The whole result is going to be zero because if you perform zero into one is going to be zero and all
the elements is going to be multiplied with the zero value.
That's the reason why the final result, whatever we have, we got it as zero.
Now what we will do is I'm going to change it to change it back to its original value.
There you go.
Now, if we don't pass this right, there is another variation which we saw for the reduce method.
If we don't pass it, what will happen?
To perform multiplication without identity.
Is that set a variable name that we have.
So without identity, I'm going to remove this.
So here we got some issue.
Let's take a look at the issue that we got.
So required.
And so the type which it is going to return is INT But what do we have we found optional?
So optional is a object which got released as part of Java eight.
Don't worry about it for now because I'll be covering optional the optional as a separate section in
the future tutorials.
For now we just have to know instead of this we are going to return optional of type integer.
Okay.
So what this is going to do, main reason why this optional is introduced as part of Java eight is to
handle the null values that it is going to return gracefully.
Basically.
It is value.
What I'm going to do instead of this, I'm going to do perform.
Multiplication without the identity.
Then I'm going to pass this integer list as an input to that.
So this is going to return optional of integer.
And then let's say we give it a name called result.
So in here, if you perform, I'm going to bring this one down.
So if you want to check what are all the methods it supports the optional object supports.
For now we're just going to explore a couple of methods and then we'll explore all the different methods
in our future tutorials, which I'm going to cover Optional as a separate section.
Dot get will give you the actual value inside that one is present will give you the actual is that value
present or not?
This is going to give you a boolean and this is going to give you the actual value.
So these are the two methods which you have to know.
For now.
We will talk more about option in the future tutorials.
Now I'm going to run this.
If I run it, say it gave you the same result.
But if you take a look at it, this value is returned as true.
Let's say I'm going to pass a.
Empty list and then we'll see what happens.
But one.
Gonna pass.
Create a new list.
This new.
Else.
In this year one.
In its fastest.
So I'm going to do.
Result.
One dot is present.
Run this.
We run it, we take a look at it.
It returns the value as false.
So if you pass.
A list with values, then it is going to perform this.
Otherwise it won't even perform it.
But it will still return that optional object.
You can check whether that optional has any value or not by using the Is present method.
But your question might be this is this might be running in your mind.
If I do get what will happen, this is going to give you exception.
See here.
No such element exception.
So it is always recommended to check whether optional has a value or not.
The option is a value or not and then perform the get method.
So ideally it should be result one dot.
Present.
It is present.
Then perform this.
Otherwise do not even perform so that you can avoid that no value present exception.
That you can avoid this exception.
When I run this, see whether is it still giving me that exception?
There you go.
The exception isn't even there.
So this is one way of using the reduce operation.
So with the initial value and without the initial value.
But this is again, the accumulator in both the scenarios is just a buy function or a binary operator,
which takes two inputs and then results returns the result of the same type.
But this.
We came to the end of this tutorial.
Thank you for watching.