Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the numeric stream ranges.
The first one is in stream, which supports two methods.
One is a range and another one is a range closed.
Both takes into input.
One is the starting value of the stream and another one is the ending value of the stream.
The difference is that the range method excludes the last element that is being passed as a second argument
for this method, for this input value one comma 50 it returns an int stream of 49 elements from 1 to
49.
Basically, this will exclude the last element, which is the second argument of this method.
The next one is a range closed method range closed method returns an inter stream of 50 elements.
Basically this is there is no exclusion concept here is going to include the values from 1 to 50.
So similar to the end stream.
The long stream also supports the range and range closed method.
The behavior is the same instead of the stream, it is going to return you the long stream.
So for the double stream it doesn't have both range and range closed.
But there is an alternative approach.
In order to create a double stream.
I'll be showing you that in a moment.
Let's go ahead and code this and explore this.
The numeric stream package.
I'm going to create a class called.
Numeric.
Dream.
Ranges.
Example.
And then.
And add the public static void main method.
This in presentation mode.
So that the code will be clear.
The first step is I'm going to create an inter stream dot range and one comma 50.
So basically, if you take a look at it, this is going to return you an input stream.
So let's give it a name int stream equal to.
Here we go.
We have the stream ready.
Let's check what is the total number of elements it has in stream dot?
Oh.
Ramdas.
It is going to print 49.
Now let's try to do a iteration of it.
Right iteration of.
Each value basically for each takes in a consumer and each value is going to have.
Value.
Plus, I'm going to add a comma here as it will be tough to read.
I'll run this example.
There we go.
We got an exception here because a stream has already been closed.
We all know streams can be iterated only once.
That's the reason why we get this problem.
In that case, I'm going to do it like this.
I'm going to print all the values here.
I'm using this print ln.
I'm going to change this one to print and run this example.
There you go.
It printed the values from 1 to 49.
Right.
Let's go ahead and explore the range closed.
I'm going to copy this and paste it here.
I'm going to be range closed dot for each.
If I do this.
Going to print the values until 40.
I would like to perform the count also here.
This one is going to be.
Found.
Put this inside a suit.
So here we are printing the range closed count.
This is range count.
Range count.
And this one is range.
Closed.
Take a look at the difference.
So the range count printed the value as 49, but the range closed count printed the value as 50 here.
Since we have print it is not printing it in the line.
Add this out.
There you go.
They printed the range closed value as 50 range as 49.
Let's explore this.
But the long stream.
If you are doing a long stream.
Same concept.
Long stream.
And then I'm going to do the same thing here.
So instead of the stream, I'm going to change this one to long string.
Okay.
Then let's print this.
You run it.
So here you have the long stream range count as this that.
Episode here.
So that can be printed in a new line so that it will be easy to read in the console.
So in the console, if you take a look at it, the range closed count is 50 and it printed all the 50
values.
So in the presentation I mentioned, that double stream doesn't have doesn't support the range and range
closed.
Right.
Let's go ahead and explore that.
If you do double stream dot, take a look at it doesn't support the range and range closure, but in
case if you need a double stream, how can we get that?
So the way to get it is.
God.
As there is a method called as long stream and as double stream as double stream is going to give you
all the values converted to double then the for each.
I'm printing this.
There was a sword here.
This, then check the route here.
We take a look at it in the console.
We have 1.02.03.0.
So there is a handy method as double stream, which is part of both in stream and long stream.
Using that, you can create the double stream.
So this is all about range and ranges that you need to know about numeric stream.
With this we came to the end of this tutorial.
Thank you for watching.