WEBVTT

00:00.320 --> 00:00.860
Hi everyone.

00:00.860 --> 00:02.000
Welcome back to this tutorial.

00:02.000 --> 00:06.460
In this tutorial we will code and explore the predicate functional interface.

00:06.470 --> 00:10.520
Let's go ahead and check the abstract method and the other default methods.

00:10.520 --> 00:17.480
This predicate functional interface supports press the shift key twice and then I'm going to search

00:17.480 --> 00:18.860
for predicate.

00:19.310 --> 00:23.000
So this is the predicate interface that we are going to focus on.

00:23.480 --> 00:29.450
Click on this and then this predicate interface is part of the Java.util dot function package.

00:29.990 --> 00:31.880
Take a look at the methods it has.

00:32.280 --> 00:35.480
This is Boolean test and it accepts a input.

00:35.480 --> 00:40.790
So this is an abstract method which we are going to implement as part of the lambda expression.

00:40.790 --> 00:47.360
So this test method accepts an input and it is going to perform some kind of operation on that input

00:47.360 --> 00:49.670
and then it is going to return a Boolean.

00:50.060 --> 00:52.130
That's what predicate is going to do.

00:52.130 --> 00:58.700
And if you take a look at it, it supports some other default methods which are like an operation or

00:58.700 --> 00:59.480
operation.

00:59.480 --> 01:05.760
And negate Operation Negate is basically it is going to reverse the result of the expression that we

01:05.760 --> 01:09.630
are going to implement as part of the boolean, as part of the Lambda expression.

01:10.110 --> 01:12.210
And the next is the is equal method.

01:13.820 --> 01:14.510
What we will do.

01:14.510 --> 01:21.500
Let's go some simple use case and then explore all the different methods this predicate functional interface

01:21.500 --> 01:22.340
supports.

01:23.420 --> 01:25.520
I'm going to create a class called.

01:27.220 --> 01:28.750
Predicate example.

01:31.670 --> 01:36.470
And then let's make this class executable by adding public static void main.

01:36.920 --> 01:41.390
In here, I'm going to create the first predicate.

01:42.230 --> 01:49.040
Functional interface implementation predicate and what the use case that I'm going to do is that, okay,

01:49.040 --> 01:54.050
I'm going to pass an integer check whether that integer is an even number.

01:54.050 --> 01:57.020
That's a use case that we are going to code here.

01:58.360 --> 01:59.960
So it is going to accept an integer.

02:00.830 --> 02:03.830
And then what I'm going to do, I'm going to give a name.

02:05.060 --> 02:08.990
So I'm going to assign a variable because it is accepting an input.

02:10.330 --> 02:15.250
Followed by that, I'm going to open and close the curly braces.

02:15.250 --> 02:17.950
This is where I'm going to give the lambda body.

02:19.130 --> 02:20.930
And it is going to return.

02:22.110 --> 02:26.760
So the use cases, we are going to check whether the past number is even or not.

02:27.610 --> 02:30.190
A modulo two.

02:31.180 --> 02:32.200
Equal to equal to zero.

02:34.150 --> 02:41.470
So we have the lambda expression for the predicate interface ready.

02:41.500 --> 02:44.110
Let's pass some inputs and then test.

02:48.380 --> 02:50.720
Dot p dot test.

02:50.720 --> 02:56.330
I'm going to pass the value as four or is an even number.

02:56.330 --> 02:56.840
Right?

02:56.960 --> 02:58.250
Let's go ahead and run this.

03:00.110 --> 03:01.160
It returned to.

03:02.480 --> 03:03.770
So you might be wondering.

03:03.770 --> 03:08.840
I can do the same operation without the predicate functional interface.

03:08.840 --> 03:09.140
Right.

03:09.170 --> 03:10.490
What's the benefit in here?

03:10.520 --> 03:14.300
The benefit is a code reusability, which we will cover in a bit.

03:14.390 --> 03:20.450
You can reuse the same predicate functional interface n number of times throughout your project.

03:20.840 --> 03:23.090
That was not possible prior to this.

03:23.120 --> 03:31.460
Now, if you take a look at this expression, this is really a simple lambda expression which has one

03:31.460 --> 03:32.430
single statement.

03:32.450 --> 03:39.530
So if you remember or if you recall from our prior tutorials, if the Lambda body is a single statement

03:39.530 --> 03:44.720
or an expression, then we don't even need the curly braces and the return statement.

03:44.840 --> 03:46.190
Or can we rewrite this?

03:47.440 --> 03:49.000
I'm going to rewrite this.

03:50.540 --> 03:52.470
Navy E one.

03:53.220 --> 03:55.070
So I'm going to remove the curly braces.

03:56.260 --> 03:59.740
If you remove the curly braces, you don't even have to use the return keyword.

04:01.040 --> 04:07.370
The syntax is applicable only for single line statement or simple expressions.

04:07.400 --> 04:08.990
Let's go ahead and check this.

04:10.550 --> 04:14.750
The expectation is that both are going to print the same result.

04:15.170 --> 04:16.130
Run this.

04:17.160 --> 04:19.090
We're going to use P one, not P.

04:22.900 --> 04:23.370
There you go.

04:23.380 --> 04:25.720
Both printed the same result.

04:25.720 --> 04:31.330
So we have successfully implemented the predicate functional interface using Lambda expression.

04:31.330 --> 04:37.990
Now let's build this use case to use the other default methods which are part of the functional interface

04:37.990 --> 04:41.950
predicate you're going to use and negate and are.

04:43.580 --> 04:47.360
I'm going to move this to the top so that we can reuse throughout this class.

04:48.950 --> 04:49.700
There we go.

04:55.300 --> 04:58.300
And then I'm going to mark this one as static.

05:01.290 --> 05:04.350
Run this to make sure this is working as expected.

05:07.610 --> 05:10.010
Let's create a method called predicate and.

05:12.760 --> 05:14.740
Public static.

05:16.310 --> 05:17.090
Wired.

05:19.430 --> 05:21.620
The name is going to be decayed.

05:24.090 --> 05:29.460
So we are going to explore the and method as part of the predicate functional interface.

05:29.490 --> 05:37.800
What I'm going to do is I'm going to introduce one more predicate interface, and this is going to check

05:37.800 --> 05:42.420
if the number is divisible by five or not.

05:42.810 --> 05:43.350
Okay.

05:43.350 --> 05:47.820
So in here, what we are going to do, we are going to have the code as.

05:49.990 --> 05:57.730
Doubt we are going to have p one dot you're going to use the and method P two.

05:58.890 --> 06:04.290
You can have n number of P one or n number of predicate interfaces as part of this chain.

06:04.320 --> 06:09.810
Again, as I mentioned, as part of the consumer interface class, this is called predicate.

06:11.020 --> 06:12.520
Painting in here.

06:12.520 --> 06:16.930
What we are going to do, we are going to pass the input using the test method.

06:16.930 --> 06:19.990
You should always use the test method in order to pass the input.

06:20.260 --> 06:22.180
I'm going to pass the input as ten.

06:22.390 --> 06:23.020
Right.

06:23.020 --> 06:27.430
So ten is a number which is divisible by two and it is also divisible by five.

06:27.460 --> 06:30.520
Now what we are going to do, we are going to pass this input.

06:31.980 --> 06:32.730
For this method.

06:34.400 --> 06:35.840
Are we going to call this method?

06:35.930 --> 06:39.350
This method is eventually going to call this one.

06:40.420 --> 06:42.680
The predicate and result is.

06:44.200 --> 06:45.280
So what will be the result?

06:45.280 --> 06:52.450
The result should be true because we're passing a value ten which is divisible by five and also divisible

06:52.480 --> 06:53.110
by two.

06:55.680 --> 06:57.000
Node and then run this.

06:58.330 --> 06:58.840
There you go.

06:59.110 --> 06:59.560
Predicate.

06:59.560 --> 07:00.580
And the result is.

07:03.180 --> 07:06.660
Now what I'm going to do, I'm going to pass the value as nine.

07:09.000 --> 07:14.400
So we are going to have one more result in the console with the value as the result as false.

07:16.860 --> 07:17.790
As expected.

07:17.940 --> 07:20.340
Let's go ahead and check the operation.

07:20.340 --> 07:22.650
I'm going to copy this and then put it here.

07:24.620 --> 07:30.130
Is going to be predicate are in the place of this we are going to use are.

07:30.910 --> 07:32.080
I'm going to replace this one.

07:32.080 --> 07:32.630
Two are.

07:33.730 --> 07:37.990
Indicate our results Indicate our results.

07:39.280 --> 07:40.240
Take a look at it.

07:40.630 --> 07:41.710
Indicate are.

07:42.590 --> 07:43.080
Candice.

07:43.250 --> 07:48.710
Now, let's go ahead and run this right and then check what is the result the result is going to be.

07:49.580 --> 07:53.360
True in both the cases now for true and false in both the cases.

07:55.410 --> 08:00.930
But this one is not divisible by two and also not divisible by five.

08:00.960 --> 08:05.170
Now, what I'm going to do, I'm going to change this value to eight right in here.

08:05.190 --> 08:08.670
Eight is divisible by two, but not divisible by five.

08:08.910 --> 08:10.320
Let's run this and check.

08:11.400 --> 08:14.160
The result is going to be true and true for the AR operation.

08:14.520 --> 08:20.550
As you see here, the result is true and true because our operation is equal to if one of the operation

08:20.550 --> 08:25.920
is one of the result of the operation is true, then it is going to evaluate the result to.

08:27.040 --> 08:29.680
Now what we will do, we will explore the negate method.

08:33.440 --> 08:36.710
Going to be dedicate negate.

08:38.300 --> 08:41.600
Negate is a method which will reverse the result.

08:42.150 --> 08:42.950
So I'm gonna.

08:45.670 --> 08:47.650
Take this one and then.

08:48.670 --> 08:48.970
Negate.

08:49.330 --> 08:55.300
So in the earlier scenario, this expression evaluated to.

08:56.670 --> 08:57.600
What's right.

08:57.600 --> 09:03.570
So this one evaluated to true because one of the operation is true, then it is going to evaluate the

09:03.570 --> 09:05.390
final result also to true.

09:05.430 --> 09:11.520
Here we are using the negate what what the negate will do is negate is going to reverse the result,

09:11.520 --> 09:13.710
whatever that it is going to evaluate.

09:13.710 --> 09:18.450
So p one dot RP2 is going to evaluate the result to true.

09:18.630 --> 09:23.040
The negate operation basically is going to reverse the result to false.

09:23.790 --> 09:26.910
So the result is going to be false.

09:27.300 --> 09:28.290
Call this method.

09:29.720 --> 09:34.430
I'm going to rename this with the correct value predicate negate.

09:37.080 --> 09:38.250
So I'm going to run this method.

09:41.370 --> 09:44.990
As you see, the predicate negate result is false.

09:45.000 --> 09:48.300
But we take a look at the actual or operation result.

09:48.300 --> 09:48.990
It is true.

09:49.570 --> 09:53.560
So these are the different methods which are part of the predicate interface.

09:53.650 --> 10:00.070
In our next tutorial, we will explore our student database and then use the predicate interface as

10:00.070 --> 10:05.660
part of that student collection and then explore more use cases.

10:05.680 --> 10:07.690
With this, we came to the end of this tutorial.

10:07.720 --> 10:08.860
Thank you for watching.
