Java Lambda - Java Generic Method Reference








We can use generic methods in method reference by specifying the actual type parameter.

The syntax is as follows

ClassName::<TypeName>methodName

The syntax for generic constructor reference

ClassName<TypeName>::new

Example

The following code creates a lambda expression by using the generic Arrays.asList method and sets the parameter as String.

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
/*from  w ww . j  ava  2s .co  m*/
public class Main{
  public static void main(String[] argv){
    Function<String[],List<String>> asList = Arrays::<String>asList;
    
    System.out.println(asList.apply(new String[]{"a","b","c"}));
  }
}

The code above generates the following result.