Java - Generic Method References


The syntax for a method reference supports the actual type parameters for generic types.

The actual type parameters are specified before the two consecutive colons.

For example, the constructor reference ArrayList<Long>::new specifies Long as the actual type parameter for the generic ArrayList<T> class.

The following code sets the actual type parameter for the generic method Arrays.asList().


import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class Main {
  public static void main(String[] args) {
    Function<String[], List<String>> asList = Arrays::<String>asList;

    String[] namesArray = { "Java", "Json", "Javascript" };
    List<String> namesList = asList.apply(namesArray);
    for (String name : namesList) {
      System.out.println(name);/*  w w  w . j av a  2 s  . co  m*/