WEBVTT

00:00.320 --> 00:00.830
Hi everyone.

00:00.830 --> 00:02.150
Welcome back to this tutorial.

00:02.150 --> 00:07.850
In this tutorial we will code and explore the aggregate functions which are part of the numeric stream.

00:07.850 --> 00:12.430
The aggregate functions are sum, max, min and average.

00:12.440 --> 00:13.970
Let's go ahead and code this one.

00:14.990 --> 00:16.210
Have the interledger open.

00:16.210 --> 00:19.150
I'm going to create a class for this purpose.

00:19.450 --> 00:25.390
Class name is going to be numeric stream aggregate example.

00:26.530 --> 00:30.850
Make this class executable by adding the public static void main method.

00:31.300 --> 00:35.140
And in here, I'm going to use the string.

00:35.140 --> 00:37.750
Before that, let me put this in presentation mode.

00:39.100 --> 00:48.580
I'm going to use in stream dot range closed on comma 50 and I'm going to perform the summation of all

00:48.580 --> 00:50.560
the values in this range.

00:50.770 --> 00:51.610
How do I do it?

00:51.650 --> 00:54.430
So there is a handy method, which is part of the.

00:55.520 --> 00:57.110
Human extreme itself.

00:57.770 --> 00:58.640
We take a look at it.

00:58.640 --> 01:02.120
There is a method called some and some.

01:03.550 --> 01:06.190
Let's print this value and check the result.

01:08.940 --> 01:09.510
Dummies.

01:12.030 --> 01:13.500
And then let's print it.

01:13.860 --> 01:15.330
Go ahead and execute this.

01:16.940 --> 01:17.390
There you go.

01:17.390 --> 01:20.330
It printed the value as 127, five.

01:21.150 --> 01:25.350
Now I want to print the max value of this one.

01:25.980 --> 01:26.550
Right.

01:26.550 --> 01:30.990
So I'm going to again call this one and then call the max value.

01:32.050 --> 01:37.090
There is a method called Max, which is going to return what is a maximum value in this range.

01:37.090 --> 01:38.170
But we know the result.

01:38.170 --> 01:40.240
The answer is going to be 50.

01:40.270 --> 01:41.680
But let's go ahead and check.

01:41.680 --> 01:42.130
What is that?

01:42.130 --> 01:43.030
It is returning.

01:43.150 --> 01:45.520
It is returning something called optional.

01:45.520 --> 01:47.110
And what does it mean?

01:47.110 --> 01:50.350
So if the stream is empty, right.

01:50.350 --> 01:54.520
In that case, it is going to return this optional object with the empty value.

01:54.520 --> 01:55.420
That's what it means.

01:55.420 --> 01:59.320
So we touched on optional a bit in the previous tutorials, but don't worry about it.

01:59.320 --> 02:02.020
We'll be having a separate section for Optional.

02:02.020 --> 02:06.010
We'll discuss all the methods it supports in that particular section.

02:07.770 --> 02:10.440
Know this is going to return something called optional.

02:10.440 --> 02:12.450
INT optional.

02:12.450 --> 02:14.000
INT I'm going to give it a name.

02:14.010 --> 02:14.580
There you go.

02:15.260 --> 02:15.920
So.

02:17.000 --> 02:18.190
We are going to print this one.

02:18.430 --> 02:19.660
Let me print this one.

02:21.050 --> 02:22.460
Then see what it has.

02:24.540 --> 02:26.250
The max value was.

02:28.760 --> 02:30.110
Then give it a colon.

02:30.560 --> 02:31.700
Let's run this.

02:32.480 --> 02:33.080
Run this.

02:33.110 --> 02:35.390
It is giving a max value of 50.

02:35.420 --> 02:36.880
That is what is expected.

02:36.890 --> 02:38.120
But what is our advantage?

02:38.120 --> 02:38.690
Right?

02:38.900 --> 02:41.540
So what is present?

02:42.290 --> 02:42.920
Is present.

02:44.160 --> 02:45.480
And I'm going to run this.

02:46.970 --> 02:49.210
We take a look at it, the value is true.

02:49.220 --> 02:55.400
So the advantage here is that if there is no stream value, if the stream is empty in those kind of

02:55.400 --> 02:59.720
cases, this present is going to be false.

02:59.720 --> 03:08.120
And then you can call if it is present, let's say we can print it like this then optional in dot,

03:08.870 --> 03:12.980
get as int, get as int is a method which will give you the actual int value.

03:13.100 --> 03:15.080
Otherwise the value can be zero.

03:17.030 --> 03:17.720
Run this.

03:19.270 --> 03:19.640
There you go.

03:19.660 --> 03:21.250
It printed the value as false.

03:21.280 --> 03:22.750
If the range is nothing.

03:22.750 --> 03:24.430
So let's write out.

03:27.550 --> 03:35.230
And it's still going to give you some as the value is zero because I believe it creates a range of zero.

03:35.230 --> 03:36.610
Let's go ahead and check that to.

03:38.620 --> 03:39.580
It was a out.

03:41.940 --> 03:42.660
Dot count.

03:44.270 --> 03:46.340
If it prints zero, then it means so.

03:46.340 --> 03:47.570
It printed the value as one.

03:47.570 --> 03:51.380
It still created a range, the value as zero.

03:52.630 --> 03:57.610
So if this dream is empty, then the value will be returned as false.

03:57.610 --> 03:59.740
In the optional event Dot is present.

04:00.010 --> 04:04.540
Let's go ahead and explore the next aggregate function which is the min.

04:04.720 --> 04:10.240
I'm going to use long stream instead of the end stream.

04:10.300 --> 04:12.160
I'm going to use a range closed.

04:12.340 --> 04:20.710
Give the value as one 200 or give the value as 5200 and I'm going to calculate the min value.

04:21.490 --> 04:22.720
We go to this min method.

04:22.720 --> 04:27.790
It is going to return optional long but is something we have to keep in our mind.

04:28.730 --> 04:35.060
So whenever you perform Max and Min operation, it is going to return the optional value.

04:35.090 --> 04:37.820
So it's going to be optional long.

04:41.010 --> 04:43.280
Then give it a name optional long.

04:44.100 --> 04:46.620
I'm going to use the same concept here.

04:48.650 --> 04:57.710
Optional long dot is present, then get the value as get as long and this is going to be zero.

04:58.220 --> 05:03.290
And this the value that I'm expecting the result that it should print would be 50.

05:05.220 --> 05:05.730
There you go.

05:05.730 --> 05:07.620
It printed the value as.

05:08.680 --> 05:10.270
So this is all about the min method.

05:10.300 --> 05:13.270
Let's go ahead and explore the.

05:14.770 --> 05:19.450
Average aggregate function when use in stream again.

05:20.660 --> 05:22.430
Stream dot range closed.

05:22.430 --> 05:25.150
I'm going to give the value as one comma 50.

05:25.380 --> 05:28.790
Then I'm going to perform average.

05:29.300 --> 05:31.080
Go ahead and take a look at the average.

05:31.100 --> 05:34.970
Average is going to return something called optional double.

05:35.750 --> 05:37.880
That is something you have to keep in your mind.

05:38.920 --> 05:39.910
Optional double.

05:40.750 --> 05:42.120
Let's import this.

05:42.160 --> 05:44.410
Want to use the same logic here.

05:46.750 --> 05:52.570
The optional double is present, then I will be get as double.

05:53.480 --> 05:55.730
And then if it is, the stream is empty.

05:55.760 --> 05:58.850
It is going to return the yeast present with the false value.

05:58.880 --> 06:01.490
In that case, we'll print the value as zero.

06:01.940 --> 06:02.810
And this.

06:04.340 --> 06:04.910
There you go.

06:05.060 --> 06:07.670
Enter the value as 25.5.

06:07.700 --> 06:11.720
Basically, that is the average for 1 to 50.

06:13.320 --> 06:20.160
So this is all about the different aggregate functions that int double and the long stream supports.

06:20.610 --> 06:22.620
With this we came to the end of this tutorial.

06:22.650 --> 06:23.700
Thank you for watching.
