jack yin
jack yin

Reputation: 61

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?

Upvotes: 6

Views: 4775

Answers (1)

Eran
Eran

Reputation: 394026

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";};

Upvotes: 10

Related Questions