WEBVTT

00:00.620 --> 00:01.130
Hi everyone.

00:01.130 --> 00:02.360
Welcome back to this tutorial.

00:02.360 --> 00:08.210
In this tutorial, we will test our knowledge by building the same use case of printing the name and

00:08.210 --> 00:09.650
the list of activities.

00:09.650 --> 00:15.800
By combining the predicate and consumer functional interface we have been coding so far.

00:15.800 --> 00:17.630
Let's go ahead and code this one.

00:18.410 --> 00:24.080
The first step is I'm going to create a class called predicate and consumer example.

00:29.760 --> 00:30.510
Sounds good.

00:31.290 --> 00:32.430
The next step is.

00:33.680 --> 00:38.840
I'm going to make this class executable by adding the public static void main method.

00:40.050 --> 00:41.600
Now what I'm going to do.

00:41.610 --> 00:44.410
Until now, we have been creating static methods, right?

00:44.430 --> 00:47.820
In this example, we are going to create an instance method.

00:47.850 --> 00:49.350
Why are we changing now?

00:49.380 --> 00:55.800
The main reason why I would like to do it via instance method is to highlight that lambda behaves the

00:55.800 --> 01:00.270
same irrespective of an instance instance method or a static method.

01:01.710 --> 01:04.710
Gonna name this one as public void.

01:07.050 --> 01:09.750
Print name and.

01:11.350 --> 01:12.310
Activities.

01:13.430 --> 01:14.930
This is what I would like to call.

01:15.920 --> 01:18.020
Okay, now we have our method ready.

01:18.050 --> 01:21.620
The next step is we'll create our consumer.

01:23.960 --> 01:24.530
Consumer.

01:29.130 --> 01:32.940
And this consumer is basically going to iterate.

01:35.480 --> 01:36.950
I import the consumer.

01:37.950 --> 01:40.060
Have the consumer instance ready.

01:40.080 --> 01:41.880
Now let's go ahead and implement it.

01:42.090 --> 01:43.950
This is going to accept the student.

01:43.950 --> 01:47.670
And what it is going to do is it is going to.

01:48.820 --> 01:51.070
Perform the filtering operation.

01:51.070 --> 01:55.450
Basically, we are going to now create the instance of predicate.

01:55.600 --> 02:02.020
So the predicate instance is going to be for the grade level and the level.

02:03.220 --> 02:04.380
I'm going to create.

02:05.920 --> 02:06.460
Indicate.

02:08.580 --> 02:09.180
Student.

02:11.990 --> 02:12.830
For this.

02:16.620 --> 02:23.670
What to name this one as P, and then we are going to get the grade level of the student right.

02:26.860 --> 02:27.520
Student.

02:28.060 --> 02:29.200
Get the student first.

02:31.190 --> 02:35.300
Dot get the grade level greater than or equal to three.

02:37.180 --> 02:37.890
Have this ready.

02:37.920 --> 02:39.330
Next, let's create the P2.

02:40.320 --> 02:46.350
The P2 is going to be get GPA greater than or equal to 3.9.

02:48.530 --> 02:48.950
Okay.

02:49.490 --> 02:50.210
Sounds good.

02:50.570 --> 02:51.050
There we go.

02:51.140 --> 02:52.910
Now we are going to use this.

02:53.780 --> 02:55.220
If be one.

02:58.680 --> 02:59.490
And.

03:01.840 --> 03:02.320
To.

03:03.470 --> 03:04.790
God, just.

03:05.630 --> 03:06.320
As the student.

03:07.410 --> 03:07.910
Right.

03:07.920 --> 03:12.570
The next step is we are going to print the name and the list of activities.

03:12.600 --> 03:13.020
Right.

03:13.020 --> 03:15.840
For that, I'm going to create a bi consumer.

03:16.870 --> 03:20.170
Because we can achieve that using the single by consumer itself.

03:21.310 --> 03:27.520
So we are going to print the name, which is of type string and the activities which is of type list

03:27.520 --> 03:28.180
of string.

03:28.990 --> 03:30.670
Okay, let's import the list.

03:30.700 --> 03:33.750
I'm using the option and enter Target.

03:33.880 --> 03:36.130
You can see all the shortcut that I'm using here.

03:36.130 --> 03:38.980
It will be printed over the window.

03:40.950 --> 03:47.250
And I'm going to name this one as student by consumer equal to.

03:48.600 --> 03:51.660
I'll get the name and activities.

03:53.010 --> 03:58.950
We don't have to provide the type because Lambda is smart enough to determine the type by looking at

03:58.950 --> 04:02.100
the by consumer type that is defined as part of it.

04:03.050 --> 04:06.290
And then the next step is system dot out dot print.

04:06.290 --> 04:12.970
Ln name we're going to have a colon in between plus the activities.

04:13.720 --> 04:16.090
So we have the bi consumer also ready.

04:17.620 --> 04:22.330
So we are going to do by consumer dot accept.

04:23.130 --> 04:23.880
Let's pass the.

04:25.300 --> 04:25.480
It.

04:28.500 --> 04:29.970
Not yet.

04:30.000 --> 04:30.480
Name.

04:31.400 --> 04:32.060
And.

04:34.940 --> 04:39.530
It's going to accept two parameters because it's a bi consumer and.

04:41.560 --> 04:42.310
Activities.

04:43.530 --> 04:45.430
And I'm going to give a semicolon here.

04:45.450 --> 04:50.020
Now we have the functional interfaces implementations ready.

04:50.040 --> 04:53.940
Now the next step is we have to call this method, right?

04:54.360 --> 04:55.770
I'm going to call this method.

04:55.800 --> 04:58.980
But prior to that, I'm going to get the list of students.

05:00.590 --> 05:04.790
The students going to be a student list?

05:05.690 --> 05:11.810
I'm going to call the student database dot, get all students, which will give me the list of student.

05:11.810 --> 05:14.360
And the next step is I'm going to pass.

05:16.840 --> 05:21.890
The new predicate and consumer example dot print name and activities.

05:21.910 --> 05:27.010
Right now this print name and activities method doesn't accept any parameters.

05:27.610 --> 05:28.900
I'm going to pass a.

05:30.050 --> 05:31.710
Partial the list of student as part of it.

05:33.210 --> 05:34.080
Print list.

05:40.440 --> 05:41.370
We're getting this.

05:42.060 --> 05:47.310
The next step is I'm going to call students dot rich.

05:47.640 --> 05:50.760
Now we have this student consumer ready.

05:51.210 --> 05:54.780
Just call this dot Accept.

06:00.390 --> 06:04.730
We don't even have to pass anything because it is already iterating the for each method is going to

06:04.730 --> 06:07.150
iterate and pass the shouldn't already.

06:07.160 --> 06:12.950
Now this shouldn't consumer is going to get the shouldn't and perform all the test operation.

06:12.950 --> 06:14.360
Basically it is going to apply.

06:14.360 --> 06:20.690
The grade level is greater than three or the grade and the level is greater than 3.9.

06:20.690 --> 06:26.720
And then it is going to use the by consumer in order to print the name and activities in the console.

06:27.200 --> 06:28.790
Let's go ahead and run this.

06:31.430 --> 06:32.150
There you go.

06:32.270 --> 06:34.970
It printed the name and its respective activities.

06:36.720 --> 06:44.550
The advantage with this kind of code is completely modularized and it is really easy to modify the functionality

06:44.550 --> 06:48.180
if something else needs to be changed or added to the code.

06:48.210 --> 06:50.340
With this, we came to the end of this tutorial.

06:50.370 --> 06:51.510
Thank you for watching.
