Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will test our knowledge by building the same use case of printing the name and
the list of activities.
By combining the predicate and consumer functional interface we have been coding so far.
Let's go ahead and code this one.
The first step is I'm going to create a class called predicate and consumer example.
Sounds good.
The next step is.
I'm going to make this class executable by adding the public static void main method.
Now what I'm going to do.
Until now, we have been creating static methods, right?
In this example, we are going to create an instance method.
Why are we changing now?
The main reason why I would like to do it via instance method is to highlight that lambda behaves the
same irrespective of an instance instance method or a static method.
Gonna name this one as public void.
Print name and.
Activities.
This is what I would like to call.
Okay, now we have our method ready.
The next step is we'll create our consumer.
Consumer.
And this consumer is basically going to iterate.
I import the consumer.
Have the consumer instance ready.
Now let's go ahead and implement it.
This is going to accept the student.
And what it is going to do is it is going to.
Perform the filtering operation.
Basically, we are going to now create the instance of predicate.
So the predicate instance is going to be for the grade level and the level.
I'm going to create.
Indicate.
Student.
For this.
What to name this one as P, and then we are going to get the grade level of the student right.
Student.
Get the student first.
Dot get the grade level greater than or equal to three.
Have this ready.
Next, let's create the P2.
The P2 is going to be get GPA greater than or equal to 3.9.
Okay.
Sounds good.
There we go.
Now we are going to use this.
If be one.
And.
To.
God, just.
As the student.
Right.
The next step is we are going to print the name and the list of activities.
Right.
For that, I'm going to create a bi consumer.
Because we can achieve that using the single by consumer itself.
So we are going to print the name, which is of type string and the activities which is of type list
of string.
Okay, let's import the list.
I'm using the option and enter Target.
You can see all the shortcut that I'm using here.
It will be printed over the window.
And I'm going to name this one as student by consumer equal to.
I'll get the name and activities.
We don't have to provide the type because Lambda is smart enough to determine the type by looking at
the by consumer type that is defined as part of it.
And then the next step is system dot out dot print.
Ln name we're going to have a colon in between plus the activities.
So we have the bi consumer also ready.
So we are going to do by consumer dot accept.
Let's pass the.
It.
Not yet.
Name.
And.
It's going to accept two parameters because it's a bi consumer and.
Activities.
And I'm going to give a semicolon here.
Now we have the functional interfaces implementations ready.
Now the next step is we have to call this method, right?
I'm going to call this method.
But prior to that, I'm going to get the list of students.
The students going to be a student list?
I'm going to call the student database dot, get all students, which will give me the list of student.
And the next step is I'm going to pass.
The new predicate and consumer example dot print name and activities.
Right now this print name and activities method doesn't accept any parameters.
I'm going to pass a.
Partial the list of student as part of it.
Print list.
We're getting this.
The next step is I'm going to call students dot rich.
Now we have this student consumer ready.
Just call this dot Accept.
We don't even have to pass anything because it is already iterating the for each method is going to
iterate and pass the shouldn't already.
Now this shouldn't consumer is going to get the shouldn't and perform all the test operation.
Basically it is going to apply.
The grade level is greater than three or the grade and the level is greater than 3.9.
And then it is going to use the by consumer in order to print the name and activities in the console.
Let's go ahead and run this.
There you go.
It printed the name and its respective activities.
The advantage with this kind of code is completely modularized and it is really easy to modify the functionality
if something else needs to be changed or added to the code.
With this, we came to the end of this tutorial.
Thank you for watching.