If I have a method foo(Predicate bar)
, I can use it next:
foo(new Predicate<MyObject>() {
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())
If your target type is a raw type like in your invocation of
foo(Predicate bar)
, you can’t expect lambda expressions to magically generate an appropriate type for you. It works with the anonymous inner class, because that declaration has an explicit type (Predicate<MyObject> {}
) which can get passed toPredicate
in an unsafe operation. But lambda expressions have no type. They are converted to a target type and in your invocation, the target type isPredicate
.If you want to create a
Predicate<MyObject>
via lambda expression, you needPredicate<MyObject>
as it’s target type:or
or
Of course, in all these cases, you could also write
(MyObject o) -> true
, but this explicit declaration of the parameter has no influence on the type of the instance created with this lambda expression.Note that the second variant has the formal structure of a type cast, but there won’t be a type cast operation at runtime. It’s only there for specifying the target type for the lambda expression (similar to widening casts used to select a particular overload in a method invocation).