Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 lambda generic interface method

@FunctionalInterface
public interface GenericFunctionalInterface {
  public <T> T genericMethod();
}

I have above @FunctionalInterface and it has a generic method.

How can I use and Lambda expression to represent this Interface?

I tried below code, but it doesn't work,

GenericFunctionalInterface gfi = () -> {return "sss";};

I got compile error: Illegal lambda expression: Method genericMethod of type GenericFunctionalInterface is generic

Where can I place the type info?

like image 325
jack yin Avatar asked Jun 30 '16 08:06

jack yin


1 Answers

The generic (not genetic) type parameter should be declared in the interface level, not in the method level :

public interface GenericFunctionalInterface<T> {
  public T genericMethod();
}

GenericFunctionalInterface<String> gfi = () -> {return "sss";};
like image 143
Eran Avatar answered Oct 05 '22 22:10

Eran