JB Header
Java 8 Method References Tutorial with Examples
This tutorial explains the concept of Method References introduced in Java 8. It first defines method references and explains its syntax. Next it looks at the 3 types of method references and explains each of them with code examples. Definition A method reference is a simplified form (or short-hand) of a lambda expressionClick to REad Tutorial on Lambda Expressions. 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.
Method Reference Syntax: <class or instance name>::<methodName>
Method Reference Example
Integer::parseInt is a method reference with the following charecteristics -
  • It is equivalent to the lambda -
      (String str, Integer integer)->Integer.parseInt(str)
  • It can can be assigned to a functional interfaceClick to Read Tutorial on Functional Interfaces Function<T ,R> like this -
      Function<String,integer> intParser=Integer::parseInt
  • Above assignment is equivalent to the assignment of lambda expression of parseInt() -
      Function<String,Integer> intParser =
          (String str,Integer integer)->Integer.parseInt(str)
Thus, instead of the longer lambda expression, just its concise method reference can be assigned to a functional interface instance.
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.
Lambda Syntax: (arguments) -> <ClassName>.<staticMethodName>(arguments);
Equivalent Method Reference: <ClassName> :: <staticMethodName>

Let us now see an example showing the usage of method reference for a static method -
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"));  }
 }
} 
OUTPUT of the above code
double value using method reference - 0.254
double value using Lambda - 0.254
Explanation of the output
The lambda and method reference worked the same and printed the same double value for the same input passed.

Type 2: Reference to an instance method of a particular object -
Lambda Syntax: (param, rest of params)-> (param).<instanceMethodName>(rest of params)
Equivalent Method Reference: <ClassName> :: <staticMethodName>
Note: <ClassName> in method reference is the class of parameter named param.
Code Example:(Skeleton code being the same Type 1 - only giving the delta code below)
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");
OUTPUT of the above code
Print from method reference based instance
Print from instance created from Lambda
Explanation of the output
The lambda and method reference worked the same and printed the string value passed to them.

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.
Lambda Syntax: (arguments) -> <expression>.<instanceMethodName>(arguments)
Equivalent Method Reference: <expression> :: <instanceMethodName>
Code Example(Skeleton code being the same Type 1 - only giving the delta code below)
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));
 OUTPUT of the above code
Is 1 a part of the intList - true
Is 1 a part of the intList - true
Explanation of the output
The lambda and method reference worked the same and printed the same boolean value 'true' for the same input passed.

Note - There is a 4th type of specialized method reference called Constructor Reference. I have written a separate article explaining Constructor ReferencesRead tutorial on Java 8 Constructor References.

Summary The above tutorial explained java 8 method references including their definition, syntax and the 3 types of method references with detailed code examples.