Why does the below code return Predicate and not boolean?
This is because the type of the function (String s) -> !s.isEmpty()
is a Predicate<String>
, at this point you're simply defining a function (which says "given a String as input return a boolean value indication whether it's empty or not).
Note that at this point you're not evaluating anything hence the result is not a boolean but rather a function.
Definition of FI from the doc:
Functional interfaces provide target types for lambda expressions and
method references. Each functional interface has a single abstract
method, called the functional method for that functional interface, to
which the lambda expression's parameter and return types are matched
or adapted. Functional interfaces can provide a target type in
multiple contexts, such as assignment context, method invocation, or
cast context:
in order to get the "boolean result" you're seeking, you must first invoke the "functional method". example:
Predicate<String> nonEmptyStringPredicate = s -> !s.isEmpty();
boolean result = nonEmptyStringPredicate.test("");