Summary
Introduce a new static method Predicate::not which will allow developers to negate predicate lambdas trivially.
Problem
The requirement for predicate negation occurs frequently since predicates are defined antipodal to a positive selection; isNull, isEmpty, isBlank.
Presently there is no easy way to negate a predicate lambda without first wrapping in a Predicate Object.
Ex.
List<String> list = bufferedReader
.lines()
.filter(((Predicate<String>)String::isEmpty).negate())
.collect(toList());
Solution
Introduce a method that accepts an unwrapped lambda and returns the negated form.
Ex.
import static java.util.function.Predicate.not;
...
List<String> list = bufferedReader
.lines()
.filter(not(String::isEmpty))
.collect(toList());
Specification
/**
* Returns a predicate that is the negation of the
* supplied predicate.
* @param <T> the type of arguments to the
* specified predicate
* @param target predicate to negate
*
* @return a predicate that negates the results
* of the supplied predicate
*
* @since 11
*/
static <T> Predicate<T> not(Predicate<? super T> target) {
return (Predicate<T>)target.negate();
}
- csr of
-
JDK-8050818 Predicate::not - provide an easier way to negate a predicate
-
- Resolved
-
- relates to
-
JDK-8204172 Predicate::not should explicitly mention "NullPointerException - if target is null"
-
- Resolved
-