Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and learn about how to perform Min and Max operation on a list of integers.
The first step is I'm going to create a class called PriMS Min Max.
Example.
And then I'm going to make this class executable by adding public static void main.
There you go.
Now I'm going to put this in presentation mode.
Afterwards.
I'm going to build the input list.
List of integers.
And then.
Out of the Java.util package.
Give it a name.
The name is going to be integer list.
You can give any name you want.
Next is arrays dot as list of I'm going to provide the numbers.
Six, seven, eight nine.
And ten.
Okay, so now we have the input list ready.
We want to calculate the max element out of this input list using the reduce function.
Let's create a method.
Public static.
And then this method is going to return an int, which is a maximum number, let's name it as max value
or let's name it as find max value.
Okay.
Now we have we are going to parse the input right this integer list as an input to this method.
The next step is we are going to create integer list, dot stream.
And what we are going to do is we are going to perform the reduce operation.
So reduce operation.
If you take a look at it, there are many variants of it.
But we are going to take a look at this one first.
The reduce operation, which takes.
Go and take a look at that.
For some reason, it is not taking me there.
Okay.
There you go.
The reduce operation takes an.
Identity, which can be a default value or initial value for the first element.
And then next is a binary operator.
We all know binary operator by now, binary operator is a type of function which takes two inputs and
return an output of the same type.
No baserunners.
Zero.
That is going to be the initial value of the stream.
And then it is going to take two inputs.
I'm going to give two different names, X and Y.
And then what we are going to do, our job is to calculate the maximum value.
We all know reduce function by now.
Reduce function always returns a output for every element that it performs.
This reduce operation in the stream.
So if X is greater than Y, then return x.
So for every iteration, for every element.
I'm going to pass this element in this order.
Seven.
Eight.
Nine, then ten.
So for every element it is going to be passed from the stream, it is going to pass these elements one
by one for every element.
It is going to perform this operation and then return the max value and assign the max value to the
Y variable to the x variable.
X variable.
Holds the max value.
For each element.
In the iteration.
Let's talk through this one for the first time.
It is going to pass.
The six is going to be assigned All these values from the stream will be assigned to the Y variable.
Everything.
Why?
Copy this.
Why?
So when it when the stream iterates and paths elements one by one, all these values will be assigned
to Y And the result of this iteration result of this function will be assigned to the x variable.
So for the first element, when the six is being passed, x will hold the value zero is zero, greater
than Y is greater, zero greater than six?
Yes.
It's not greater than six.
It's going to return six as the greatest element and that value will be assigned to X.
So when for the next element, seven comes in, x will hold the six, which is a result of the previous
iteration, and then seven is a current value in the stream it compares.
It's a six greater than seven.
No.
Then Y will be returned y, which is seven.
So for the next iteration, seven will be assigned to the X variable.
So that's how the workflow.
Is going to work.
And then the next thing is we are going to call this function and then find out the max value.
INT max value.
Equal to find max value and then pass this integer list.
Okay, now I'm going to print this max value.
Max values.
And then I'm going to create.
Render result.
Let's run this and then check.
There is an issue because I did not return this.
You have to return this.
Let's run this and check the result.
So it printed the value as ten.
That is what is expected for the max value.
Right now, what we will do is let's pass an empty list and then see what happens.
I'm going to comment this one.
And then I'm going to give it a name called New List.
And pass this.
Let's see, what is a value that it is going to return.
Basically we are not passing any value, but still the max value is returned as zero.
That is not right.
The reason being if the input value is.
If the input list is empty, ideally it should return us an empty value, not zero because zero.
The reason why it is returning zero is because we have a default value set and this is not correct.
So there is an another approach for that.
So you should always.
You should avoid assigning a default value whenever you are performing a min and max value from a stream
of integers.
That is something which we have to keep it in our mind.
So I'm going to create another method.
And then I'm going to name this one as point value optional.
So we talked about optional a bit in the previous tutorial.
Now we will know the benefit of using Optionals.
So in here what we are going to do, I'm going to remove this default value.
Let's see, we have a compilation issue here.
Let's take a look at what is that issue?
Make find Max value optional return Java.util dot optional.
Right now we are returning the integer, so this one is trying to return an integer.
They're asking us to change the return type to optional of integer.
Let's go ahead and do that.
Click on this, it's going to change the type.
So there you go.
It written this.
Now what we are going to do, I'm going to come on this one out and then.
Import this.
I'm gonna call this method, find Max value optional.
I'm going to pass this integer list.
So this is going to return me an optional of type integer.
And then this is going to be max value.
Optional.
Equal to this.
Now, how can we get the result?
The way to get it is if the max value has a value.
Then the use present method call is going to return as a boolean as true.
Right?
If it is true.
Meaning there is some value present in that one.
Gonna print the value.
Max value using optional.
This max value optional.
Dot get get will give you the actual value in that particular optional object.
And if it is, if there is no value present in those cases, we can give.
In print.
The output has no max value.
Okay, now let's run this example and then check.
If you take a look at the result, both the result are the same.
The things will be they're going to be interesting when we pass the list as empty value.
If we pass the list as empty value, what will happen?
So ideally I'm expecting no max value found because the input is empty.
Or you can have.
Input list is empty because this is the right message, because we passed an empty list and we want
to print.
Okay.
I do not see any input value, so I'm not going to send the optional object with some value.
It is going to send an empty optional.
We can print this optional and then check to this out.
Optional Max's.
Okay.
Run this check.
If you run it.
There you go.
So it printed the optional max is optional dot empty.
And then the input list is empty is what we printed.
Let's say we.
Is this what will happen?
Going to print optional with a value ten.
So this is one of the key difference of using optional and without optional while calculating the max
value.
So always keep in your mind if you are calculating a max value, it is always recommended to use optional
as a return type.
We will discuss about the min operation in the next tutorial.
With this we came to the end of this tutorial.
Thank you for watching.