If I have a method foo(Predicate bar), I can use it next:
foo(new Predicate() {
public boolean apply(MyObject obj) {
return true;
}
}
)
But how can I reach the same result using lambda-style expressions:
foo((MyObject obj) -> true); //obvious compile exception
How can I pass generic type with lambda into the method?
Or, simply, how can I create anonymous object of class Predicate(or others) using lambda-style without local variables.
解决方案
Don't use raw types foo(Predicate extends MyObject> bar)
Or cast inside lambda foo(obj -> ((MyObject) obj).myMethod())