Java : DoublePredicate with Examples
DoublePredicate (Java SE 21 & JDK 21) with Examples.
You will find code examples on most DoublePredicate methods.
Summary
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)
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 ()
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)
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)
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]