Java Stream How to - Create Predicate from Predicate.negate()








Question

We would like to know how to create Predicate from Predicate.negate().

Answer

import java.util.function.Predicate;
//  w w  w . j a  va  2  s  .  com
public class Main {
  public static void main(String... args) {
    Predicate<String> isEmpty = String::isEmpty;
    System.out.println(isEmpty.test(""));

    Predicate<String> isNotEmpty = isEmpty.negate();
    
    System.out.println(isNotEmpty.test(""));
  }
}

The code above generates the following result.