It is not quite just because you call negate()
. Take a look at this version, which is very close to yours but does compile:
Predicate<String> predicate = str -> str.length() <= 5;
names.removeIf(predicate.negate());
The difference between this and your version? It's about how lambda expressions get their types (the "target type").
What do you think this does?
(str -> str.length() <= 5).negate()?
Your current answer is that "it calls negate()
on the Predicate<String>
given by the expression str -> str.length() <= 5
". Right? But that's just because this is what you meant it to do. The compiler doesn't know that. Why? Because it could be anything. My own answer to the above question could be "it calls negate
on my other functional interface type... (yes, the example will be a little bizarre)
interface SentenceExpression {
boolean checkGrammar();
default SentenceExpression negate() {
return ArtificialIntelligence.contradict(explainSentence());
};
}
I could use the very same lambda expression names.removeIf((str -> str.length() <= 5).negate());
but meaning str -> str.length() <= 5
to be a SentenceExpression
rather than a Predicate<String>
.
Explanation: (str -> str.length() <= 5).negate()
does not make str -> str.length() <= 5
a Predicate<String>
. And this is why I said it could be anything, including my functional interface above.
Back to Java... This is why lambda expressions have the concept of "target type", which defines the mechanics by which a lambda expression is understood by the compiler as of a given functional interface type (i.e., how you help the compiler know that the expression is a Predicate<String>
rather than SentenceExpression
or anything else it could be). You may find it useful to read through What is meant by lambda target type and target type context in Java? and Java 8: Target typing
One of the contexts in which target types are inferred (if you read the answers on those posts) is the invocation context, where you pass a lambda expression as the argument for a parameter of a functional interface type, and that is what is applicable to names.removeIf(((str -> str.length() <= 5)));
: it's just the lambda expression given as argument to a method that takes a Predicate<String>
. This does not apply to the statement that is not compiling.
So, in other words...
names.removeIf(str -> str.length() <= 5);
uses a lambda expression in a place where the argument type clearly defines what the type of the lambda expression is expected to be (i.e., the target type of str -> str.length() <= 5
is clearly Predicate<String>
).
However, (str -> str.length() <= 5).negate()
is not a lambda expression, it's just an expression that happens to use a lambda expression. This is to say that str -> str.length() <= 5
in this case is not in the invocation context that determines the lambda expression's target type (as in the case of your last statement). Yes, the compiler knows that removeIf
needs a Predicate<String>
, and it knows for sure that the entire expression passed to the method has to be a Predicate<String>
, but it wouldn't assume that any lambda expression in the argument expression would be a Predicate<String>
(even if you treat it as a predicate by calling negate()
on it; it could have been anything that is compatible with the lambda expression).
That's why typing your lambda with an explicit cast (or otherwise, as in the first counter-example I gave) is needed.
Predicate<String> predicate = str -> str.length() <= 5; names.removeIf(predicate.negate()); names.removeIf(predicate);
negate()
be called upon if it's not being able to infer the type of the lambda attribute that is part of the expression and thus further unable to establish that the expression signifies aPredicate<String>
.