Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the predicate functional interface.
Let's go ahead and check the abstract method and the other default methods.
This predicate functional interface supports press the shift key twice and then I'm going to search
for predicate.
So this is the predicate interface that we are going to focus on.
Click on this and then this predicate interface is part of the Java.util dot function package.
Take a look at the methods it has.
This is Boolean test and it accepts a input.
So this is an abstract method which we are going to implement as part of the lambda expression.
So this test method accepts an input and it is going to perform some kind of operation on that input
and then it is going to return a Boolean.
That's what predicate is going to do.
And if you take a look at it, it supports some other default methods which are like an operation or
operation.
And negate Operation Negate is basically it is going to reverse the result of the expression that we
are going to implement as part of the boolean, as part of the Lambda expression.
And the next is the is equal method.
What we will do.
Let's go some simple use case and then explore all the different methods this predicate functional interface
supports.
I'm going to create a class called.
Predicate example.
And then let's make this class executable by adding public static void main.
In here, I'm going to create the first predicate.
Functional interface implementation predicate and what the use case that I'm going to do is that, okay,
I'm going to pass an integer check whether that integer is an even number.
That's a use case that we are going to code here.
So it is going to accept an integer.
And then what I'm going to do, I'm going to give a name.
So I'm going to assign a variable because it is accepting an input.
Followed by that, I'm going to open and close the curly braces.
This is where I'm going to give the lambda body.
And it is going to return.
So the use cases, we are going to check whether the past number is even or not.
A modulo two.
Equal to equal to zero.
So we have the lambda expression for the predicate interface ready.
Let's pass some inputs and then test.
Dot p dot test.
I'm going to pass the value as four or is an even number.
Right?
Let's go ahead and run this.
It returned to.
So you might be wondering.
I can do the same operation without the predicate functional interface.
Right.
What's the benefit in here?
The benefit is a code reusability, which we will cover in a bit.
You can reuse the same predicate functional interface n number of times throughout your project.
That was not possible prior to this.
Now, if you take a look at this expression, this is really a simple lambda expression which has one
single statement.
So if you remember or if you recall from our prior tutorials, if the Lambda body is a single statement
or an expression, then we don't even need the curly braces and the return statement.
Or can we rewrite this?
I'm going to rewrite this.
Navy E one.
So I'm going to remove the curly braces.
If you remove the curly braces, you don't even have to use the return keyword.
The syntax is applicable only for single line statement or simple expressions.
Let's go ahead and check this.
The expectation is that both are going to print the same result.
Run this.
We're going to use P one, not P.
There you go.
Both printed the same result.
So we have successfully implemented the predicate functional interface using Lambda expression.
Now let's build this use case to use the other default methods which are part of the functional interface
predicate you're going to use and negate and are.
I'm going to move this to the top so that we can reuse throughout this class.
There we go.
And then I'm going to mark this one as static.
Run this to make sure this is working as expected.
Let's create a method called predicate and.
Public static.
Wired.
The name is going to be decayed.
So we are going to explore the and method as part of the predicate functional interface.
What I'm going to do is I'm going to introduce one more predicate interface, and this is going to check
if the number is divisible by five or not.
Okay.
So in here, what we are going to do, we are going to have the code as.
Doubt we are going to have p one dot you're going to use the and method P two.
You can have n number of P one or n number of predicate interfaces as part of this chain.
Again, as I mentioned, as part of the consumer interface class, this is called predicate.
Painting in here.
What we are going to do, we are going to pass the input using the test method.
You should always use the test method in order to pass the input.
I'm going to pass the input as ten.
Right.
So ten is a number which is divisible by two and it is also divisible by five.
Now what we are going to do, we are going to pass this input.
For this method.
Are we going to call this method?
This method is eventually going to call this one.
The predicate and result is.
So what will be the result?
The result should be true because we're passing a value ten which is divisible by five and also divisible
by two.
Node and then run this.
There you go.
Predicate.
And the result is.
Now what I'm going to do, I'm going to pass the value as nine.
So we are going to have one more result in the console with the value as the result as false.
As expected.
Let's go ahead and check the operation.
I'm going to copy this and then put it here.
Is going to be predicate are in the place of this we are going to use are.
I'm going to replace this one.
Two are.
Indicate our results Indicate our results.
Take a look at it.
Indicate are.
Candice.
Now, let's go ahead and run this right and then check what is the result the result is going to be.
True in both the cases now for true and false in both the cases.
But this one is not divisible by two and also not divisible by five.
Now, what I'm going to do, I'm going to change this value to eight right in here.
Eight is divisible by two, but not divisible by five.
Let's run this and check.
The result is going to be true and true for the AR operation.
As you see here, the result is true and true because our operation is equal to if one of the operation
is one of the result of the operation is true, then it is going to evaluate the result to.
Now what we will do, we will explore the negate method.
Going to be dedicate negate.
Negate is a method which will reverse the result.
So I'm gonna.
Take this one and then.
Negate.
So in the earlier scenario, this expression evaluated to.
What's right.
So this one evaluated to true because one of the operation is true, then it is going to evaluate the
final result also to true.
Here we are using the negate what what the negate will do is negate is going to reverse the result,
whatever that it is going to evaluate.
So p one dot RP2 is going to evaluate the result to true.
The negate operation basically is going to reverse the result to false.
So the result is going to be false.
Call this method.
I'm going to rename this with the correct value predicate negate.
So I'm going to run this method.
As you see, the predicate negate result is false.
But we take a look at the actual or operation result.
It is true.
So these are the different methods which are part of the predicate interface.
In our next tutorial, we will explore our student database and then use the predicate interface as
part of that student collection and then explore more use cases.
With this, we came to the end of this tutorial.
Thank you for watching.