Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the aggregate functions which are part of the numeric stream.
The aggregate functions are sum, max, min and average.
Let's go ahead and code this one.
Have the interledger open.
I'm going to create a class for this purpose.
Class name is going to be numeric stream aggregate example.
Make this class executable by adding the public static void main method.
And in here, I'm going to use the string.
Before that, let me put this in presentation mode.
I'm going to use in stream dot range closed on comma 50 and I'm going to perform the summation of all
the values in this range.
How do I do it?
So there is a handy method, which is part of the.
Human extreme itself.
We take a look at it.
There is a method called some and some.
Let's print this value and check the result.
Dummies.
And then let's print it.
Go ahead and execute this.
There you go.
It printed the value as 127, five.
Now I want to print the max value of this one.
Right.
So I'm going to again call this one and then call the max value.
There is a method called Max, which is going to return what is a maximum value in this range.
But we know the result.
The answer is going to be 50.
But let's go ahead and check.
What is that?
It is returning.
It is returning something called optional.
And what does it mean?
So if the stream is empty, right.
In that case, it is going to return this optional object with the empty value.
That's what it means.
So we touched on optional a bit in the previous tutorials, but don't worry about it.
We'll be having a separate section for Optional.
We'll discuss all the methods it supports in that particular section.
Know this is going to return something called optional.
INT optional.
INT I'm going to give it a name.
There you go.
So.
We are going to print this one.
Let me print this one.
Then see what it has.
The max value was.
Then give it a colon.
Let's run this.
Run this.
It is giving a max value of 50.
That is what is expected.
But what is our advantage?
Right?
So what is present?
Is present.
And I'm going to run this.
We take a look at it, the value is true.
So the advantage here is that if there is no stream value, if the stream is empty in those kind of
cases, this present is going to be false.
And then you can call if it is present, let's say we can print it like this then optional in dot,
get as int, get as int is a method which will give you the actual int value.
Otherwise the value can be zero.
Run this.
There you go.
It printed the value as false.
If the range is nothing.
So let's write out.
And it's still going to give you some as the value is zero because I believe it creates a range of zero.
Let's go ahead and check that to.
It was a out.
Dot count.
If it prints zero, then it means so.
It printed the value as one.
It still created a range, the value as zero.
So if this dream is empty, then the value will be returned as false.
In the optional event Dot is present.
Let's go ahead and explore the next aggregate function which is the min.
I'm going to use long stream instead of the end stream.
I'm going to use a range closed.
Give the value as one 200 or give the value as 5200 and I'm going to calculate the min value.
We go to this min method.
It is going to return optional long but is something we have to keep in our mind.
So whenever you perform Max and Min operation, it is going to return the optional value.
So it's going to be optional long.
Then give it a name optional long.
I'm going to use the same concept here.
Optional long dot is present, then get the value as get as long and this is going to be zero.
And this the value that I'm expecting the result that it should print would be 50.
There you go.
It printed the value as.
So this is all about the min method.
Let's go ahead and explore the.
Average aggregate function when use in stream again.
Stream dot range closed.
I'm going to give the value as one comma 50.
Then I'm going to perform average.
Go ahead and take a look at the average.
Average is going to return something called optional double.
That is something you have to keep in your mind.
Optional double.
Let's import this.
Want to use the same logic here.
The optional double is present, then I will be get as double.
And then if it is, the stream is empty.
It is going to return the yeast present with the false value.
In that case, we'll print the value as zero.
And this.
There you go.
Enter the value as 25.5.
Basically, that is the average for 1 to 50.
So this is all about the different aggregate functions that int double and the long stream supports.
With this we came to the end of this tutorial.
Thank you for watching.