Java : DoublePredicate with Examples

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


Summary

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

Class diagram

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return value < 0.0;
    }
};

final var stream = DoubleStream.of(0.1, -0.2, 0.3, -0.4);

final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [-0.2, -0.4]
// An example with a lambda expression.
final var stream = DoubleStream.of(0.1, -0.2, 0.3, -0.4);

final var ret = stream.filter(v -> v < 0.0).toArray();
System.out.println(Arrays.toString(ret)); // [-0.2, -0.4]

Methods

default DoublePredicate and (DoublePredicate other)

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

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return value < 0.0;
    }
};

System.out.println(predicate.test(0.1)); // false
System.out.println(predicate.test(-0.2)); // true
System.out.println(predicate.test(10.3)); // false
System.out.println(predicate.test(-10.4)); // true

final var other = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return Math.abs(value) < 10.0;
    }
};

System.out.println(other.test(0.1)); // true
System.out.println(other.test(-0.2)); // true
System.out.println(other.test(10.3)); // false
System.out.println(other.test(-10.4)); // false

final var and = predicate.and(other);

System.out.println(and.test(0.1)); // false
System.out.println(and.test(-0.2)); // true
System.out.println(and.test(10.3)); // false
System.out.println(and.test(-10.4)); // false

default DoublePredicate negate ()

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

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return value < 0.0;
    }
};

System.out.println(predicate.test(0.1)); // false
System.out.println(predicate.test(-0.2)); // true
System.out.println(predicate.test(0.3)); // false
System.out.println(predicate.test(-0.4)); // true

final var negate = predicate.negate();

System.out.println(negate.test(0.1)); // true
System.out.println(negate.test(-0.2)); // false
System.out.println(negate.test(0.3)); // true
System.out.println(negate.test(-0.4)); // false

default DoublePredicate or (DoublePredicate other)

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

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return value < 0.0;
    }
};

System.out.println(predicate.test(0.1)); // false
System.out.println(predicate.test(-0.2)); // true
System.out.println(predicate.test(10.3)); // false
System.out.println(predicate.test(-10.4)); // true

final var other = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return Math.abs(value) < 10.0;
    }
};

System.out.println(other.test(0.1)); // true
System.out.println(other.test(-0.2)); // true
System.out.println(other.test(10.3)); // false
System.out.println(other.test(-10.4)); // false

final var or = predicate.or(other);

System.out.println(or.test(0.1)); // true
System.out.println(or.test(-0.2)); // true
System.out.println(or.test(10.3)); // false
System.out.println(or.test(-10.4)); // true

boolean test (double value)

Evaluates this predicate on the given argument.

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return value < 0.0;
    }
};

final var stream = DoubleStream.of(0.1, -0.2, 0.3, -0.4);

final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [-0.2, -0.4]
// An example with a lambda expression.
final var stream = DoubleStream.of(0.1, -0.2, 0.3, -0.4);

final var ret = stream.filter(v -> v < 0.0).toArray();
System.out.println(Arrays.toString(ret)); // [-0.2, -0.4]

Related posts

To top of page