Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the scenarios where the method reference cannot be used directly,
but refactoring the code will make it eligible to use method reference.
A step.
I'm going to create a class called.
Refactor method.
Reference example.
Want to make this class executable by adding the public static void main method.
There you go.
And for this example, what I'm going to do, I'm going to use one of the Lambda expression which we
had implemented in the prior tutorials.
I'm going to go to this functional interfaces package.
Then I'm going to take this predicate student example, and then I'm going to use this one.
So copy this and then put it here and then discuss about what is this Lambda expression is going to
do.
So it is going to take in a student as an input and then return the Boolean because it is a predicate
functional interface here.
If the grade level of the student is greater than or equal to three, then it is going to return the
boolean value as true.
Otherwise it is going to return the boolean result as false.
Let's test this one.
Want to print the result.
P1 dot test.
So the next thing is we need to pass a student, right?
Let's go and check our database class and then see do we have any utility method that just returns a
student?
No, it doesn't return anything like that.
Let's create a utility method that is going to just return a single student.
What I'm going to do is I'm going to use the supplier function interface for that.
So supplier functional interface is to supply any type that is provided in that context, right?
So I'm going to name this one as a static, and then I'm going to provide a supplier of type student.
Then I'm going to name this one as student supplier.
So let's write the lambda expression.
It doesn't take any input.
And returns a student.
So I'm going to copy one of the student from here and then return that.
And give semicolon here.
What is the issue here?
There is some issue which it is complaining about.
Here.
Missing return statement.
We need to add the return statement.
So a student supplier is ready to supply the students whenever it is invoked.
It is going to supply the student.
So from here, what we are going to do for the test method, we need the student, right?
I'm going to get the student database dot.
So we have one method and another function which is of type lambda.
Right?
So let's use that and then call the get method of that supplier is going to give you the student.
There you go.
And what is the student data that it is returning?
If you take a look at it, the student name is Adam and the grade level is two and the GPAs 3.6.
Gender is male and swimming.
Basketball and volleyball are the list of activities.
Adams part of it.
Now.
Go back to our example and then run this one here.
The grade level is three, right?
Let's try to let's run this and then check what is the result.
It is going to return the result as false.
The reason being the grade level is equal to two for Adam and this condition is evaluated to false.
Now the main use cases.
How can we write a method reference for these kind of implementations?
So basically we need to refactor this code and move this logic into a method.
So what I'm going to do, I'm going to create a method public boolean.
Greater than.
Grade level.
And then what I'm going to do is I'm going to pass the student as an input and have this logic mode
inside this.
That and this needs to have the return keyword.
And this logic is successfully moved inside this method.
The next step is.
Let's give the class name refactor method, reference example, and then give the greater than grade
level.
Let's see what it is complaining.
So we have to make that one static because we are trying to reference a method in the static context.
Let me run this and then check the result.
It should give me the false as a result.
We have the result as expected.
So we have successfully refactor the code which was not compatible for method reference.
And one more thing to call out here.
It is not mandatory that each and every code should be compatible with the method.
Reference syntax is not possible to write every code to be method reference compatible in a real world
application.
I just showed here a pattern on how to make a code compatible for method reference syntax.
With this we came to the end of this tutorial.