Method References


Method References

1. What is  a Method Reference

Lambda expressions are used to create anonymous methods. In cases when a lambda expression invokes only one existing method, it is better to refer to the existing method by name. Method references gives the possibility to do this. They are compact lambda expressions for methods that already have a name.

For example the next lambda expression calls only one method:

Consumer<String> consumer = str -> System.out.println(str);

It can be rewritten with method reference, which makes the code clearer:

Consumer<String> consumer = System.out::println;

There are four types of method references:

Type Example
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

2. Reference to a Static Method



Let's refactor an example, where we are using static method of a Boolean class:

Function<String, Boolean> function = e -> Boolean.valueOf(e);

 With a method reference the code looks like:

Function<String, Boolean> function = Boolean::valueOf;

3. Reference to an Instance Method of a Particular Object



This type of method references is used when a lambda expression calls a method of an external object that already exists.

For example, let's refactor the example:

Consumer<String> consumer = e -> System.out.println(e);
consumer.accept("OCPJP 8");

Since System.out is an instance of type PrintStream, we can rewrite the previous example as:

Consumer<String> consumer = System.out::println;
consumer.accept("OCPJP 8");

 Or let't look at another example:

Integer integer = new Integer(5);
Supplier<String> supplier = () -> integer.toString();

After refactoring, the code will be:

Integer integer = new Integer(5);
Supplier<String> supplier = integer::toString;

4. Reference to an Instance Method of an Arbitrary Object of a Particular Type



It is called a non-static method but a class name is used for this type of method reference. Actually the instance of the class is passed when the method is called.

Let's look at the lambda expression, where non-static String.toLowerCase method is utilized:

Function<String, String> function = s -> s.toLowerCase();
System.out.println(function.apply("OCPJP 8"));

It can be refactored as: 

Function<String, String> function = String::toLowerCase;
System.out.println(function.apply("OCPJP 8"));

5. Reference to a Constructor



The ClassName shouldn't be the name of an abstract class or interface.

Let's refactor example:

Function<String, Integer> function = (d) -> new Integer(d);

The result is:

Function<String, Integer> function = Integer::new;

The exact constructor isn't specified in a constructor reference expression. When a class declares multiple constructors, the compiler will check the type of the functional interface with all of the constructors and choose the best match:

Function<String, Integer> function1 = (d) -> new Integer(d);
Function<Integer, Integer> function2 = (d) -> new Integer(d);


Sources: Method References - Oracle Tutorial, Develop code that uses a method reference, including refactoring a lambda expression to a method reference.

Leave your comment: