WEBVTT

00:00.170 --> 00:00.650
Hi everyone.

00:00.650 --> 00:01.760
Welcome back to this tutorial.

00:01.760 --> 00:07.670
In this tutorial we will code and explore the scenarios where the method reference cannot be used directly,

00:07.670 --> 00:13.130
but refactoring the code will make it eligible to use method reference.

00:14.270 --> 00:14.690
A step.

00:14.690 --> 00:16.430
I'm going to create a class called.

00:17.320 --> 00:19.120
Refactor method.

00:20.060 --> 00:21.680
Reference example.

00:22.560 --> 00:26.940
Want to make this class executable by adding the public static void main method.

00:27.480 --> 00:27.990
There you go.

00:28.860 --> 00:34.410
And for this example, what I'm going to do, I'm going to use one of the Lambda expression which we

00:34.410 --> 00:36.960
had implemented in the prior tutorials.

00:37.050 --> 00:39.710
I'm going to go to this functional interfaces package.

00:39.770 --> 00:45.240
Then I'm going to take this predicate student example, and then I'm going to use this one.

00:46.170 --> 00:51.630
So copy this and then put it here and then discuss about what is this Lambda expression is going to

00:51.630 --> 00:52.140
do.

00:52.410 --> 00:57.360
So it is going to take in a student as an input and then return the Boolean because it is a predicate

00:57.360 --> 00:59.460
functional interface here.

00:59.490 --> 01:04.920
If the grade level of the student is greater than or equal to three, then it is going to return the

01:05.130 --> 01:06.930
boolean value as true.

01:06.930 --> 01:10.770
Otherwise it is going to return the boolean result as false.

01:11.570 --> 01:12.800
Let's test this one.

01:13.690 --> 01:14.950
Want to print the result.

01:14.990 --> 01:17.140
P1 dot test.

01:17.280 --> 01:19.810
So the next thing is we need to pass a student, right?

01:20.020 --> 01:26.050
Let's go and check our database class and then see do we have any utility method that just returns a

01:26.050 --> 01:26.590
student?

01:26.620 --> 01:28.630
No, it doesn't return anything like that.

01:28.660 --> 01:32.890
Let's create a utility method that is going to just return a single student.

01:33.610 --> 01:38.300
What I'm going to do is I'm going to use the supplier function interface for that.

01:38.320 --> 01:44.560
So supplier functional interface is to supply any type that is provided in that context, right?

01:44.650 --> 01:52.150
So I'm going to name this one as a static, and then I'm going to provide a supplier of type student.

01:52.950 --> 01:55.350
Then I'm going to name this one as student supplier.

01:56.350 --> 01:58.180
So let's write the lambda expression.

01:58.180 --> 02:00.250
It doesn't take any input.

02:01.640 --> 02:02.810
And returns a student.

02:02.810 --> 02:07.700
So I'm going to copy one of the student from here and then return that.

02:08.940 --> 02:11.310
And give semicolon here.

02:11.970 --> 02:12.930
What is the issue here?

02:12.930 --> 02:15.510
There is some issue which it is complaining about.

02:16.640 --> 02:17.270
Here.

02:18.290 --> 02:19.190
Missing return statement.

02:19.190 --> 02:21.470
We need to add the return statement.

02:23.330 --> 02:27.380
So a student supplier is ready to supply the students whenever it is invoked.

02:27.380 --> 02:29.750
It is going to supply the student.

02:29.780 --> 02:35.670
So from here, what we are going to do for the test method, we need the student, right?

02:35.690 --> 02:38.780
I'm going to get the student database dot.

02:38.810 --> 02:43.820
So we have one method and another function which is of type lambda.

02:43.820 --> 02:44.170
Right?

02:44.180 --> 02:49.640
So let's use that and then call the get method of that supplier is going to give you the student.

02:50.210 --> 02:50.900
There you go.

02:51.170 --> 02:54.500
And what is the student data that it is returning?

02:54.530 --> 03:01.010
If you take a look at it, the student name is Adam and the grade level is two and the GPAs 3.6.

03:01.010 --> 03:03.500
Gender is male and swimming.

03:03.500 --> 03:06.200
Basketball and volleyball are the list of activities.

03:07.070 --> 03:08.540
Adams part of it.

03:08.810 --> 03:09.560
Now.

03:10.710 --> 03:14.430
Go back to our example and then run this one here.

03:14.430 --> 03:16.110
The grade level is three, right?

03:16.470 --> 03:20.040
Let's try to let's run this and then check what is the result.

03:20.070 --> 03:22.760
It is going to return the result as false.

03:22.770 --> 03:31.710
The reason being the grade level is equal to two for Adam and this condition is evaluated to false.

03:32.840 --> 03:34.580
Now the main use cases.

03:34.610 --> 03:39.170
How can we write a method reference for these kind of implementations?

03:39.650 --> 03:44.930
So basically we need to refactor this code and move this logic into a method.

03:45.020 --> 03:50.390
So what I'm going to do, I'm going to create a method public boolean.

03:52.430 --> 03:53.390
Greater than.

03:54.730 --> 03:55.570
Grade level.

03:56.480 --> 04:02.450
And then what I'm going to do is I'm going to pass the student as an input and have this logic mode

04:02.450 --> 04:04.190
inside this.

04:05.590 --> 04:09.630
That and this needs to have the return keyword.

04:09.720 --> 04:13.260
And this logic is successfully moved inside this method.

04:13.290 --> 04:14.550
The next step is.

04:16.070 --> 04:23.000
Let's give the class name refactor method, reference example, and then give the greater than grade

04:23.000 --> 04:23.480
level.

04:24.320 --> 04:26.180
Let's see what it is complaining.

04:26.330 --> 04:32.270
So we have to make that one static because we are trying to reference a method in the static context.

04:32.300 --> 04:34.860
Let me run this and then check the result.

04:34.880 --> 04:37.520
It should give me the false as a result.

04:38.030 --> 04:39.670
We have the result as expected.

04:39.680 --> 04:44.810
So we have successfully refactor the code which was not compatible for method reference.

04:45.050 --> 04:47.420
And one more thing to call out here.

04:47.420 --> 04:52.100
It is not mandatory that each and every code should be compatible with the method.

04:52.100 --> 04:58.820
Reference syntax is not possible to write every code to be method reference compatible in a real world

04:58.820 --> 04:59.820
application.

04:59.840 --> 05:06.920
I just showed here a pattern on how to make a code compatible for method reference syntax.

05:06.950 --> 05:08.960
With this we came to the end of this tutorial.
