Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and learn how to debug a stream API and what gets passed between the stream
operations.
I'm going to open the example which we coded in one of the previous tutorial, which is streams example.
Let's click on that.
So if you take a look at this example, we are performing one, two and three stream operations and
I'm going to run this example first and check the result.
The result is the name and the students activities.
That's what we are printing here.
Now we want to see what is being parsed from the stream method to the filter method.
There is a handy method that is available as part of the stream API, which is the called peek.
So if you take a look at the peak, peek accepts a consumer.
We all know consumer by now.
Consumer accepts an input and which doesn't return any output.
So now what I'm going to do, I'm going to build a consumer.
Let's create the lambda.
So have the lambda created and it is going to take student as an input.
As soon as I typed in the student, it gave me this recommendation.
I'm going to press enter.
There you go.
We have the lambda created.
To open the curly braces.
And in here, what I'm going to do.
I'm going to.
Rent the student.
For this in presentation mode, the code will be clear.
There you go.
So we have this pick method and then we have the consumer parsed as part of the pick method, as an
input to the pick method actually.
And then we are printing the student in here.
What I'm going to do, I'm going to run and then check the result.
As soon as you run it.
You see, earlier we were just printing the result, which is a map.
We take a look at it now.
It is printing all the values as you see it is printing all the students which are being parsed as part
of the student.
How many students are there?
One, two, three, four, five, six.
There are six students.
Our collection, our student database, get all students also has six students, as you see here.
Student one to.
Student six That's good.
Now we want to see how many students are getting filtered and then passed to the next level in this
student predicate.
So in this student predicate, basically we have the filter which is equal to greater than or equal
to three.
Let's write the peak method and then see how many students are getting passed from this filter to this
filter.
So I'm going to have.
This one printed as after first filter.
Then I'm going to run this example.
Now let's see how many occurrences of this is printed in the console.
I am expecting for because we have like two students in each grade.
We have two, three and four four grades as part of the.
It all students with it.
We take a look at it.
Second grade.
We have two students, third grade, we have two students and fourth grade.
We have two students.
Basically, with this filter going to just print four students, go ahead and take a look at it.
So here.
One, two, three, four.
It gave me four values in the result.
Meaning it printed it, applied the filter, and then it filtered out two students from this actual
students method which returned you six students.
Now what I'm going to do, I'm going to add this peek method here to and then see here I'm going to
name this one as after second filter.
After the second filter.
Let's see how many students are going to be passed from this level to the collector.
And thus.
Bring it up a little bit.
Search for it.
We have like three matches here.
This is what is happening.
So it is applying the second filter, which eventually resulted in passing three students to the.
Actual collector.
And if you take a look at it, it's not just like the whole collection.
It is in sequence.
So first filter and then the second filter.
First filter and then the second filter.
So it applies the filters to the elements one by one.
Basically stream is parsing the elements.
Stream is parsing the elements from the top to the bottom one by one, and it is applying the filter
on the elements one by one.
So this is the way we can debug and then see what is being passed from one operation to the other operation.
So the method is called the peek method.
With this we came to the end of this tutorial.
Thank you for watching.