Generic Method References

Description

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  www.  j a v  a2s.  c o 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.





















Home »
  Java Lambda »
    Java Lambda Tutorial »




Java Lambda Tutorial