Java : DoublePredicate - API使用例
DoublePredicate (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
DoublePredicate は、プリミティブ型 の double パラメータと、戻り値 boolean の関数型インタフェースです。
主に、DoubleStream の ラムダ式 として使われます。
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]
// ラムダ式の例です。
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]
メソッド
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]
// ラムダ式の例です。
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]
関連記事
- API 使用例