Java : BiPredicate with Examples

BiPredicate (Java SE 18 & JDK 18) API Examples.
You will find code examples on most BiPredicate methods.


Summary

Represents a predicate (boolean-valued function) of two arguments. This is the two-arity specialization of Predicate.

Class diagram

final var predicate = new BiPredicate<Integer, String>() {
    @Override
    public boolean test(Integer i, String s) {
        return Integer.decode(s).equals(i);
    }
};

final var ret1 = predicate.test(255, "0xff");
System.out.println(ret1); // true

final var ret2 = predicate.test(100, "0x100");
System.out.println(ret2); // false

Methods

default BiPredicate<T,U> and (BiPredicate<? super T,? super U> other)

Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.

final var predicate1 = new BiPredicate<String, String>() {
    @Override
    public boolean test(String s1, String s2) {
        return s1.equals(s2);
    }
};

System.out.println(predicate1.test("ABC", "XYZ")); // false
System.out.println(predicate1.test("ABC", "ABC")); // true
System.out.println(predicate1.test("def", "def")); // true

final var predicate2 = new BiPredicate<String, String>() {
    @Override
    public boolean test(String s1, String s2) {
        return s1.toUpperCase().equals(s1) && s2.toUpperCase().equals(s2);
    }
};

System.out.println(predicate2.test("ABC", "XYZ")); // true
System.out.println(predicate2.test("ABC", "ABC")); // true
System.out.println(predicate2.test("def", "def")); // false

final var and = predicate1.and(predicate2);

System.out.println(and.test("ABC", "XYZ")); // false
System.out.println(and.test("ABC", "ABC")); // true
System.out.println(and.test("def", "def")); // false

default BiPredicate<T,U> negate ()

Returns a predicate that represents the logical negation of this predicate.

final var predicate = new BiPredicate<String, String>() {
    @Override
    public boolean test(String s1, String s2) {
        return s1.equals(s2);
    }
};

System.out.println(predicate.test("abc", "abc")); // true
System.out.println(predicate.test("abc", "xyz")); // false

final var negate = predicate.negate();

System.out.println(negate.test("abc", "abc")); // false
System.out.println(negate.test("abc", "xyz")); // true

default BiPredicate<T,U> or (BiPredicate<? super T,? super U> other)

Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.

final var predicate1 = new BiPredicate<String, String>() {
    @Override
    public boolean test(String s1, String s2) {
        return s1.equals(s2);
    }
};

System.out.println(predicate1.test("ABC", "XYZ")); // false
System.out.println(predicate1.test("ABC", "ABC")); // true
System.out.println(predicate1.test("def", "def")); // true

final var predicate2 = new BiPredicate<String, String>() {
    @Override
    public boolean test(String s1, String s2) {
        return s1.toUpperCase().equals(s1) && s2.toUpperCase().equals(s2);
    }
};

System.out.println(predicate2.test("ABC", "XYZ")); // true
System.out.println(predicate2.test("ABC", "ABC")); // true
System.out.println(predicate2.test("def", "def")); // false

final var or = predicate1.or(predicate2);

System.out.println(or.test("ABC", "XYZ")); // true
System.out.println(or.test("ABC", "ABC")); // true
System.out.println(or.test("def", "def")); // true

boolean test (T t, U u)

Evaluates this predicate on the given arguments.

final var predicate = new BiPredicate<Integer, String>() {
    @Override
    public boolean test(Integer i, String s) {
        return Integer.decode(s).equals(i);
    }
};

final var ret1 = predicate.test(255, "0xff");
System.out.println(ret1); // true

final var ret2 = predicate.test(100, "0x100");
System.out.println(ret2); // false

Related posts

To top of page