Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will explore the numeric streams.
What are numeric streams in the first place?
Numeric streams are used to represent the primitive values in a stream.
There are three different types.
One is a stream, long stream and double stream.
I would like to explain the advantages of numeric stream over the regular stream.
Using an example, let's go back to the IntelliJ and code this one.
Then here what I'm going to do, the first step is I'm going to create a package called.
Numeric streams.
And then I'm going to create a class called.
Numeric.
Reims example.
Make this class executable by adding public static void main method.
So the use case that we are going to code, we are going to perform sum of N numbers.
The first step is I'm going to build the input list.
List of integer.
Let's import this one.
And then.
Start an integer list.
Its name.
This one as arrays, dot as list.
Basically, we are going to create a integer list.
I'm going to provide the input values.
There you go.
We have the input list ready.
I'm going to create a method here.
The method is going to perform sum of N numbers and return the sum as a result.
Some of.
In numbers.
And it is going to take in this input.
List of integers.
As a method parameter.
And in here we are going to perform the summation.
Here we are going to use the reduce function, which is part of the streams API.
The first step is we are going to create the stream and then let's use the reduce method.
The reduce method is going to take in a default value, meaning the initial value.
And then we are going to perform sum of two numbers.
Then X plus Y.
Then I'm going to return this method.
For a keyboard return.
Here.
Let's print the result and then check the result.
Sum of numbers.
And here we are going to.
All this method and pass the integer list as an input.
Let's run this and validate the result.
Tony's run the program.
It gave me the result as sum of N numbers, so we got the desired result.
But there is a problem with this code.
Actually, if you take a look at it, we are passing a list of integer.
Integer is a wrapper class for the int primitive type.
So what happens behind the scenes?
Basically under the hood it performs the unboxing operation to convert the integer to int.
So there is some wasted effort here.
So every time it the stream passes an element it is going to perform unboxing of that integer, converting
this integer wrapper class to a int primitive type and then perform the summation and return the result.
So how can we avoid this?
That's where the end stream comes into picture.
I'm going to create a method called public static int.
Some of.
In numbers.
INT string.
So end stream as a handy method called in stream.
There you go.
It is part of the java.util dot stream package dot, and then it has a method called range closed.
So we will cover this range closed and ranges as a separate tutorial in the next tutorial.
But for now you can use this range closed.
Basically this is going to give you one, two, three, four, five, six is going to give you six integers.
And then we are going to perform a sub method.
It has a lot of other methods.
We'll be exploring all the different methods.
The following tutorials.
For now, our use case is going to be we are going to perform the sum of N numbers.
And I'm going to return this.
And if you take a look at it, one more thing here.
We had to call the stream method.
But in here we did not call the stream method.
The reason being if you go to this range closed, the return type of this range closed is in stream.
That is something which we have to keep in our mind.
It already creates a stream and gives you the stream and this is going to pass the values one by one,
one to.
Three.
For.
Five and six.
The Sun mother is going to get the values one by one and then perform the accumulation of the result
and return the result as a output.
I'm of in numbers.
Using end.
Bring.
So.
Dream.
I don't have to pass any input to that method.
It is just going to be an empty method, meaning empty input method.
Let's run this and check the result.
There you go.
It printed the same result.
So under scenarios where you are going to perform number related operations, then you can make use
of the numeric streams instead of the regular streams.
With this, we came to the end of this tutorial.
Thank you for watching.