Java : IntPredicate with Examples

IntPredicate (Java SE 21 & JDK 21) with Examples.
You will find code examples on most IntPredicate methods.


Summary

Represents a predicate (boolean-valued function) of one int-valued argument. This is the int-consuming primitive type specialization of Predicate.

Class diagram

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        // Check if the number is even.
        return value % 2 == 0;
    }
};

final var stream = IntStream.of(1, 2, 3, 4);

final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4]
// An example with a lambda expression.
final var stream = IntStream.of(1, 2, 3, 4);

final var ret = stream.filter(v -> v % 2 == 0).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4]

Methods

default IntPredicate and (IntPredicate other)

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

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        // Check if the number is even.
        return value % 2 == 0;
    }
};

System.out.println(predicate.test(1)); // false
System.out.println(predicate.test(2)); // true
System.out.println(predicate.test(13)); // false
System.out.println(predicate.test(14)); // true

final var other = new IntPredicate() {
    @Override
    public boolean test(int value) {
        return value < 10;
    }
};

System.out.println(other.test(1)); // true
System.out.println(other.test(2)); // true
System.out.println(other.test(13)); // false
System.out.println(other.test(14)); // false

final var and = predicate.and(other);

System.out.println(and.test(1)); // false
System.out.println(and.test(2)); // true
System.out.println(and.test(13)); // false
System.out.println(and.test(14)); // false

default IntPredicate negate ()

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

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        // Check if the number is even.
        return value % 2 == 0;
    }
};

System.out.println(predicate.test(1)); // false
System.out.println(predicate.test(2)); // true
System.out.println(predicate.test(3)); // false
System.out.println(predicate.test(4)); // true

final var negate = predicate.negate();

System.out.println(negate.test(1)); // true
System.out.println(negate.test(2)); // false
System.out.println(negate.test(3)); // true
System.out.println(negate.test(4)); // false

default IntPredicate or (IntPredicate other)

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

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        // Check if the number is even.
        return value % 2 == 0;
    }
};

System.out.println(predicate.test(1)); // false
System.out.println(predicate.test(2)); // true
System.out.println(predicate.test(13)); // false
System.out.println(predicate.test(14)); // true

final var other = new IntPredicate() {
    @Override
    public boolean test(int value) {
        return value < 10;
    }
};

System.out.println(other.test(1)); // true
System.out.println(other.test(2)); // true
System.out.println(other.test(13)); // false
System.out.println(other.test(14)); // false

final var or = predicate.or(other);

System.out.println(or.test(1)); // true
System.out.println(or.test(2)); // true
System.out.println(or.test(13)); // false
System.out.println(or.test(14)); // true

boolean test (int value)

Evaluates this predicate on the given argument.

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        // Check if the number is even.
        return value % 2 == 0;
    }
};

final var stream = IntStream.of(1, 2, 3, 4);

final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4]
// An example with a lambda expression.
final var stream = IntStream.of(1, 2, 3, 4);

final var ret = stream.filter(v -> v % 2 == 0).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4]

Related posts

To top of page