Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

BiPredicate<T, U> is Java’s standard functional interface for a condition that accepts two inputs and returns a boolean. Call test(first, second) to evaluate it, or combine two checks with and, or, and negate. It has been part of Java since Java 8.

What BiPredicate represents

Oracle defines BiPredicate as a predicate—a boolean-valued function—of two arguments. Its type parameters can be different reference types, such as a String and an Integer. Its functional method is test(T, U), which returns true or false.

import java.util.function.BiPredicate;

BiPredicate<String, Integer> nameAndAge =
    (name, age) -> name.startsWith("A") && age >= 18;

boolean allowed = nameAndAge.test("Ari", 21);

Here, test passes both arguments to the lambda and stores its boolean result in allowed. The interface is useful when a condition depends naturally on a pair—for example, a key and value or a name and age.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to compose two-input conditions

Use and and or to combine checks that accept the same two inputs. Use negate to create a predicate that returns the opposite result.

BiPredicate<String, Integer> hasName =
    (name, age) -> name != null && !name.isBlank();
BiPredicate<String, Integer> adult =
    (name, age) -> age >= 18;

BiPredicate<String, Integer> eligible = hasName.and(adult);
boolean result = eligible.test("Ari", 21);

The named checks make the combined rule easier to read and reuse. In this example, hasName checks for a non-null, nonblank name before the age check runs.

Short-circuiting, exceptions, and null predicates

Composition preserves Java’s short-circuit evaluation. With left.and(right), Java skips right if left returns false. With left.or(right), it skips right if left returns true. If an evaluated predicate throws an exception, that exception reaches the caller.

Passing null as the other predicate to and or or throws NullPointerException. Keep predicate bodies straightforward: composition combines their boolean results, but it does not remove the effects or exception risks of the logic inside them. Oracle’s Java SE 25 BiPredicate API documents these method contracts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

BiPredicate vs. Predicate

Choose Predicate<T> when a condition needs one input; choose BiPredicate<T, U> when it needs two. Their functional methods have different arities, so a BiPredicate cannot be passed directly to an API that expects a Predicate.

If an operation processes one value at a time but your condition also needs a second, fixed value, a lambda can capture that value and adapt the logic. For example, a stream filter over names could use a predicate that closes over a chosen minimum age only if each stream element supplies the needed data; a two-input predicate itself is not automatically converted into a one-input predicate. Dev.java’s guide to lambda expressions discusses Java’s functional-interface model.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Primitive inputs and BiPredicate

Java provides single-input primitive specializations such as IntPredicate, but it does not provide a specialized primitive BiPredicate interface. A BiPredicate uses reference-type parameters, so primitive values used with it are boxed. If avoiding boxing is important, consider whether another API shape or a small custom interface better fits the operation. Dev.java covers the distinction between functional interfaces and primitive specializations.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.