Negate a Java Predicate and Method Reference

Examples of using the negate() and not() methods of the Predicate to negate a Java Predicate or a Method Reference.

Overview

Java Predicate, a functional interface, represents a single-argument function that results in a Boolean value. Predicates are helpful when we want to supply a condition to filter a Java Collection or Stream.

This article discusses negating a Predicate or a Method Reference in Java. When we negate a Predicate, it will reverse its condition to match the elements excluded by the original Predicate condition.

Negate a Predicate using negate()

The Java Predicate class’s static ‘negate()‘ method negates this predicate condition.

Consider we have a Predicate that matches all even numbers.

Predicate<Integer> evenNumber = i -> i % 2 == 0;Code language: Java (java)

We can use it to find all even numbers from a Java Stream of integers.

Stream.of(1, 2, 3, 4, 5, 6)
    .filter(evenNumber)
    .forEach(System.out::print);
//prints

//246Code language: Java (java)

To reverse or negate the Stream filter criteria, we have to use the negate() method of the Predicate instance.

Example of using the negate() method to negate a Java Predicate

Stream.of(1, 2, 3, 4, 5, 6)
    .filter(evenNumber.negate())
    .forEach(System.out::print);
//prints
//135
Code language: Java (java)

Negate a Predicate using not()

Alternatively, we can use the static not() method in the Predicate class that negates the given Predicate condition.

Example of Predicate’s not() method to negate the given Predicate.

Stream.of(1, 2, 3, 4, 5, 6)
    .filter(Predicate.not(evenNumber))
    .forEach(System.out::print);
//prints
//135Code language: Java (java)

Negate a Method Reference using not()

Lambda expressions help us create anonymous methods, which sometimes do nothing but invoke another method. Java’s method reference help to use a short-hand notation instead of Lambda expressions.

Consider a Person class with a boolean field indicating if the person is an Adult.

public class Person {
  private final String name;
  private final Boolean adult;

  public String getName() {
    return name;
  }

  public Boolean getAdult() {
    return adult;
  }
}Code language: Java (java)

Create a Stream containing Persons, both adults and minors.

Stream<Person> stream = Stream.of(
    new Person("Bob", true),
    new Person("Tom", false),
    new Person("Cob", false),
    new Person("Rick", true)
);Code language: Java (java)

We can use the method reference on the Stream to filter to get a Stream of adult persons.

Stream<Person> adults = 
    stream.filter(Person::getAdult);Code language: Java (java)

In contrast, if we want to have a Stream of minor persons, we can negate the same method reference using the static not() method.

Example of negating a Java Method Reference.

Stream<Person> minors =
    stream.filter(Predicate.not(Person::getAdult));

minors.forEach(System.out::println);
//prints
//Person(name=Tom, adult=false)
//Person(name=Cob, adult=false)Code language: Java (java)

Summary

This article discussed different ways of negating a Java Predicate or a Java Method Reference. We underwent an example of using Java Predicate’s negate() method that can reverse the condition specified by this Predicate. Then we introduced the static not() method that can negate the given Predicate, including the given Method Reference.

You can refer to our GitHub Repository for the complete source code of the examples used in this tutorial.