Understanding Java-8 method references with examples
In my previous post, I described about Lamda expression and its usages. Now I am going to write about method references in Java 8.
What is a method reference?
As I described in my previous post, a lamda expression is an anonymous function. However, sometimes lamda expression is only calling an existing method. On the other hand, if the function code in the lamda expression is too complicated or has many lines, it makes sense to include it in a separate method. This is when method references comes in handy. Instead of writing a lamda expression, we can refer to an existing method.
Based on the method type, there are 4 types of method reference syntax.
Example usages
I am going to take the same example used in my previous post. Here I have done a slight modification, where GreetProgram has an existing method which has the same function code of the lamda expression. It is the "getMyGreet" method.
So instead of a lamda expression, it can use a method reference as below. Refer the MethodReferSample class code.
What is a method reference?
As I described in my previous post, a lamda expression is an anonymous function. However, sometimes lamda expression is only calling an existing method. On the other hand, if the function code in the lamda expression is too complicated or has many lines, it makes sense to include it in a separate method. This is when method references comes in handy. Instead of writing a lamda expression, we can refer to an existing method.
Based on the method type, there are 4 types of method reference syntax.
Type | Syntax |
---|---|
Reference to a static method | ContainingClass::staticMethodName |
Reference to an instance method of a particular object | containingObject::instanceMethodName |
Reference to an instance method of an arbitrary object of a particular type | ContainingType::methodName |
Reference to a constructor | ClassName::new |
Example usages
I am going to take the same example used in my previous post. Here I have done a slight modification, where GreetProgram has an existing method which has the same function code of the lamda expression. It is the "getMyGreet" method.
interface MyInterface { public String sayWelcome(String name); } class GreetProgram { public void greet(MyInterface myIn) { System.out.println(myIn.sayWelcome("Lakmali")); } public String getMyGreet(String name) { return "Welcome " + name; } } public class LamdaExpSample { public static void main(String args[]) { GreetProgram greetProg = new GreetProgram (); greetProg.greet((name) -> { return "Welcome " + name; }); } }
So instead of a lamda expression, it can use a method reference as below. Refer the MethodReferSample class code.
interface MyInterface { public String sayWelcome(String name); } class GreetProgram { public void greet(MyInterface myIn) { System.out.println(myIn.sayWelcome("Lakmali")); } public String getMyGreet(String name) { return "Welcome " + name; } } public class MethodRefSample { public static void main(String args[]) { GreetProgram greetProg = new GreetProgram (); greetProg.greet(greetProg::getMyGreet); } }
Comments
Post a Comment