Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the predicate example using the student collection that we
have as part of this project.
The first step is I'm going to create a class called.
Predicate.
Student example.
And the next step is I'm going to make this class executable.
The way to make this class executable is by adding the public static void main method.
There you go.
We have the class ready.
The next step is we are going to implement the predicate functional interface using lambda expression
for the object student.
Predicate and the input that it is going to accept is student.
If you remember from the previous example in this example, the input was integer.
Here we are going to.
Implement the predicate interface for student.
Want to name it as P1 can name it in any name you want.
So the input is going to be student.
And then what is this method is going to do?
So this is going to return true if the grade level of the student is equal to is greater than or equal
to three.
So I'm basically recreating the same example that we did for our consumer example.
You take a look at it, we are checking the grade level is greater than or equal to three.
I'm trying to implement the same thing in predicate student example.
There you go.
We have the predicate ready.
The next step is let's go ahead and implement this.
I'm going to create a method called.
Public.
Static void.
And then.
Filter.
Student.
By.
Grade level.
That's the method I'm going to.
First thing is we need the student collection.
So we know.
There is a student database.
That get all student.
This returns a list of student.
Britain's best.
We have everything ready.
Right.
What is the next step?
The next step is we need to iterate each and every student.
Right.
I'm going to make.
Which of the.
Porridge method for that.
So.
Student list Dot Burrage.
Each method takes in a consumer interface.
We all know that it's going to take a student, and then here we are going to implement our.
Lambda body.
The body of this particular lambda expression.
So here the first thing is we are going to use.
Predicate and we are going to pass all the test method for this test method, we are going to pass the
student.
And here it is.
Complaining V1 is not static.
Right?
We are trying to use a variable.
One to the static method, but.
Cricket is not a static, so I'm going to make this static.
There you go.
The compilation issue is gone.
The next step is if P1 dot test student then.
And all the students.
So we have written an inline Lambda consumer interface that uses the predicate functional interface.
Right now what I'm going to do, I'm going to call this method.
So we all know this student database has six students, two of second grade students, two of the third
grade students and two of the fourth grade students.
Now, if we take a look at the filter, the filter that we have here is.
Grade level is greater than or equal to three.
Then in that case, the students who are lesser than three will be.
Ignored as part of this condition.
I'm gonna run this.
There will be four students who will be printed in the console.
There you go.
We have all the four students printed here.
The next step is you want to add one more condition, right?
So in the consumer example, what we did was we add all those conditions in here.
Now what I'm going to do, I'm going to create another.
Pretty good instance.
And in here I'm going to set the get GPA levels greater than or equal to three point.
And I'm going to use a same example condition here.
Greater than or equal to 3.9.
Close this.
So I'm going to create another method.
Filter runs by.
EP.
Okay.
And then here what I'm going to do, I'm going to use P to.
Doctors.
So here we are, not considering the grade level.
We are taking the level is greater than or equal to 3.9.
So it is going to check all the grades, students, second grade, third grade and fourth grade students.
And it is going to apply.
The filter of level is greater than or equal to 3.9.
I'm going to call this method.
And run this.
Harder to sought in order to differentiate the results between this method and this method.
There you go.
So if you take a look at this one, applied the filter of the grade level.
So the result, the number of results are of four count.
Take a look at this one.
We are using the level, the students or of the six, only three students have the level equal to or
greater than 3.9.
That's the reason why you see three results.
Now we want to add one more condition.
Meaning I want to filter the students who are at the grade level three and whose GPAs 3.9 if.
We do not have these predicates created within the head.
In that case, what we will do is like what we did in here.
We'll be adding all those conditions inside the method, right?
So here, since we have all these conditions already predefined, what I'm going to do, I'm going to
create another method.
Public static.
White filter students.
But this method is going to be a common method.
You can add any number of conditions to it.
Let's say in here.
P one dot.
And.
He to doctors.
So basically.
And reuse the code.
So Lambda always embraces a code reusability.
So you define the condition at the top.
It's not only in this class.
You can use the same condition throughout your project.
If you define all these different conditions in a common code which can be shared across all the code,
if you have a condition like that, if you have a class which takes care of maintaining all the conditions
and all the filter levels can use it throughout the project so that you can avoid the code duplication.
Eventually you can reuse the code as much as you can in your code base.
Now what I'm going to do, I'm going to call this filter students class.
We printed.
Now you are changing your mind.
Okay.
I don't want, uh, both the conditions to be applicable.
If there is only one condition which matches this, then I want those students to be printed.
In that case, the changes are really, really simple.
If you take a look at it, I just change this condition to R and it is going to give me the result that
I want.
Gave me the result that I want.
And then you don't want to print any of the students who matches this condition.
In that case, it is a negate because the negate.
Not going to print anything.
Those kind of scenarios.
You don't want want to print only the students who doesn't satisfy this condition.
So in those cases, we will introduce the else loop.
This is going to print the students, whoever is not matching that result.
Here it printed all the students.
Who is not matching that condition?
It is clearly evident that Lambda expressions embrace the code reusability.
Let's check how we could have implemented this if we had not used lambdas.
We have to hardcode the condition first.
Right?
So we are using the.
Grade level in this method.
So you would have put that condition here.
And then we are using the filter in this method.
So we would have put this condition here in this method, we need both of these conditions.
So in that case, we could have put both of these conditions in here.
So this leads to code duplication.
Or you could have put this common logic in a utility class.
And inside the method you'll be having all these conditions.
You could have referenced that logic from this class, but with Lambda it simplifies all the logic and
all the logic.
It is assigned to an variable and passed across the project just like any other variable.
Eventually what this does is that it helps building the projects really faster and embraces the code
reusability.
With this, we came to the end of this tutorial.
Thank you for watching.