Hi everyone.
Welcome back to this tutorial.
In this tutorial, we'll code and learn how to perform the min operation of a list of integers in a
stream.
So here we talked about the max in the previous tutorial.
I'm going to use the same example here.
The first thing is I want to show the difference between with the identity and without the identity
value.
I'm going to copy the same code and then paste it here.
It's going to be named as min min value.
I'm going to comment this whole max part.
I don't want this.
Because I don't I'm not going to use this as part of this tutorial.
Now, let me put this in presentation mode.
Then here we are going to talk about the min value so far, the max value.
What we did was we used the greater than for this.
We are just going to change this one to less than.
Let's talk about it.
What happens behind the scenes.
So here six is going to be assigned against the Y variable here and X is going to hold the result of
it, right?
So for the first time, what it is going to do, it is going to assign zero.
That is the initial value against the X variable and then Y is going to hold six for the first element
in the string because we know string is going to pass the elements one by one and then is zero less
than six.
So here is a condition which we which we have is a zero less than six.
Yes.
In that case X will be assigned with zero value for the next element.
Seven is again, seven is set against the y variable is zero less than seven.
Yes.
And then it is going to assign the zero as a minimum value.
It is going to continue until all the elements in the stream are passed as part of the iteration.
What I'm going to do, I'm going to call this method and then check what is the result that it is going
to give.
So int min value equal to.
Find min value and then pass integer list.
You're going to be surprised by looking at the result because.
We'll get to know about that in a bit.
Run this.
So the output that we're expecting is six, but the result that we got is zero.
Why?
If we take a look at it, we have the default value, which is set to zero.
That is going to be the least value compared to all the elements in the string.
Right.
So this is something which we have to keep in our mind when you are calculating the min value.
You should never, ever use the default value.
So I'm going to remove this and then let's change this one to optional.
Same concept.
And then I'm going to copy this and put it here.
I'll be minvalue.
Optional.
And then now the drill.
Going to print the mean value optional.
Here the f min value dot is present, then I will get the value and then printed this out.
The minimum value is.
Minvalue optional dot get.
Good action is else going to print?
No input is.
Now let's run this and check.
So we are we're calling the find min value.
It is taking a list is iterating, and then it is going to apply each and every value in the stream
to the Y variable, and X is going to hold the result as the.
X variable.
So in here, let's run this and check.
So the value that I'm expecting is six.
We take a look at it.
The value is six.
Let's say I'm passing an empty list.
What will happen?
Empty list is going to.
Print the result as no input is passed.
Take a look at it.
No input is passed and the optional value is.
So this is one of the approach of calculating the min value using the reduce function.
So this is all about how to calculate the max and min value from a stream of integers.
You can just do it for any kind of other number types.
Double long.
The same approach.
But thus we came to the end of this tutorial.
Thank you for watching.