'Predicate::not' Coming to Java
There's a plan in place to let devs negate predicate lambdas (starting in JDK 11). The solution? Bringing in the static method Predicate::not.
Join the DZone community and get the full member experience.Join For Free
Jim Laskey's recent message "RFR: CSR - JDK-8203428 Predicate::not" on the OpenJDK core-libs-dev mailing list calls out JDK Bug JDK-8203428 ["Predicate::not"]. The "Summary" of JDK-8203428 states, "Introduce a new static method Predicate::not, which will allow developers to negate predicate lambdas trivially." It is currently assigned to JDK 11.
The "Problem" section of JDK-8203428 provides a succinct description of the issue that
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.
There is a highly illustrative example of how this would work in the JDK-8203428 write-up. The "Problem" section of JDK-8203428 provides code that demonstrates how "predicate negation" would be performed today and the "Solution" section provides code demonstrating how the same functionality could be implemented with the proposed static method
There are some other interesting messages in this mailing list thread. A Brian Goetz message in the thread states that "we did discover that default methods on [functional interfaces] combined with subtyping of [functional interfaces] caused trouble. But static methods are fine." A Rémi Forax message in the thread states that "StackOverflow has already decided that Predicate.not was the right method." A Sundararajan Athijegannathan message in the thread points out that "
not(String::isEmpty) reads almost like
A message on a different thread on the core-libs-dev mailing list adds this additional context regarding the motivation behind the addition of
Predicate.not(): "One of the rationals for adding 'Predicate.not' is to avoid adding another stream operation" because "we are trying to avoid adding 'overloaded' forms of the same kind of operation." This Paul Sandoz message provides the example that one would use the general
filter(not(String::isEmpty)) rather than a specific
The addition of static function
not(Predicate<T>) to Predicate is a small thing, but should improve the fluency of many lines of Java code.
Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.