Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and learn about the filter function which is part of the streams.
API.
Filter function is used to filter the elements in the stream.
The input to the filter is a predicate functional interface.
We have seen a lot of examples about filter in the previous tutorials.
If you are pretty confident about filter function, you can skip this tutorial and move on to the next
one.
Let's go ahead and code this one.
So I'm going to expand the strings package.
I'm going to create a class called.
Dreams filter.
Example.
And the next step is I'm going to make this class executable by adding public static, void main method.
Let me put this in presentation mode.
Here we go.
The next step is I'm going to create a method called public static.
List of.
Students.
And then the method name is going to be filter students.
Simple this.
Let's import this one to.
Java dot util package.
The first step is I'm going to call the student database dot, get all students.
So this is going to give you the access to the list of students.
And then I'm going to use the stream method.
The start leveraging or start using the stream functions.
And part of the Streams API.
The next step is I'm going to use the filter.
So if we take a look at here, filter takes in a predicate.
Let's implement the predicate.
So each and every element of the stream is going to be passed to this filter function one by one.
So what I'm going to do, I'm going to apply the filter as, okay, let's say I want only the female
students as part of the whole student database.
Get all students method.
So in that case, how can I do that?
So we can achieve that by calling student dot get gender dot equals.
Female.
What it is going to do.
It is going to the stream function is going to pass the students one by one.
If the student is of gender female, then only it will be passed to the next step in the pipeline.
The next step is I'm going to keep this pipeline really simple.
I'm going to do a collect collect of.
List.
Because that's what we are going to do.
We are going to return.
List of students.
So it's going to return the list of students.
So what this one is going to give you, this one is going to give you.
Stream of students.
Student.
And again, this one is also give you a dream of student But here this will filter out.
Filters and.
Sends only the.
Students whose gender is female.
So whoever has a gender as male will not be passed to the next step.
Basically, they will be filtered out from the stream.
Let's print this.
When adults also served here.
And then I'm going to print this.
To hear all this as soon as you run.
This example is going to print the list of students in the console.
There you go.
It printed all the students in order for more readability.
What I'm going to do, I'm going to print it by iterating all the values dot for each.
Then we are going to use the method reference syntax.
Colon.
Println.
There we go.
Run this.
It's going to print all the students in.
Single line.
So if we take a look at it, all the students who ever got printed are female and then it printed in
the order just passed from the screen.
This is all about filter method.
And one more thing if you want, you can add a number of filter here.
So if you want, you can add a filter.
Let's say you want to filter out the student.
Whose grade level is above 3.9.
So you want to do that.
So student dot get GPA.
Now the grade level sorry, it's a GPA is is greater than or equal to 3.9.
So if you do that basically from this list, 3.5 and 3.8, Ginny and Sophia will be filtered out of
the list.
If I run this, it's just going to print Emily.
There you go.
So you can add any number of filter operations here in the pipeline and then you can get your desired
result that you are looking for.
With this, we came to the end of this tutorial.
Thank you for watching.