Java : OptionalDouble - API使用例
OptionalDouble (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
OptionalDouble クラスは、プリミティブ型 の double を扱う Optional です。
関連記事 : Optionalの基本
本記事のコード例では、以下の DoubleSample クラスを使います。
public class DoubleSample {
private final double value;
public DoubleSample() {
this.value = -1.0;
}
public DoubleSample(double value) {
if (value < 0.0) {
throw new IllegalArgumentException();
}
this.value = value;
}
public OptionalDouble getValue() {
if (value < 0.0) {
return OptionalDouble.empty();
} else {
return OptionalDouble.of(value);
}
}
}
final var sample = new DoubleSample(1.23);
sample.getValue().ifPresent(value -> {
System.out.println("value : " + value);
});
// 結果
// ↓
//value : 1.23
final var sample = new DoubleSample();
sample.getValue().ifPresentOrElse(value -> {
System.out.println("value : " + value);
}, () -> {
System.out.println("value : empty!");
});
// 結果
// ↓
//value : empty!
メソッド
static OptionalDouble empty ()
final var opt = OptionalDouble.empty();
System.out.println(opt); // OptionalDouble.empty
System.out.println(opt.isEmpty()); // true
final var opt = OptionalDouble.empty();
try {
final var value = opt.orElseThrow();
} catch (NoSuchElementException e) {
System.out.println(e);
}
// 結果
// ↓
//java.util.NoSuchElementException: No value present
boolean equals (Object obj)
final var opt1 = OptionalDouble.of(100.0);
final var opt2 = OptionalDouble.of(100.0);
final var opt3 = OptionalDouble.of(999.0);
System.out.println(opt1.equals(opt2)); // true
System.out.println(opt1.equals(opt3)); // false
final var opt1 = OptionalDouble.of(1.23);
final var opt2 = OptionalDouble.empty();
System.out.println(opt1.equals(opt2)); // false
final var opt1 = OptionalDouble.empty();
final var opt2 = OptionalDouble.empty();
System.out.println(opt1.equals(opt2)); // true
double getAsDouble ()
final var sample = new DoubleSample(1.23);
final var opt = sample.getValue();
if (opt.isPresent()) {
System.out.println(opt.getAsDouble()); // 1.23
}
final var sample = new DoubleSample();
final var opt = sample.getValue();
try {
final var value = opt.getAsDouble();
} catch (NoSuchElementException e) {
System.out.println(e);
}
// 結果
// ↓
//java.util.NoSuchElementException: No value present
int hashCode ()
final var ret1 = OptionalDouble.empty().hashCode();
System.out.println(ret1); // 0
final var ret2 = OptionalDouble.of(1.23).hashCode();
System.out.println(ret2); // 1158867386
final var ret3 = OptionalDouble.of(-0.789).hashCode();
System.out.println(ret3); // 1383618319
void ifPresent (DoubleConsumer action)
final var sample = new DoubleSample(1.23);
System.out.println("-- ifPresent --");
sample.getValue().ifPresent(value -> {
System.out.println(" action : " + value);
});
System.out.println("-- end --");
// 結果
// ↓
//-- ifPresent --
// action : 1.23
//-- end --
final var sample = new DoubleSample();
System.out.println("-- ifPresent --");
sample.getValue().ifPresent(value -> {
System.out.println(" action : " + value);
});
System.out.println("-- end --");
// 結果
// ↓
//-- ifPresent --
//-- end --
void ifPresentOrElse (DoubleConsumer action, Runnable emptyAction)
final var sample = new DoubleSample(1.23);
System.out.println("-- ifPresentOrElse --");
sample.getValue().ifPresentOrElse(value -> {
System.out.println(" action : " + value);
}, () -> {
System.out.println(" emptyAction!");
});
System.out.println("-- end --");
// 結果
// ↓
//-- ifPresentOrElse --
// action : 1.23
//-- end --
final var sample = new DoubleSample();
System.out.println("-- ifPresentOrElse --");
sample.getValue().ifPresentOrElse(value -> {
System.out.println(" action : " + value);
}, () -> {
System.out.println(" emptyAction!");
});
System.out.println("-- end --");
// 結果
// ↓
//-- ifPresentOrElse --
// emptyAction!
//-- end --
boolean isEmpty ()
final var sample = new DoubleSample(1.23);
if (sample.getValue().isEmpty()) {
System.out.println("empty!");
} else {
System.out.println("present!");
}
// 結果
// ↓
//present!
final var sample = new DoubleSample();
if (sample.getValue().isEmpty()) {
System.out.println("empty!");
} else {
System.out.println("present!");
}
// 結果
// ↓
//empty!
boolean isPresent ()
final var sample = new DoubleSample(1.23);
if (sample.getValue().isPresent()) {
System.out.println("present!");
} else {
System.out.println("empty!");
}
// 結果
// ↓
//present!
final var sample = new DoubleSample();
if (sample.getValue().isPresent()) {
System.out.println("present!");
} else {
System.out.println("empty!");
}
// 結果
// ↓
//empty!
static OptionalDouble of (double value)
final var opt1 = OptionalDouble.of(1.23);
System.out.println(opt1); // OptionalDouble[1.23]
final var opt2 = OptionalDouble.of(-0.789);
System.out.println(opt2); // OptionalDouble[-0.789]
double orElse (double other)
final var sample = new DoubleSample(100.0);
final var ret = sample.getValue().orElse(9999.0);
System.out.println(ret); // 100.0
final var sample = new DoubleSample();
final var ret = sample.getValue().orElse(9999.0);
System.out.println(ret); // 9999.0
double orElseGet (DoubleSupplier supplier)
final var sample = new DoubleSample(100.0);
final var ret = sample.getValue().orElseGet(() -> {
System.out.println("action!");
return 9999.0;
});
System.out.println("ret = " + ret);
// 結果
// ↓
//ret = 100.0
final var sample = new DoubleSample();
final var ret = sample.getValue().orElseGet(() -> {
System.out.println("action!");
return 9999.0;
});
System.out.println("ret = " + ret);
// 結果
// ↓
//action!
//ret = 9999.0
double orElseThrow ()
final var sample = new DoubleSample(1.23);
final var opt = sample.getValue();
if (opt.isPresent()) {
System.out.println(opt.orElseThrow()); // 1.23
}
final var sample = new DoubleSample();
final var opt = sample.getValue();
try {
final var value = opt.orElseThrow();
} catch (NoSuchElementException e) {
System.out.println(e);
}
// 結果
// ↓
//java.util.NoSuchElementException: No value present
<X extends Throwable> double orElseThrow (Supplier<? extends X> exceptionSupplier)
final var sample = new DoubleSample(1.23);
final var opt = sample.getValue();
if (opt.isPresent()) {
final var value = opt.orElseThrow(IllegalStateException::new);
System.out.println(value); // 1.23
}
final var sample = new DoubleSample();
try {
final var value = sample.getValue().orElseThrow(() -> {
throw new IllegalStateException("empty!");
});
} catch (IllegalStateException e) {
System.out.println(e);
}
// 結果
// ↓
//java.lang.IllegalStateException: empty!
DoubleStream stream ()
final var sample = new DoubleSample(1.23);
final var array = sample.getValue().stream().toArray();
System.out.println(Arrays.toString(array)); // [1.23]
final var count = sample.getValue().stream().count();
System.out.println(count); // 1
final var sample = new DoubleSample();
final var array = sample.getValue().stream().toArray();
System.out.println(Arrays.toString(array)); // []
final var count = sample.getValue().stream().count();
System.out.println(count); // 0
String toString ()
final var ret1 = OptionalDouble.empty().toString();
System.out.println(ret1); // OptionalDouble.empty
final var ret2 = OptionalDouble.of(1.23).toString();
System.out.println(ret2); // OptionalDouble[1.23]
final var ret3 = OptionalDouble.of(-0.789).toString();
System.out.println(ret3); // OptionalDouble[-0.789]
関連記事
- API 使用例