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