Java : Predicate with Examples

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


Summary

Represents a predicate (boolean-valued function) of one argument.

Class diagram

final var predicate = new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.equals(s.toUpperCase());
    }
};

final var stream = Stream.of("aaa", "BBB", "ccc", "DDD");

final var ret = stream.filter(predicate).toList();
System.out.println(ret); // [BBB, DDD]
// An example with a lambda expression.
final var stream = Stream.of("aaa", "BBB", "ccc", "DDD");

final var ret = stream.filter(s -> s.equals(s.toUpperCase())).toList();
System.out.println(ret); // [BBB, DDD]

Methods

default Predicate<T> and (Predicate<? super T> other)

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

final var predicate1 = new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.equals(s.toUpperCase());
    }
};

System.out.println(predicate1.test("abcd")); // false
System.out.println(predicate1.test("ABCD")); // true
System.out.println(predicate1.test("XYZ")); // true

final var predicate2 = new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.equalsIgnoreCase("abcd");
    }
};

System.out.println(predicate2.test("abcd")); // true
System.out.println(predicate2.test("ABCD")); // true
System.out.println(predicate2.test("XYZ")); // false

final var and = predicate1.and(predicate2);

System.out.println(and.test("abcd")); // false
System.out.println(and.test("ABCD")); // true
System.out.println(and.test("XYZ")); // false

static <T> Predicate<T> isEqual (Object targetRef)

Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).

final var predicate = Predicate.<String>isEqual("abcd");

System.out.println(predicate.test("abcd")); // true
System.out.println(predicate.test("xyz")); // false
final var predicate = Predicate.<Integer>isEqual(123);

System.out.println(predicate.test(123)); // true
System.out.println(predicate.test(456)); // false

default Predicate<T> negate ()

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

final var predicate = new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.equals(s.toUpperCase());
    }
};

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

final var negate = predicate.negate();

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

static <T> Predicate<T> not (Predicate<? super T> target)

Returns a predicate that is the negation of the supplied predicate.

final var predicate = new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.equals(s.toUpperCase());
    }
};

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

final var not = Predicate.not(predicate);

System.out.println(not.test("ABCD")); // false
System.out.println(not.test("xyz")); // true

default Predicate<T> or (Predicate<? super T> other)

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

final var predicate1 = new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.equals(s.toUpperCase());
    }
};

System.out.println(predicate1.test("abcd")); // false
System.out.println(predicate1.test("ABCD")); // true
System.out.println(predicate1.test("XYZ")); // true

final var predicate2 = new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.equalsIgnoreCase("abcd");
    }
};

System.out.println(predicate2.test("abcd")); // true
System.out.println(predicate2.test("ABCD")); // true
System.out.println(predicate2.test("XYZ")); // false

final var or = predicate1.or(predicate2);

System.out.println(or.test("abcd")); // true
System.out.println(or.test("ABCD")); // true
System.out.println(or.test("XYZ")); // true

boolean test (T t)

Evaluates this predicate on the given argument.

final var predicate = new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.equals(s.toUpperCase());
    }
};

System.out.println(predicate.test("ABCD")); // true
System.out.println(predicate.test("xyz")); // false
// An example with a lambda expression.
final var stream = Stream.of("aaa", "BBB", "ccc", "DDD");

final var ret = stream.filter(s -> s.equals(s.toUpperCase())).toList();
System.out.println(ret); // [BBB, DDD]

Related posts

To top of page