WEBVTT

00:00.350 --> 00:00.980
Hi everyone.

00:00.980 --> 00:02.360
Welcome back to this tutorial.

00:02.360 --> 00:06.710
In this tutorial I will be explaining about how Stream API works.

00:06.710 --> 00:12.770
I would say this is one of the important tutorial in which I will be covering the Fundamentals of Streams

00:12.770 --> 00:13.520
API.

00:13.520 --> 00:18.920
I would highly recommend you guys to pay more attention to this content before we get into the internals

00:18.920 --> 00:24.470
of the Stream API, let's run the example which we coded in the previous tutorial and then find the

00:24.470 --> 00:25.190
result.

00:26.770 --> 00:30.400
So this is an example which we coded in the previous tutorial.

00:30.400 --> 00:34.090
I'm going to run this again and then validate the result.

00:34.090 --> 00:39.310
The result is having a hashmap with a string as a key, which is a name, and the list of activities

00:39.310 --> 00:41.800
as a value which is of type array list.

00:42.070 --> 00:42.850
That's good.

00:42.850 --> 00:49.630
Let's discuss about what happens after the completion of each and every step before the output is bundled

00:49.630 --> 00:50.920
into the map.

00:50.920 --> 00:54.880
Here we have to filter operations and then the collect operations.

00:54.880 --> 01:00.580
So what happens after the completion of each and every operation?

01:00.610 --> 01:04.120
That is something which I'm going to explain it using the presentation.

01:04.150 --> 01:06.010
Let us go back to the presentation.

01:06.010 --> 01:06.850
There you go.

01:10.030 --> 01:10.780
In this slide.

01:10.780 --> 01:16.810
I have the stream operations mentioned in the left side, and then the result of each and every operation

01:16.810 --> 01:21.160
is mentioned in the right side under the output column.

01:21.280 --> 01:28.960
All these operations together form a pipeline called the Stream pipeline, which starts from number

01:28.960 --> 01:30.880
one to number four.

01:31.610 --> 01:35.810
Each and every operation in this returns a string.

01:37.140 --> 01:43.050
There are two more definitions here, which are intermediate operations and terminal operations.

01:44.530 --> 01:50.830
The stream operations that happens between the stream method and the collect method are called intermediate

01:50.830 --> 01:51.970
operations.

01:53.750 --> 01:57.830
Here we just have the filter method in general for streams.

01:58.250 --> 02:03.650
It is not restricted to have just the filter method as intermediate operations.

02:03.680 --> 02:09.800
There are many different intermediate operations which I will be covering in the future tutorials.

02:10.850 --> 02:13.580
The next one is a terminal operation here.

02:13.580 --> 02:17.270
The terminal operations that we have covered is a collect.

02:18.290 --> 02:21.870
Again, collect is not the only terminal operations.

02:21.890 --> 02:27.590
There are many different terminal operations which I will be covering in the upcoming tutorials.

02:27.890 --> 02:33.200
For now, you just need to have an understanding of the definition of what is an intermediate operation

02:33.200 --> 02:35.570
and what is a terminal operation.

02:35.600 --> 02:39.080
Now let's go through the operations one by one.

02:39.830 --> 02:40.910
First operation.

02:40.910 --> 02:48.560
Here we are invoking a stream which creates a stream of students, which is nothing but a sequence of

02:48.560 --> 02:49.970
students, as you see here.

02:50.510 --> 02:58.310
The next operation in the pipeline is the filter method, which will perform the filtering as per the

02:58.550 --> 03:00.470
condition that we have provided here.

03:00.500 --> 03:02.960
Here we are performing the students.

03:03.980 --> 03:06.660
Grade level is greater than or equal to three.

03:06.680 --> 03:12.650
So here, from this step to this step, it will be passing the students one by one and the filter will

03:12.650 --> 03:14.450
be applied on top of it.

03:15.020 --> 03:21.200
If the student matches that filter, if this returns the boolean as true, then it will go to the next

03:21.200 --> 03:21.680
step.

03:23.000 --> 03:27.380
So after that we are performing again the filter operation.

03:27.380 --> 03:29.060
So it is similar to this step.

03:29.090 --> 03:31.400
It is going to get the sequence of students here.

03:31.400 --> 03:36.740
Basically, if you take a look at it in each and every step, we are getting the stream of students

03:36.740 --> 03:40.940
as an output of every operation in here.

03:40.970 --> 03:44.660
As I mentioned before, we are performing the filter operation.

03:44.660 --> 03:49.190
Again, there is no limit on the number of operations you can have.

03:49.220 --> 03:52.220
You can have n number of operations in here.

03:52.250 --> 03:58.190
The collect method is a one which converts the stream to the final desired output, which we are looking

03:58.190 --> 03:59.270
for here.

03:59.270 --> 04:04.040
We have used collectors dot to map which converts a stream to map.

04:04.040 --> 04:06.720
There are many variations available in this one too.

04:06.860 --> 04:12.770
You can have collectors dot to list or collectors dot to set, which I will be covering as part of the

04:12.770 --> 04:14.240
future tutorials.

04:14.690 --> 04:21.170
One key thing I would like to highlight here is that collect is the one which starts the complete process,

04:21.200 --> 04:24.570
meaning this is the one which starts the pipeline.

04:24.570 --> 04:31.350
If the collect method wasn't there, then there is no way this pipeline would have started and it wouldn't

04:31.350 --> 04:33.810
have created the output.

04:33.870 --> 04:37.710
All of the stream operation above won't happen at all.

04:37.710 --> 04:39.360
Let's go ahead and check this one.

04:39.360 --> 04:42.810
I'm going to go back to the example that we have coded in the previous tutorial.

04:43.550 --> 04:44.460
Back to IntelliJ.

04:44.640 --> 04:49.860
So in here, if I don't call this right, nothing will happen.

04:49.890 --> 04:51.300
Let's go ahead and check that.

04:51.450 --> 04:53.580
So it's going to give me all these errors.

04:53.580 --> 04:55.620
I'm going to comment this whole thing.

04:56.480 --> 05:01.790
And then what I'm going to do is I'm just going to copy until this line and then put it here.

05:03.090 --> 05:04.510
I'm going to run this one, too.

05:04.530 --> 05:09.720
So here, if I run the program, right, none of these operations would happen.

05:09.750 --> 05:10.530
I'm going to run it.

05:12.460 --> 05:12.940
So here.

05:12.940 --> 05:13.480
There you go.

05:13.480 --> 05:13.810
You see?

05:13.840 --> 05:14.380
Right.

05:14.410 --> 05:17.170
It did not start the stream process.

05:17.200 --> 05:20.470
It did not pass the students to this filter method.

05:20.470 --> 05:21.400
Nothing happens.

05:21.400 --> 05:24.490
Collect method is a one which starts the whole process.

05:24.490 --> 05:28.090
That is something which we have to register in your mind.

05:28.870 --> 05:33.870
So another important thing that we have learned here is that streams are lazy.

05:33.880 --> 05:40.300
No intermediate operations will be invoked until the terminal operation is invoked.

05:40.960 --> 05:48.380
So I'm going to remove the code, whatever we commented, and then let's write these things.

05:48.400 --> 05:53.410
So this one is going to give me a stream of students.

05:56.480 --> 05:57.410
And then.

05:59.810 --> 06:05.000
This one is again going to return stream of students, and this is the one which is going to give us

06:05.000 --> 06:06.620
the desired result.

06:06.890 --> 06:11.060
So type map, because that's what we are asking here.

06:11.960 --> 06:16.910
But this is all about Streams API and its internal working.

06:16.940 --> 06:21.710
Let's talk about some more features of the Streams API in the following tutorials.

06:21.740 --> 06:24.000
With this we came to the end of this tutorial.

06:24.020 --> 06:25.070
Thank you so much.
