Method Reference in Java 8

Overview

In this tutorial, we will get an overview of the method reference in Java 8.

Method Reference

 In Java, we can use references to objects by creating new objects or using existing objects. for example - 

String name = "GeeksCoder";
logService.print(name);

In the above code, the name is the reference to a string "GeeksCoder", we are passing the name as a parameter in logService print method, It is the usual way how we use any reference to any object in Java. 

But what about a reference to a method? If we only use a method of an object in another method, we still have to pass the full object as an argument. Wouldn't it be more practical to just pass the method as an argument?

For example:

isFull(list.size);


In Java 8, We can achieve this using lambda expressions and pass methods same like we pass objects or primitive values. A method reference is the shorthand syntax for a lambda expression that executes just ONE method. Here's the general syntax of a method reference:

Object :: methodName
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class EvenOddCheck {
    public static boolean isEven(int n) {
        return n % 2 == 0;
    }
}
public class MethodReference {
    public static void main(String[] args) {
        List numberList =  Arrays.asList(1,2,3,4,5);
        //we will filter the even number from this list using method reference
        List evenNumbers = numberList.stream().filter(EvenOddCheck::isEven).collect(Collectors.toList());
        evenNumbers.forEach(System.out::println);
    }
}

Output: 2 4

In the above code, we are passing the isEven method in the filter method of the Stream class without passing the whole object of EvenOddCheck class.

There are four types of method references.

1. Reference to a static method

ContainingClass::staticMethodName

It is the same as how we did in the above EvenOddEheck example. In this way, the method which is passed as a reference should be static.

2. Reference to an instance method of a particular object

containingObject::instanceMethodName
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class EvenOddCheck {
    public  boolean isEven(int n) {
        return n % 2 == 0;
    }
}
public class StaticMethodReference {
    public static void main(String[] args) {
        List numberList =  Arrays.asList(1,2,3,4,5);
        //we will filter the even number from this list using method reference
        EvenOddCheck evenOddCheck = new EvenOddCheck();
        List evenNumbers = numberList.stream().filter(evenOddCheck::isEven).collect(Collectors.toList());
        evenNumbers.forEach(System.out::println);
    }
}

Here we have use the object reference of EvenOddCheck class in place of class name and convert the isEven static method to instance method.

3. Reference to an instance method of an arbitrary object of a particular type

ContainingType::methodName
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArbitraryObjectMethodReference {

    public static void main(String[] args) {
        List lowerCaseList =  Arrays.asList("Hello", "GeeksCoder");
        //we will convert lowercase words to uppercase using arbitrary object method reference
        List upperCaseList = lowerCaseList.stream().map(String::toUpperCase).collect(Collectors.toList());
        upperCaseList.forEach(System.out::println);
    }

}

This is an example of a reference to an instance method of an arbitrary object of a particular type (String in this case)

4. Reference to a constructor

ClassName::new