Java - Generic Method References

Introduction

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().

Demo

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);//from  w w w  . jav  a  2s.  c  o m
    }
  }
}

Result