3.5 Method and Constructor References
Sometimes, there is already a method that carries out exactly the action that you’d like to pass on to some other code. There is special syntax for a method reference that is even shorter than a lambda expression calling the method. A similar shortcut exists for constructors. You will see both in the following sections.
3.5.1 Method References
Suppose you want to sort strings regardless of letter case. You could call
Arrays.sort(strings, (x, y) -> x.compareToIgnoreCase(y));
Instead, you can pass this method expression:
The expression String::compareToIgnoreCase is a method reference that is equivalent to the lambda expression (x, y) -> x.compareToIgnoreCase(y).
Here is another example. The Objects class defines a method isNull. The call Objects.isNull(x) simply returns the value of x == null. It seems hardly worth having a method for this case, but it was designed to be passed as a method expression. The call
removes all null values from a list.
As another example, suppose you want to print all elements of a list. The ArrayList class has a method forEach that applies a function to each element. You could call
list.forEach(x -> System.out.println(x));
It would be nicer, however, if you could just pass the println method to the forEach method. Here is how to do that:
As you can see from these examples, the :: operator separates the method name from the name of a class or object. There are three variations:
In the first case, the first parameter becomes the receiver of the method, and any other parameters are passed to the method. For example, String::compareToIgnoreCase is the same as (x, y) -> x.compareToIgnoreCase(y).
In the second case, all parameters are passed to the static method. The method expression Objects::isNull is equivalent to x -> Objects.isNull(x).
In the third case, the method is invoked on the given object, and the parameters are passed to the instance method. Therefore, System.out::println is equivalent to x -> System.out.println(x).
You can capture the this parameter in a method reference. For example, this::equals is the same as x -> this.equals(x).
3.5.2 Constructor References
Constructor references are just like method references, except that the name of the method is new. For example, Employee::new is a reference to an Employee constructor. If the class has more than one constructor, then it depends on the context which constructor is chosen.
Here is an example for using such a constructor reference. Suppose you have a list of strings
List<String> names = ...;
You want a list of employees, one for each name. As you will see in Chapter 8, you can use streams to do this without a loop: Turn the list into a stream, and then call the map method. It applies a function and collects all results.
Stream<Employee> stream = names.stream().map(Employee::new);
Since names.stream() contains String objects, the compiler knows that Employee::new refers to the constructor Employee(String).
You can form constructor references with array types. For example, int::new is a constructor reference with one parameter: the length of the array. It is equivalent to the lambda expression n -> new int[n].
Array constructor references are useful to overcome a limitation of Java: It is not possible to construct an array of a generic type. (See Chapter 6 for details.) For that reason, methods such Stream.toArray return an Object array, not an array of the element type:
Object employees = stream.toArray();
But that is unsatisfactory. The user wants an array of employees, not objects. To solve this problem, another version of toArray accepts a constructor reference:
Employee buttons = stream.toArray(Employee::new);
The toArray method invokes this constructor to obtain an array of the correct type. Then it fills and returns the array.