Predicate.not() Method in Java with Examples

Java 11 introduces a tool called Predicate.not() to make negating predicates easier. Predicates, which are frequently employed in filtering and conditional logic, are functional interfaces that express boolean-valued functions of a single argument. Negating a predicate in Java 11 required a significantly lengthier method. Predicate.not() makes the syntax clearer and easier to read. In this section, we will discuss the Predicate.not() method in detail, along with examples to show how to use it.

What is a Predicate?

A functional interface defined in the java.util.function package is known as a predicate in Java. It stands for a function with a single argument that yields a boolean result. Test(T t), the main method in the Predicate interface, assesses the predicate based on the supplied argument.

Traditional Predicate Negation

Prior to Java 11, the negate() method was required in order to negate a predicate. Negating a predicate that determines whether a string is empty, for instance, would look like this:

Although this method is effective, it can be laborious when combining several predicates or using it in more complicated expressions.

Introduction of Predicate.not()

Java 11 introduced the Predicate.not() method, providing a more straightforward way to negate predicates. The Predicate.not() method is a static method that takes a predicate and returns its negation.

This method simplifies the syntax for negating predicates, making the code more readable and concise.

Advantages of Predicate.not() Method

  1. Readability: The Predicate.not() method makes the code more readable by reducing the verbosity associated with negating predicates.
  2. Conciseness: It simplifies the expression of negated predicates, making the code cleaner and more concise.
  3. Consistency: It provides a consistent approach to negating predicates, which is especially useful when working with streams and complex conditional logic.

Example 1: Filtering a List of Strings

Suppose, we have a list of strings, and we want to filter out the empty strings. Using Predicate.not(), we can achieve this as follows:

File Name: PredicateNotExample.java

Output:

[Java, Predicate, Not]

Explanation

The PredicateNotExample class shows how to filter out empty strings from a list using the Predicate.not() function. The first step of the class is to import the packages needed to work with streams, predicates, and lists. The main method creates a list of strings that includes both empty and non-empty strings.

After converting this list to a stream, the code excludes empty strings using Predicate.not(String::isEmpty) in conjunction with the filter() method. Comparing this to other techniques, the negative is clearer and easier to comprehend. The output [Java, Predicate, Not] is produced by gathering the filtered results back into a list and printing it.

Example 2: Negating a Custom Predicate

Consider a custom predicate that checks if a number is even. Using Predicate.not(), you can easily create a predicate to check for odd numbers:

File Name: PredicateNotExample.java

Output:

[1, 3, 5]

Explanation

The PredicateNotExample class provides an example of how to filter out even numbers from a list using the Predicate.not() function, so retaining just the odd numbers. The class imports the essential predicates, stream operations, and list handling packages.

A predicate called isEven is defined in the main() method to determine whether a given number is even. Next, an integer list ranging from 1 to 6 is generated. After converting this list to a stream, Predicate.not(isEven) is used in the filter method to remove even numbers from the list, leaving only odd numbers. The output is produced by compiling the filtered stream back into a list and printing it [1, 3, 5].

Conclusion

Predicate negation code can now be written in a more expressive and readable manner thanks to the addition of the Predicate.not() method to the Java 11 standard library. Developers can build more readable and concise code by utilising Predicate.not(), especially when working with streams and conditional logic. Predicate.not() provides a simple and elegant predicate negation solution, whether you're filtering collections or defining complex conditions.