Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the limit and skip function using the streams API.
These two functions helps to create a sub stream.
Basically, if you take a look at the limit, it takes in an input.
If you pass the input as five, let's say you have a list of ten elements and then you are passing the
input value for the limit function as five.
Basically what it is going to do, it is just going to perform any kind of operation on the first five
elements.
Even though you have the stream which has ten elements in it.
Let's talk about Skip.
Skip skips the N number of elements from the stream.
So what it is going to do, let's say you have a stream which has ten elements and then you provide
the value as a skip of three.
What it is going to do, it is going to skip the first three elements and process only the remaining
seven elements.
Let's go ahead and code this one.
So we want to create a class for this purpose.
Going to be streams limit skip example.
I'm going to make this class executable by adding public static void main.
Let's put this in presentation mode.
And what we are going to do.
Builder input list first.
List of integers.
Want to keep it simple.
My list is going to hold five elements.
I'm going to use a same set of input that we used in the previous tutorial.
Arrays dot as list of six, seven, eight, nine and ten.
Okay.
Now the first step is we will code and explore the limit function as part of the streams API.
Want to use the same reduce operation.
What I'm going to do, I'm going to perform the summation of part of the stream from this list.
I'm going to use the same reduce operation.
So.
When to pass the return the value as integer.
Here it's going to be limit.
For thus going to accept the list of integers as an input.
In here.
Let's create a stream first dot stream, and then I'm going to use a limit.
So if I pass the value as two.
Write what it is going to do is it is just going to consider these two elements and then perform the
reduce operation.
So reduce.
I'm not going to assign a default values.
It's going to be X comma, Y.
Return A plus B write basically X plus Y.
And then I'm going to return this.
So here the stream is going to.
Six, seven, eight.
Nine and ten.
Since we have the limit function here, it is going to just restrict only these two elements passed
to the reduce function and then return the result.
Go ahead and check the result.
The result is going to be optional of integer and it's going to be limit result.
Equal to the MIT.
And pass the integers.
Right.
So if.
Limit result Dot is present.
Then.
The short.
The limit result is.
Going to be limit result dot.
And else.
Going to be.
No input is past.
Okay.
Let's go ahead and run this.
So let's revisit this code again.
Here we are passing a stream.
We know the stream is going to have five elements and we are using the limit function to limit the stream
to just process the two elements in this in the string.
So let's go ahead and run this check the result.
There we go.
The result is 13.
If I change this value to three.
It is going to.
Perform the summation of these three, then the result is going to be 21.
So this is a benefit of a limit.
So if you have a use case where you have a stream of like 1000 elements and you do not want to process
all the thousand elements to get your result, in that case, you can limit the number of elements you
want from the actual stream using the limit function.
Now what I'm going to do, I'm going to create the skip function.
Basically, we are going to explore the skip function.
I'm going to copy this and then put it here.
Name this one as Skip.
In the place of limit.
I'm going to use a skip.
What the skip is going to do.
So in here, I'd like to write this since we are using three, it's going to pass.
Six, seven, eight.
Okay, so here we have the value of Skip S3.
Basically, it is going to skip six, seven, eight.
The first three elements will be skipped and then it is going to pass only the nine and ten.
So run this.
Gonna name this one as skip result.
It features in Brazil's dot is present a result dot get.
And this skip.
Now let's revisit this code again.
So the stream has five elements and then we are using the skip function and we are providing a value.
So this is a value which is a key to process only the remaining two elements and skip the first three
elements in the stream.
So the result that we are expecting is going to be 19.
Let's go ahead and run this to avoid some confusion.
I'm going to comment this piece of the logic which has the limit result in it.
And thus.
If you run this, the result is going to be 19.
If I change this value to two.
Going to just skip the first two elements and the result is going to be 19 plus eight, which is going
to be 27.
You go put it back.
You.
The skip function can be used if you want to skip a few elements from the stream and process the remaining
elements in the stream.
This is all about a limit.
And skip with this, we came to the end of this tutorial.
Thank you for watching.