THE Waterfall :
I have the following interface:
public interface Mapper {
public <T> T map(T element);
}
and when I do Mapper mapper = (int n) -> n*2;
I get the problem:
Illegal lambda expression: Method map of type Mapper is generic
What am I missing here? How is it possible to create a generic method to use in lambda expression?
Naman :
You should change your definition to
public interface Mapper<T> { // type bound to the interface
T map(T element);
}
and then use it as :
Mapper<Integer> mapper = element -> element * 2; // notice Integer and not 'int' for the type
which can also be written as :
Mapper<Integer> mapper = (Integer element) -> element * 2;