Hi everyone.
Welcome back to this tutorial.
In this tutorial I will be explaining about how Stream API works.
I would say this is one of the important tutorial in which I will be covering the Fundamentals of Streams
API.
I would highly recommend you guys to pay more attention to this content before we get into the internals
of the Stream API, let's run the example which we coded in the previous tutorial and then find the
result.
So this is an example which we coded in the previous tutorial.
I'm going to run this again and then validate the result.
The result is having a hashmap with a string as a key, which is a name, and the list of activities
as a value which is of type array list.
That's good.
Let's discuss about what happens after the completion of each and every step before the output is bundled
into the map.
Here we have to filter operations and then the collect operations.
So what happens after the completion of each and every operation?
That is something which I'm going to explain it using the presentation.
Let us go back to the presentation.
There you go.
In this slide.
I have the stream operations mentioned in the left side, and then the result of each and every operation
is mentioned in the right side under the output column.
All these operations together form a pipeline called the Stream pipeline, which starts from number
one to number four.
Each and every operation in this returns a string.
There are two more definitions here, which are intermediate operations and terminal operations.
The stream operations that happens between the stream method and the collect method are called intermediate
operations.
Here we just have the filter method in general for streams.
It is not restricted to have just the filter method as intermediate operations.
There are many different intermediate operations which I will be covering in the future tutorials.
The next one is a terminal operation here.
The terminal operations that we have covered is a collect.
Again, collect is not the only terminal operations.
There are many different terminal operations which I will be covering in the upcoming tutorials.
For now, you just need to have an understanding of the definition of what is an intermediate operation
and what is a terminal operation.
Now let's go through the operations one by one.
First operation.
Here we are invoking a stream which creates a stream of students, which is nothing but a sequence of
students, as you see here.
The next operation in the pipeline is the filter method, which will perform the filtering as per the
condition that we have provided here.
Here we are performing the students.
Grade level is greater than or equal to three.
So here, from this step to this step, it will be passing the students one by one and the filter will
be applied on top of it.
If the student matches that filter, if this returns the boolean as true, then it will go to the next
step.
So after that we are performing again the filter operation.
So it is similar to this step.
It is going to get the sequence of students here.
Basically, if you take a look at it in each and every step, we are getting the stream of students
as an output of every operation in here.
As I mentioned before, we are performing the filter operation.
Again, there is no limit on the number of operations you can have.
You can have n number of operations in here.
The collect method is a one which converts the stream to the final desired output, which we are looking
for here.
We have used collectors dot to map which converts a stream to map.
There are many variations available in this one too.
You can have collectors dot to list or collectors dot to set, which I will be covering as part of the
future tutorials.
One key thing I would like to highlight here is that collect is the one which starts the complete process,
meaning this is the one which starts the pipeline.
If the collect method wasn't there, then there is no way this pipeline would have started and it wouldn't
have created the output.
All of the stream operation above won't happen at all.
Let's go ahead and check this one.
I'm going to go back to the example that we have coded in the previous tutorial.
Back to IntelliJ.
So in here, if I don't call this right, nothing will happen.
Let's go ahead and check that.
So it's going to give me all these errors.
I'm going to comment this whole thing.
And then what I'm going to do is I'm just going to copy until this line and then put it here.
I'm going to run this one, too.
So here, if I run the program, right, none of these operations would happen.
I'm going to run it.
So here.
There you go.
You see?
Right.
It did not start the stream process.
It did not pass the students to this filter method.
Nothing happens.
Collect method is a one which starts the whole process.
That is something which we have to register in your mind.
So another important thing that we have learned here is that streams are lazy.
No intermediate operations will be invoked until the terminal operation is invoked.
So I'm going to remove the code, whatever we commented, and then let's write these things.
So this one is going to give me a stream of students.
And then.
This one is again going to return stream of students, and this is the one which is going to give us
the desired result.
So type map, because that's what we are asking here.
But this is all about Streams API and its internal working.
Let's talk about some more features of the Streams API in the following tutorials.
With this we came to the end of this tutorial.
Thank you so much.