Java Stream How to - Use Objects::isNull as method reference for Predicate








Question

We would like to know how to use Objects::isNull as method reference for Predicate.

Answer

import java.util.Objects;
import java.util.function.Predicate;
//ww  w . ja  va  2  s .com
public class Main {
  public static void main(String... args) {
    Predicate<Boolean> nonNull = Objects::isNull;
    System.out.println(nonNull.test(true));
    System.out.println(nonNull.negate().test(false));
  }
}

The code above generates the following result.