WEBVTT

00:01.450 --> 00:01.990
Hi everyone.

00:01.990 --> 00:03.340
Welcome back to this tutorial.

00:03.340 --> 00:10.360
In this tutorial we will code and explore the limit and skip function using the streams API.

00:10.390 --> 00:14.230
These two functions helps to create a sub stream.

00:14.230 --> 00:18.580
Basically, if you take a look at the limit, it takes in an input.

00:19.060 --> 00:26.230
If you pass the input as five, let's say you have a list of ten elements and then you are passing the

00:26.230 --> 00:28.900
input value for the limit function as five.

00:28.990 --> 00:36.580
Basically what it is going to do, it is just going to perform any kind of operation on the first five

00:36.580 --> 00:37.300
elements.

00:37.300 --> 00:41.590
Even though you have the stream which has ten elements in it.

00:42.040 --> 00:43.300
Let's talk about Skip.

00:43.300 --> 00:46.840
Skip skips the N number of elements from the stream.

00:46.840 --> 00:52.570
So what it is going to do, let's say you have a stream which has ten elements and then you provide

00:52.570 --> 00:54.610
the value as a skip of three.

00:54.640 --> 01:01.000
What it is going to do, it is going to skip the first three elements and process only the remaining

01:01.100 --> 01:02.260
seven elements.

01:02.270 --> 01:03.770
Let's go ahead and code this one.

01:05.870 --> 01:09.560
So we want to create a class for this purpose.

01:09.980 --> 01:14.630
Going to be streams limit skip example.

01:16.620 --> 01:20.580
I'm going to make this class executable by adding public static void main.

01:20.700 --> 01:22.500
Let's put this in presentation mode.

01:25.850 --> 01:27.620
And what we are going to do.

01:28.840 --> 01:30.400
Builder input list first.

01:32.270 --> 01:34.040
List of integers.

01:35.040 --> 01:36.410
Want to keep it simple.

01:37.010 --> 01:39.620
My list is going to hold five elements.

01:40.100 --> 01:43.730
I'm going to use a same set of input that we used in the previous tutorial.

01:44.030 --> 01:49.010
Arrays dot as list of six, seven, eight, nine and ten.

01:50.300 --> 01:50.710
Okay.

01:50.720 --> 01:56.360
Now the first step is we will code and explore the limit function as part of the streams API.

01:57.630 --> 01:59.470
Want to use the same reduce operation.

01:59.470 --> 02:05.950
What I'm going to do, I'm going to perform the summation of part of the stream from this list.

02:08.240 --> 02:10.790
I'm going to use the same reduce operation.

02:10.790 --> 02:11.480
So.

02:12.950 --> 02:16.430
When to pass the return the value as integer.

02:16.970 --> 02:18.680
Here it's going to be limit.

02:21.060 --> 02:25.830
For thus going to accept the list of integers as an input.

02:27.680 --> 02:28.430
In here.

02:28.430 --> 02:35.240
Let's create a stream first dot stream, and then I'm going to use a limit.

02:35.420 --> 02:38.300
So if I pass the value as two.

02:39.240 --> 02:45.720
Write what it is going to do is it is just going to consider these two elements and then perform the

02:45.720 --> 02:46.770
reduce operation.

02:47.220 --> 02:48.300
So reduce.

02:49.300 --> 02:51.100
I'm not going to assign a default values.

02:51.100 --> 02:53.830
It's going to be X comma, Y.

02:57.270 --> 03:00.960
Return A plus B write basically X plus Y.

03:03.740 --> 03:05.210
And then I'm going to return this.

03:06.030 --> 03:09.540
So here the stream is going to.

03:12.570 --> 03:15.840
Six, seven, eight.

03:17.070 --> 03:18.450
Nine and ten.

03:18.600 --> 03:24.870
Since we have the limit function here, it is going to just restrict only these two elements passed

03:24.870 --> 03:27.570
to the reduce function and then return the result.

03:28.170 --> 03:29.460
Go ahead and check the result.

03:30.520 --> 03:37.270
The result is going to be optional of integer and it's going to be limit result.

03:39.420 --> 03:41.700
Equal to the MIT.

03:42.800 --> 03:44.120
And pass the integers.

03:44.330 --> 03:45.080
Right.

03:45.270 --> 03:46.520
So if.

03:47.720 --> 03:50.030
Limit result Dot is present.

03:50.870 --> 03:51.470
Then.

03:53.100 --> 03:53.670
The short.

03:55.100 --> 03:58.160
The limit result is.

03:59.240 --> 04:02.390
Going to be limit result dot.

04:04.270 --> 04:05.530
And else.

04:06.910 --> 04:07.600
Going to be.

04:07.630 --> 04:08.730
No input is past.

04:12.110 --> 04:12.380
Okay.

04:13.040 --> 04:14.150
Let's go ahead and run this.

04:15.330 --> 04:17.850
So let's revisit this code again.

04:17.880 --> 04:19.800
Here we are passing a stream.

04:19.800 --> 04:25.890
We know the stream is going to have five elements and we are using the limit function to limit the stream

04:25.890 --> 04:29.310
to just process the two elements in this in the string.

04:29.630 --> 04:32.130
So let's go ahead and run this check the result.

04:32.550 --> 04:33.210
There we go.

04:33.210 --> 04:34.230
The result is 13.

04:34.260 --> 04:36.240
If I change this value to three.

04:37.150 --> 04:38.440
It is going to.

04:40.380 --> 04:44.670
Perform the summation of these three, then the result is going to be 21.

04:44.880 --> 04:47.730
So this is a benefit of a limit.

04:47.880 --> 04:53.460
So if you have a use case where you have a stream of like 1000 elements and you do not want to process

04:53.460 --> 04:59.010
all the thousand elements to get your result, in that case, you can limit the number of elements you

04:59.010 --> 05:02.100
want from the actual stream using the limit function.

05:02.220 --> 05:07.550
Now what I'm going to do, I'm going to create the skip function.

05:07.560 --> 05:09.570
Basically, we are going to explore the skip function.

05:09.570 --> 05:12.270
I'm going to copy this and then put it here.

05:12.930 --> 05:14.250
Name this one as Skip.

05:15.560 --> 05:17.270
In the place of limit.

05:17.270 --> 05:18.380
I'm going to use a skip.

05:18.830 --> 05:20.730
What the skip is going to do.

05:20.750 --> 05:24.980
So in here, I'd like to write this since we are using three, it's going to pass.

05:25.280 --> 05:26.780
Six, seven, eight.

05:28.290 --> 05:32.010
Okay, so here we have the value of Skip S3.

05:32.310 --> 05:35.390
Basically, it is going to skip six, seven, eight.

05:35.400 --> 05:40.170
The first three elements will be skipped and then it is going to pass only the nine and ten.

05:41.340 --> 05:43.140
So run this.

05:45.400 --> 05:48.250
Gonna name this one as skip result.

05:50.240 --> 05:57.020
It features in Brazil's dot is present a result dot get.

05:58.040 --> 05:59.210
And this skip.

06:00.260 --> 06:03.050
Now let's revisit this code again.

06:03.050 --> 06:09.890
So the stream has five elements and then we are using the skip function and we are providing a value.

06:09.890 --> 06:17.930
So this is a value which is a key to process only the remaining two elements and skip the first three

06:17.930 --> 06:18.920
elements in the stream.

06:18.920 --> 06:21.770
So the result that we are expecting is going to be 19.

06:21.800 --> 06:24.590
Let's go ahead and run this to avoid some confusion.

06:24.590 --> 06:29.900
I'm going to comment this piece of the logic which has the limit result in it.

06:32.750 --> 06:33.380
And thus.

06:34.520 --> 06:36.800
If you run this, the result is going to be 19.

06:36.830 --> 06:38.800
If I change this value to two.

06:39.050 --> 06:45.620
Going to just skip the first two elements and the result is going to be 19 plus eight, which is going

06:45.620 --> 06:46.550
to be 27.

06:48.550 --> 06:50.530
You go put it back.

06:51.400 --> 06:51.790
You.

06:53.250 --> 06:58.630
The skip function can be used if you want to skip a few elements from the stream and process the remaining

06:58.630 --> 07:00.460
elements in the stream.

07:01.670 --> 07:03.050
This is all about a limit.

07:03.050 --> 07:05.870
And skip with this, we came to the end of this tutorial.

07:05.900 --> 07:07.010
Thank you for watching.
