Java Reference ยท Streams and Functional
Predicate and BiPredicate interfaces
Reference for java.util.function.Predicate and BiPredicate: test(), and(), or(), negate(), not(), isEqual() and how they work with filter and removeIf.
Predicate<T> is a functional interface for a yes/no question about one value. Its abstract method is boolean test(T t). BiPredicate<T, U> asks the question about two values with boolean test(T t, U u).
Predicates are what you pass to Stream.filter(), Collection.removeIf(), Optional.filter(), Stream.takeWhile(), anyMatch() and Collectors.partitioningBy(). They can be combined with and(), or() and negate(), so you can build complex conditions from small named pieces.
Predicate<String> notBlank = s -> !s.isBlank(); boolean ok = notBlank.test("hi"); // true
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
@FunctionalInterface public interface Predicate<T> { boolean test(T t); default Predicate<T> and(Predicate<? super T> other); default Predicate<T> or(Predicate<? super T> other); default Predicate<T> negate(); static <T> Predicate<T> isEqual(Object targetRef); static <T> Predicate<T> not(Predicate<? super T> target); // Java 11 } @FunctionalInterface public interface BiPredicate<T, U> { boolean test(T t, U u); default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other); default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other); default BiPredicate<T, U> negate(); }
| Member | Description |
|---|---|
test(t) |
Evaluates the condition. |
and(other) |
True only if both are true; short-circuits, so other is skipped when this is false. |
or(other) |
True if either is true; other is skipped when this is already true. |
negate() |
The logical opposite. |
isEqual(x) |
Predicate that tests Objects.equals(x, input), so null is handled. |
not(p) |
Static version of negate(), handy with method references (since Java 11). |
Usage notes
- Method references cannot be negated directly.
String::isBlank.negate()does not compile because a method reference needs a target type; writePredicate.not(String::isBlank)or a lambda. - Short-circuiting:
and/orevaluate the right-hand predicate lazily, just like&&and||, so a null check first is safe. - Primitive versions:
IntPredicate,LongPredicateandDoublePredicateavoid boxing; see primitive interfaces. - Side effects: predicates passed to streams should not modify state or depend on order.
- Null handling:
test(null)is the predicate's own business; most lambdas throwNullPointerException. - Naming: assign complex conditions to well-named variables such as
isEligible, which beats long inline lambdas.