WEBVTT

00:00.580 --> 00:01.210
Hi everyone.

00:01.210 --> 00:02.560
Welcome back to this tutorial.

00:02.560 --> 00:10.690
In this tutorial we will code and learn about how to perform Min and Max operation on a list of integers.

00:10.960 --> 00:18.640
The first step is I'm going to create a class called PriMS Min Max.

00:19.770 --> 00:20.550
Example.

00:21.550 --> 00:25.690
And then I'm going to make this class executable by adding public static void main.

00:26.200 --> 00:26.980
There you go.

00:27.010 --> 00:29.290
Now I'm going to put this in presentation mode.

00:30.920 --> 00:31.360
Afterwards.

00:31.370 --> 00:33.500
I'm going to build the input list.

00:33.530 --> 00:36.380
List of integers.

00:38.220 --> 00:39.150
And then.

00:41.450 --> 00:43.460
Out of the Java.util package.

00:43.820 --> 00:44.780
Give it a name.

00:45.840 --> 00:47.420
The name is going to be integer list.

00:47.430 --> 00:48.990
You can give any name you want.

00:49.110 --> 00:53.460
Next is arrays dot as list of I'm going to provide the numbers.

00:53.460 --> 00:56.070
Six, seven, eight nine.

00:57.170 --> 00:57.740
And ten.

00:59.690 --> 01:01.970
Okay, so now we have the input list ready.

01:01.970 --> 01:07.340
We want to calculate the max element out of this input list using the reduce function.

01:07.790 --> 01:09.020
Let's create a method.

01:09.620 --> 01:10.730
Public static.

01:11.630 --> 01:17.420
And then this method is going to return an int, which is a maximum number, let's name it as max value

01:18.110 --> 01:22.130
or let's name it as find max value.

01:23.430 --> 01:23.900
Okay.

01:23.910 --> 01:30.060
Now we have we are going to parse the input right this integer list as an input to this method.

01:31.890 --> 01:36.930
The next step is we are going to create integer list, dot stream.

01:37.140 --> 01:41.040
And what we are going to do is we are going to perform the reduce operation.

01:41.160 --> 01:42.360
So reduce operation.

01:42.360 --> 01:44.760
If you take a look at it, there are many variants of it.

01:44.760 --> 01:47.130
But we are going to take a look at this one first.

01:47.160 --> 01:49.800
The reduce operation, which takes.

01:50.920 --> 01:52.000
Go and take a look at that.

01:53.220 --> 01:54.840
For some reason, it is not taking me there.

01:54.870 --> 01:55.110
Okay.

01:55.110 --> 01:55.650
There you go.

01:55.680 --> 01:57.690
The reduce operation takes an.

01:58.610 --> 02:03.110
Identity, which can be a default value or initial value for the first element.

02:03.110 --> 02:05.030
And then next is a binary operator.

02:05.030 --> 02:11.540
We all know binary operator by now, binary operator is a type of function which takes two inputs and

02:11.540 --> 02:13.460
return an output of the same type.

02:14.580 --> 02:16.140
No baserunners.

02:16.230 --> 02:16.980
Zero.

02:17.160 --> 02:19.830
That is going to be the initial value of the stream.

02:19.830 --> 02:21.390
And then it is going to take two inputs.

02:21.390 --> 02:24.930
I'm going to give two different names, X and Y.

02:25.630 --> 02:30.160
And then what we are going to do, our job is to calculate the maximum value.

02:30.190 --> 02:32.080
We all know reduce function by now.

02:32.080 --> 02:37.390
Reduce function always returns a output for every element that it performs.

02:37.390 --> 02:39.060
This reduce operation in the stream.

02:39.070 --> 02:44.890
So if X is greater than Y, then return x.

02:48.330 --> 02:51.270
So for every iteration, for every element.

02:53.750 --> 02:55.540
I'm going to pass this element in this order.

02:55.540 --> 02:56.320
Seven.

02:56.980 --> 02:57.640
Eight.

02:58.920 --> 03:01.140
Nine, then ten.

03:01.350 --> 03:07.050
So for every element it is going to be passed from the stream, it is going to pass these elements one

03:07.050 --> 03:08.490
by one for every element.

03:08.490 --> 03:14.760
It is going to perform this operation and then return the max value and assign the max value to the

03:14.760 --> 03:16.920
Y variable to the x variable.

03:17.400 --> 03:18.450
X variable.

03:20.670 --> 03:23.880
Holds the max value.

03:25.470 --> 03:27.240
For each element.

03:28.410 --> 03:29.370
In the iteration.

03:31.930 --> 03:33.670
Let's talk through this one for the first time.

03:33.670 --> 03:34.480
It is going to pass.

03:34.480 --> 03:40.150
The six is going to be assigned All these values from the stream will be assigned to the Y variable.

03:40.870 --> 03:41.590
Everything.

03:42.520 --> 03:43.120
Why?

03:43.900 --> 03:44.830
Copy this.

03:45.530 --> 03:46.030
Why?

03:46.040 --> 03:46.590
Why?

03:46.590 --> 03:46.890
Why?

03:47.020 --> 03:52.090
So when it when the stream iterates and paths elements one by one, all these values will be assigned

03:52.090 --> 03:58.100
to Y And the result of this iteration result of this function will be assigned to the x variable.

03:58.120 --> 04:03.670
So for the first element, when the six is being passed, x will hold the value zero is zero, greater

04:03.670 --> 04:06.580
than Y is greater, zero greater than six?

04:06.910 --> 04:07.570
Yes.

04:07.570 --> 04:08.830
It's not greater than six.

04:08.830 --> 04:13.910
It's going to return six as the greatest element and that value will be assigned to X.

04:13.930 --> 04:19.510
So when for the next element, seven comes in, x will hold the six, which is a result of the previous

04:19.540 --> 04:23.920
iteration, and then seven is a current value in the stream it compares.

04:23.920 --> 04:25.390
It's a six greater than seven.

04:25.690 --> 04:26.320
No.

04:26.320 --> 04:29.710
Then Y will be returned y, which is seven.

04:29.710 --> 04:34.160
So for the next iteration, seven will be assigned to the X variable.

04:34.180 --> 04:36.430
So that's how the workflow.

04:37.710 --> 04:39.110
Is going to work.

04:39.110 --> 04:44.060
And then the next thing is we are going to call this function and then find out the max value.

04:44.340 --> 04:46.550
INT max value.

04:48.290 --> 04:52.160
Equal to find max value and then pass this integer list.

04:53.280 --> 04:56.880
Okay, now I'm going to print this max value.

05:00.370 --> 05:01.490
Max values.

05:02.550 --> 05:05.280
And then I'm going to create.

05:07.360 --> 05:07.990
Render result.

05:08.380 --> 05:09.610
Let's run this and then check.

05:11.230 --> 05:14.560
There is an issue because I did not return this.

05:14.590 --> 05:16.030
You have to return this.

05:16.780 --> 05:18.400
Let's run this and check the result.

05:20.770 --> 05:22.360
So it printed the value as ten.

05:22.390 --> 05:25.120
That is what is expected for the max value.

05:25.210 --> 05:30.190
Right now, what we will do is let's pass an empty list and then see what happens.

05:30.220 --> 05:31.540
I'm going to comment this one.

05:33.770 --> 05:37.610
And then I'm going to give it a name called New List.

05:39.870 --> 05:40.600
And pass this.

05:40.620 --> 05:43.080
Let's see, what is a value that it is going to return.

05:44.910 --> 05:50.340
Basically we are not passing any value, but still the max value is returned as zero.

05:50.610 --> 05:51.780
That is not right.

05:51.780 --> 05:55.020
The reason being if the input value is.

05:55.050 --> 06:00.810
If the input list is empty, ideally it should return us an empty value, not zero because zero.

06:00.840 --> 06:06.840
The reason why it is returning zero is because we have a default value set and this is not correct.

06:06.840 --> 06:09.450
So there is an another approach for that.

06:09.450 --> 06:10.800
So you should always.

06:11.370 --> 06:18.900
You should avoid assigning a default value whenever you are performing a min and max value from a stream

06:18.930 --> 06:20.160
of integers.

06:20.190 --> 06:23.040
That is something which we have to keep it in our mind.

06:23.190 --> 06:26.190
So I'm going to create another method.

06:27.910 --> 06:31.720
And then I'm going to name this one as point value optional.

06:31.720 --> 06:34.570
So we talked about optional a bit in the previous tutorial.

06:34.600 --> 06:37.330
Now we will know the benefit of using Optionals.

06:37.480 --> 06:42.580
So in here what we are going to do, I'm going to remove this default value.

06:42.610 --> 06:44.530
Let's see, we have a compilation issue here.

06:44.530 --> 06:46.930
Let's take a look at what is that issue?

06:47.260 --> 06:51.790
Make find Max value optional return Java.util dot optional.

06:51.790 --> 06:55.380
Right now we are returning the integer, so this one is trying to return an integer.

06:55.390 --> 07:00.160
They're asking us to change the return type to optional of integer.

07:00.160 --> 07:01.360
Let's go ahead and do that.

07:02.770 --> 07:04.410
Click on this, it's going to change the type.

07:04.420 --> 07:05.200
So there you go.

07:05.230 --> 07:06.490
It written this.

07:06.580 --> 07:12.400
Now what we are going to do, I'm going to come on this one out and then.

07:13.340 --> 07:14.150
Import this.

07:17.710 --> 07:22.330
I'm gonna call this method, find Max value optional.

07:22.330 --> 07:24.190
I'm going to pass this integer list.

07:24.790 --> 07:28.360
So this is going to return me an optional of type integer.

07:30.000 --> 07:33.330
And then this is going to be max value.

07:35.190 --> 07:35.940
Optional.

07:38.520 --> 07:39.240
Equal to this.

07:40.020 --> 07:43.110
Now, how can we get the result?

07:43.380 --> 07:46.890
The way to get it is if the max value has a value.

07:47.730 --> 07:52.920
Then the use present method call is going to return as a boolean as true.

07:52.950 --> 07:53.640
Right?

07:55.170 --> 07:56.010
If it is true.

07:56.040 --> 07:58.170
Meaning there is some value present in that one.

07:59.100 --> 08:00.990
Gonna print the value.

08:02.180 --> 08:04.340
Max value using optional.

08:05.400 --> 08:08.190
This max value optional.

08:10.030 --> 08:14.950
Dot get get will give you the actual value in that particular optional object.

08:15.340 --> 08:21.190
And if it is, if there is no value present in those cases, we can give.

08:23.180 --> 08:23.540
In print.

08:23.540 --> 08:25.310
The output has no max value.

08:27.460 --> 08:30.190
Okay, now let's run this example and then check.

08:34.430 --> 08:37.560
If you take a look at the result, both the result are the same.

08:37.580 --> 08:43.580
The things will be they're going to be interesting when we pass the list as empty value.

08:43.880 --> 08:46.460
If we pass the list as empty value, what will happen?

08:47.300 --> 08:52.640
So ideally I'm expecting no max value found because the input is empty.

08:52.670 --> 08:53.960
Or you can have.

08:56.640 --> 09:03.450
Input list is empty because this is the right message, because we passed an empty list and we want

09:03.450 --> 09:04.110
to print.

09:04.140 --> 09:04.500
Okay.

09:04.500 --> 09:10.980
I do not see any input value, so I'm not going to send the optional object with some value.

09:11.010 --> 09:13.410
It is going to send an empty optional.

09:13.410 --> 09:16.560
We can print this optional and then check to this out.

09:18.830 --> 09:20.240
Optional Max's.

09:22.490 --> 09:23.060
Okay.

09:23.240 --> 09:24.140
Run this check.

09:26.690 --> 09:27.650
If you run it.

09:27.800 --> 09:28.370
There you go.

09:28.550 --> 09:32.690
So it printed the optional max is optional dot empty.

09:33.230 --> 09:35.840
And then the input list is empty is what we printed.

09:36.290 --> 09:38.300
Let's say we.

09:40.600 --> 09:41.470
Is this what will happen?

09:43.710 --> 09:46.610
Going to print optional with a value ten.

09:46.620 --> 09:55.110
So this is one of the key difference of using optional and without optional while calculating the max

09:55.110 --> 09:55.730
value.

09:55.740 --> 10:03.000
So always keep in your mind if you are calculating a max value, it is always recommended to use optional

10:03.360 --> 10:04.500
as a return type.

10:06.470 --> 10:10.210
We will discuss about the min operation in the next tutorial.

10:10.220 --> 10:12.400
With this we came to the end of this tutorial.

10:12.410 --> 10:13.300
Thank you for watching.
