WEBVTT

00:00.810 --> 00:01.320
Hi everyone.

00:01.320 --> 00:02.520
Welcome back to this tutorial.

00:02.520 --> 00:08.460
In this tutorial we will code and explore the predicate example using the student collection that we

00:08.460 --> 00:10.140
have as part of this project.

00:10.290 --> 00:13.260
The first step is I'm going to create a class called.

00:14.580 --> 00:15.510
Predicate.

00:16.810 --> 00:18.520
Student example.

00:19.210 --> 00:21.960
And the next step is I'm going to make this class executable.

00:21.970 --> 00:26.680
The way to make this class executable is by adding the public static void main method.

00:27.100 --> 00:27.910
There you go.

00:27.910 --> 00:29.320
We have the class ready.

00:29.620 --> 00:35.710
The next step is we are going to implement the predicate functional interface using lambda expression

00:35.710 --> 00:37.990
for the object student.

00:39.050 --> 00:42.830
Predicate and the input that it is going to accept is student.

00:42.860 --> 00:47.150
If you remember from the previous example in this example, the input was integer.

00:47.180 --> 00:49.340
Here we are going to.

00:50.460 --> 00:52.920
Implement the predicate interface for student.

00:54.060 --> 00:58.050
Want to name it as P1 can name it in any name you want.

00:58.780 --> 01:00.610
So the input is going to be student.

01:02.370 --> 01:05.340
And then what is this method is going to do?

01:05.370 --> 01:11.880
So this is going to return true if the grade level of the student is equal to is greater than or equal

01:11.880 --> 01:12.450
to three.

01:12.840 --> 01:18.990
So I'm basically recreating the same example that we did for our consumer example.

01:19.050 --> 01:22.750
You take a look at it, we are checking the grade level is greater than or equal to three.

01:22.770 --> 01:27.330
I'm trying to implement the same thing in predicate student example.

01:27.480 --> 01:28.050
There you go.

01:28.080 --> 01:29.370
We have the predicate ready.

01:29.400 --> 01:32.310
The next step is let's go ahead and implement this.

01:35.030 --> 01:37.400
I'm going to create a method called.

01:38.800 --> 01:39.460
Public.

01:40.620 --> 01:42.210
Static void.

01:43.130 --> 01:43.880
And then.

01:45.070 --> 01:45.640
Filter.

01:49.800 --> 01:50.250
Student.

01:51.780 --> 01:52.050
By.

01:53.400 --> 01:54.000
Grade level.

01:54.330 --> 01:55.470
That's the method I'm going to.

01:58.830 --> 02:01.170
First thing is we need the student collection.

02:01.890 --> 02:02.700
So we know.

02:03.730 --> 02:05.290
There is a student database.

02:06.070 --> 02:07.930
That get all student.

02:09.080 --> 02:10.880
This returns a list of student.

02:13.700 --> 02:15.230
Britain's best.

02:16.470 --> 02:18.210
We have everything ready.

02:18.510 --> 02:19.120
Right.

02:19.140 --> 02:20.130
What is the next step?

02:20.160 --> 02:24.520
The next step is we need to iterate each and every student.

02:24.540 --> 02:24.900
Right.

02:24.900 --> 02:25.920
I'm going to make.

02:26.900 --> 02:27.350
Which of the.

02:29.260 --> 02:31.000
Porridge method for that.

02:31.490 --> 02:31.750
So.

02:33.500 --> 02:36.650
Student list Dot Burrage.

02:38.110 --> 02:40.340
Each method takes in a consumer interface.

02:40.360 --> 02:46.930
We all know that it's going to take a student, and then here we are going to implement our.

02:48.680 --> 02:49.610
Lambda body.

02:49.640 --> 02:53.480
The body of this particular lambda expression.

02:54.520 --> 02:57.970
So here the first thing is we are going to use.

03:00.480 --> 03:07.380
Predicate and we are going to pass all the test method for this test method, we are going to pass the

03:07.380 --> 03:08.010
student.

03:09.160 --> 03:09.730
And here it is.

03:09.730 --> 03:11.890
Complaining V1 is not static.

03:11.890 --> 03:12.210
Right?

03:12.220 --> 03:14.620
We are trying to use a variable.

03:15.730 --> 03:18.550
One to the static method, but.

03:19.690 --> 03:22.630
Cricket is not a static, so I'm going to make this static.

03:22.660 --> 03:23.410
There you go.

03:23.590 --> 03:25.210
The compilation issue is gone.

03:25.240 --> 03:29.080
The next step is if P1 dot test student then.

03:30.030 --> 03:31.110
And all the students.

03:32.870 --> 03:42.350
So we have written an inline Lambda consumer interface that uses the predicate functional interface.

03:42.470 --> 03:45.500
Right now what I'm going to do, I'm going to call this method.

03:48.660 --> 03:54.360
So we all know this student database has six students, two of second grade students, two of the third

03:54.360 --> 03:56.580
grade students and two of the fourth grade students.

03:56.610 --> 04:00.960
Now, if we take a look at the filter, the filter that we have here is.

04:01.940 --> 04:04.640
Grade level is greater than or equal to three.

04:04.670 --> 04:11.120
Then in that case, the students who are lesser than three will be.

04:12.520 --> 04:14.200
Ignored as part of this condition.

04:14.400 --> 04:15.250
I'm gonna run this.

04:15.280 --> 04:17.920
There will be four students who will be printed in the console.

04:18.460 --> 04:19.220
There you go.

04:19.240 --> 04:22.090
We have all the four students printed here.

04:22.360 --> 04:25.620
The next step is you want to add one more condition, right?

04:25.630 --> 04:30.130
So in the consumer example, what we did was we add all those conditions in here.

04:31.250 --> 04:34.460
Now what I'm going to do, I'm going to create another.

04:37.040 --> 04:38.360
Pretty good instance.

04:39.050 --> 04:45.470
And in here I'm going to set the get GPA levels greater than or equal to three point.

04:45.500 --> 04:48.020
And I'm going to use a same example condition here.

04:51.030 --> 04:53.130
Greater than or equal to 3.9.

04:53.370 --> 04:54.090
Close this.

04:56.510 --> 04:58.190
So I'm going to create another method.

05:01.620 --> 05:03.210
Filter runs by.

05:06.950 --> 05:07.280
EP.

05:09.430 --> 05:09.640
Okay.

05:09.910 --> 05:14.500
And then here what I'm going to do, I'm going to use P to.

05:15.840 --> 05:16.350
Doctors.

05:16.500 --> 05:20.170
So here we are, not considering the grade level.

05:20.190 --> 05:26.220
We are taking the level is greater than or equal to 3.9.

05:26.250 --> 05:31.680
So it is going to check all the grades, students, second grade, third grade and fourth grade students.

05:31.680 --> 05:32.820
And it is going to apply.

05:32.820 --> 05:36.150
The filter of level is greater than or equal to 3.9.

05:36.750 --> 05:38.220
I'm going to call this method.

05:42.930 --> 05:43.650
And run this.

05:48.550 --> 05:54.460
Harder to sought in order to differentiate the results between this method and this method.

06:00.200 --> 06:00.530
There you go.

06:00.950 --> 06:05.260
So if you take a look at this one, applied the filter of the grade level.

06:05.270 --> 06:09.110
So the result, the number of results are of four count.

06:09.650 --> 06:10.780
Take a look at this one.

06:10.790 --> 06:18.680
We are using the level, the students or of the six, only three students have the level equal to or

06:18.680 --> 06:19.760
greater than 3.9.

06:19.790 --> 06:21.550
That's the reason why you see three results.

06:21.560 --> 06:24.620
Now we want to add one more condition.

06:24.620 --> 06:32.480
Meaning I want to filter the students who are at the grade level three and whose GPAs 3.9 if.

06:33.600 --> 06:36.420
We do not have these predicates created within the head.

06:36.510 --> 06:40.320
In that case, what we will do is like what we did in here.

06:41.100 --> 06:46.480
We'll be adding all those conditions inside the method, right?

06:46.500 --> 06:52.320
So here, since we have all these conditions already predefined, what I'm going to do, I'm going to

06:52.320 --> 06:53.340
create another method.

06:56.230 --> 06:57.250
Public static.

06:58.590 --> 07:00.900
White filter students.

07:02.980 --> 07:05.590
But this method is going to be a common method.

07:07.330 --> 07:09.810
You can add any number of conditions to it.

07:10.410 --> 07:11.670
Let's say in here.

07:13.420 --> 07:15.400
P one dot.

07:17.370 --> 07:17.970
And.

07:20.470 --> 07:22.180
He to doctors.

07:22.450 --> 07:23.830
So basically.

07:29.770 --> 07:30.790
And reuse the code.

07:30.790 --> 07:33.910
So Lambda always embraces a code reusability.

07:34.120 --> 07:36.510
So you define the condition at the top.

07:36.520 --> 07:38.080
It's not only in this class.

07:38.080 --> 07:40.720
You can use the same condition throughout your project.

07:41.050 --> 07:46.690
If you define all these different conditions in a common code which can be shared across all the code,

07:46.840 --> 07:52.120
if you have a condition like that, if you have a class which takes care of maintaining all the conditions

07:52.120 --> 07:59.380
and all the filter levels can use it throughout the project so that you can avoid the code duplication.

07:59.380 --> 08:04.830
Eventually you can reuse the code as much as you can in your code base.

08:04.870 --> 08:08.830
Now what I'm going to do, I'm going to call this filter students class.

08:13.540 --> 08:15.070
We printed.

08:16.450 --> 08:18.620
Now you are changing your mind.

08:18.640 --> 08:18.910
Okay.

08:18.910 --> 08:22.990
I don't want, uh, both the conditions to be applicable.

08:22.990 --> 08:28.780
If there is only one condition which matches this, then I want those students to be printed.

08:28.870 --> 08:31.960
In that case, the changes are really, really simple.

08:31.960 --> 08:38.530
If you take a look at it, I just change this condition to R and it is going to give me the result that

08:38.530 --> 08:38.980
I want.

08:40.130 --> 08:41.260
Gave me the result that I want.

08:41.570 --> 08:46.190
And then you don't want to print any of the students who matches this condition.

08:46.190 --> 08:50.270
In that case, it is a negate because the negate.

08:51.390 --> 08:52.890
Not going to print anything.

08:53.930 --> 08:55.130
Those kind of scenarios.

08:55.130 --> 08:59.990
You don't want want to print only the students who doesn't satisfy this condition.

09:00.590 --> 09:03.010
So in those cases, we will introduce the else loop.

09:03.020 --> 09:06.950
This is going to print the students, whoever is not matching that result.

09:07.430 --> 09:09.500
Here it printed all the students.

09:09.500 --> 09:12.410
Who is not matching that condition?

09:12.410 --> 09:16.610
It is clearly evident that Lambda expressions embrace the code reusability.

09:16.640 --> 09:21.130
Let's check how we could have implemented this if we had not used lambdas.

09:21.140 --> 09:23.030
We have to hardcode the condition first.

09:23.030 --> 09:23.300
Right?

09:23.300 --> 09:24.740
So we are using the.

09:25.610 --> 09:26.990
Grade level in this method.

09:27.210 --> 09:29.150
So you would have put that condition here.

09:29.420 --> 09:33.320
And then we are using the filter in this method.

09:33.590 --> 09:37.760
So we would have put this condition here in this method, we need both of these conditions.

09:37.760 --> 09:42.620
So in that case, we could have put both of these conditions in here.

09:43.970 --> 09:45.790
So this leads to code duplication.

09:45.800 --> 09:49.610
Or you could have put this common logic in a utility class.

09:49.610 --> 09:52.550
And inside the method you'll be having all these conditions.

09:52.550 --> 10:00.290
You could have referenced that logic from this class, but with Lambda it simplifies all the logic and

10:00.290 --> 10:01.190
all the logic.

10:01.190 --> 10:07.210
It is assigned to an variable and passed across the project just like any other variable.

10:07.220 --> 10:13.070
Eventually what this does is that it helps building the projects really faster and embraces the code

10:13.070 --> 10:14.060
reusability.

10:14.210 --> 10:16.400
With this, we came to the end of this tutorial.

10:16.400 --> 10:17.330
Thank you for watching.
