Core Java

Predicate::not Coming to Java

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 Predicate::not addresses:

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 Predicate::not.

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 !str.isEmpty()“.

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 on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: Predicate::not Coming to Java

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button