Hi everyone.
Welcome back to this tutorial.
In the previous tutorial we discussed the theory part of method reference.
In this tutorial we will code and implement the method reference Using some examples, I'm going to
create a package called.
A third reference.
The first example for method reference is by using the function functional interface.
I'm going to create a class called function method reference.
Sample typo here.
I'm going to change that.
Then.
Let's make this class executable.
Public static, void main.
There we go.
Now, I'm going to use the same example that I showed in the presentation.
Let's make it static.
Function.
This is going to take in a string.
As an input and string as an output.
The function takes two inputs.
One is the input type and second is the output type.
I'm going to name this one first.
Let's code the function.
The name is to.
Her case of lambda.
Equal to basically it is accepting an input and then it performs a uppercase on that input.
You go Now let's check this by passing the input.
Two uppercase lambda dot apply.
Java eight.
If you run, this basically is going to give me the output, but with the uppercase operation on top
of it.
Now.
How do we write the method reference implementation of the same thing?
I'm going to use two uppercase.
I'm going to give a different name.
And then the place of this.
You're going to replace this one with method reference.
We know the syntax.
Class name double colon and then the method name.
So it is class name, double colon and the method name.
See, this is the same implementation as the upper one.
It's a god.
Two uppercase method reference dot apply.
Let's go ahead and run this.
There you go.
Both the output are the same.
Let's see whether IntelliJ is going to help us create these method references.
So click on this lambda expression.
I'm using Mac.
I'm pressing the option Enter key.
As soon as I click that option for Windows, it is Add plus enter as you see in the screen.
As soon as I press those keys, it is giving me this option to replace this lambda with method reference.
As soon as I click it, there you go, It automatically creates a method reference implementation for
the lambda expression.
That's one of the advantages of using IntelliJ.
IntelliJ has the code to convert the lambda expression to the method reference.
This is one of the shortcut which you can use to check whether the lambda expression, whatever you
have in your code is eligible for a method reference or not.
So we have successfully implemented the Lambda expression and the equivalent method reference for that
one.
But thus we came to the end of this tutorial.
Let's take a look at some more examples in the next tutorial.
Thank you for.