A method reference is a simplified form (or short-hand) of a lambda expression. It specifies the class name or the instance name followed by the method name. Instead of writing the lambda expression with all the details such as parameter and return type, a method reference lets you create a lambda expression from an existing method implementation.
Types of Method References
Type 1: Reference to a static method – If we intend to use a static method of a class then instead of writing the lengthier lambda expresion we can just refer to the method via method references.
Example 1: Reference to a static method
package com.javabrahman.java8;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
public class MethodReferenceExample {
public static void main(String args[]){
Function<String, Double> doubleConvertor=Double::parseDouble;
Function<String, Double> doubleConvertorLambda=(String s) -> Double.parseDouble(s);
System.out.println("double value using method reference - "+ doubleConvertor.apply("0.254"));
System.out.println("double value using Lambda - "+ doubleConvertorLambda.apply("0.254")); }
}
}
Type 2: Reference to an instance method of a particular object –
Example 2:Reference to an instance method of an object
Consumer<String> stringPrinter=System.out::println;
Consumer<String> stringPrinterLambda=(String s) -> System.out.println(s);
stringPrinter.accept("Print from method reference based instance");
stringPrinterLambda.accept("Print from instance created from Lambda");
Type 3: Reference to an instance method of an arbitrary object of a particular type– Here the method reference used is of an instance method of an existing object.
Example 3:Reference to an instance method of an arbitrary object of a particular type
List<Integer> intList=Arrays.asList(1,2,3,4);
BiPredicate<List>Integer>,Integer> isPartOf=List::contains;
BiPredicate<List<Integer>,Integer> isPartOfLambda=(List<Integer> listInt, Integer value) -> listInt.contains(value);
System.out.println("Is 1 a part of the intList - "+ isPartOf.test(intList, 1));
System.out.println("Is 1 a part of the intList - "+ isPartOfLambda.test(intList, 1));
Type 4: Constructor Reference
Example usage of Constructor References in code
STEP 1 – Let us define an Employee class with a constructor having 2 parameters as shown below –
Employee.java
public class Employee{
String name;
Integer age;
//Contructor of employee
public Employee(String name, Integer age){
this.name=name;
this.age=age;
}
}
STEP 2 – Now lets create a factory interface for employees called EmployeeFactory. getEmployee() method of EmployeeFactory will return an employee instance as per the normal design of factory pattern
Interface EmployeeFactory.java
public Interface EmployeeFactory{
public abstract Employee getEmployee(String name, Integer age);
}
Note – Since EmployeeFactory interface has a single abstract method hence it is a Functional Interface.
STEP 3 – The client side code to invoke EmployeeFactory to create Employee instances would be as follows –
Client-side code for invoking Factory interface
EmployeeFactory empFactory=Employee::new;
Employee emp= empFactory.getEmployee("John Hammond", 25);
Explanation of the code
The Constructor Reference of Employee is assigned to an instance of EmployeeFactory called empFactory. This is possible because the function descriptor of Employee constructor is same as that of the abstract method of the Functional Interface EmployeeFactory i.e.
(String, Integer) ->Employee.
Then the getEmployee method of empFactory is called with John’s name and age which internally calls the constructor of Employee and a new Employee instance emp is created.
Types of Method References
Type 1: Reference to a static method – If we intend to use a static method of a class then instead of writing the lengthier lambda expresion we can just refer to the method via method references.
Example 1: Reference to a static method
package com.javabrahman.java8;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
public class MethodReferenceExample {
public static void main(String args[]){
Function<String, Double> doubleConvertor=Double::parseDouble;
Function<String, Double> doubleConvertorLambda=(String s) -> Double.parseDouble(s);
System.out.println("double value using method reference - "+ doubleConvertor.apply("0.254"));
System.out.println("double value using Lambda - "+ doubleConvertorLambda.apply("0.254")); }
}
}
Type 2: Reference to an instance method of a particular object –
Example 2:Reference to an instance method of an object
Consumer<String> stringPrinter=System.out::println;
Consumer<String> stringPrinterLambda=(String s) -> System.out.println(s);
stringPrinter.accept("Print from method reference based instance");
stringPrinterLambda.accept("Print from instance created from Lambda");
Type 3: Reference to an instance method of an arbitrary object of a particular type– Here the method reference used is of an instance method of an existing object.
Example 3:Reference to an instance method of an arbitrary object of a particular type
List<Integer> intList=Arrays.asList(1,2,3,4);
BiPredicate<List>Integer>,Integer> isPartOf=List::contains;
BiPredicate<List<Integer>,Integer> isPartOfLambda=(List<Integer> listInt, Integer value) -> listInt.contains(value);
System.out.println("Is 1 a part of the intList - "+ isPartOf.test(intList, 1));
System.out.println("Is 1 a part of the intList - "+ isPartOfLambda.test(intList, 1));
Type 4: Constructor Reference
Example usage of Constructor References in code
STEP 1 – Let us define an Employee class with a constructor having 2 parameters as shown below –
Employee.java
public class Employee{
String name;
Integer age;
//Contructor of employee
public Employee(String name, Integer age){
this.name=name;
this.age=age;
}
}
STEP 2 – Now lets create a factory interface for employees called EmployeeFactory. getEmployee() method of EmployeeFactory will return an employee instance as per the normal design of factory pattern
Interface EmployeeFactory.java
public Interface EmployeeFactory{
public abstract Employee getEmployee(String name, Integer age);
}
Note – Since EmployeeFactory interface has a single abstract method hence it is a Functional Interface.
STEP 3 – The client side code to invoke EmployeeFactory to create Employee instances would be as follows –
Client-side code for invoking Factory interface
EmployeeFactory empFactory=Employee::new;
Employee emp= empFactory.getEmployee("John Hammond", 25);
Explanation of the code
The Constructor Reference of Employee is assigned to an instance of EmployeeFactory called empFactory. This is possible because the function descriptor of Employee constructor is same as that of the abstract method of the Functional Interface EmployeeFactory i.e.
(String, Integer) ->Employee.
Then the getEmployee method of empFactory is called with John’s name and age which internally calls the constructor of Employee and a new Employee instance emp is created.
No comments:
Post a Comment