Java : OptionalInt with Examples
OptionalInt (Java SE 21 & JDK 21) with Examples.
You will find code examples on most OptionalInt methods.
Summary
Code examples on this page uses the IntSample class below.
public class IntSample {
private final int value;
public IntSample() {
this.value = -1;
}
public IntSample(int value) {
if (value < 0) {
throw new IllegalArgumentException();
}
this.value = value;
}
public OptionalInt getValue() {
if (value == -1) {
return OptionalInt.empty();
} else {
return OptionalInt.of(value);
}
}
}
final var sample = new IntSample(123);
sample.getValue().ifPresent(value -> {
System.out.println("value : " + value);
});
// Result
// ↓
//value : 123
final var sample = new IntSample();
sample.getValue().ifPresentOrElse(value -> {
System.out.println("value : " + value);
}, () -> {
System.out.println("value : empty!");
});
// Result
// ↓
//value : empty!
Methods
static OptionalInt empty ()
final var opt = OptionalInt.empty();
System.out.println(opt); // OptionalInt.empty
System.out.println(opt.isEmpty()); // true
final var opt = OptionalInt.empty();
try {
final var value = opt.orElseThrow();
} catch (NoSuchElementException e) {
System.out.println(e);
}
// Result
// ↓
//java.util.NoSuchElementException: No value present
boolean equals (Object obj)
final var opt1 = OptionalInt.of(100);
final var opt2 = OptionalInt.of(100);
final var opt3 = OptionalInt.of(999);
System.out.println(opt1.equals(opt2)); // true
System.out.println(opt1.equals(opt3)); // false
final var opt1 = OptionalInt.of(123);
final var opt2 = OptionalInt.empty();
System.out.println(opt1.equals(opt2)); // false
final var opt1 = OptionalInt.empty();
final var opt2 = OptionalInt.empty();
System.out.println(opt1.equals(opt2)); // true
int getAsInt ()
final var sample = new IntSample(123);
final var opt = sample.getValue();
if (opt.isPresent()) {
System.out.println(opt.getAsInt()); // 123
}
final var sample = new IntSample();
final var opt = sample.getValue();
try {
final var value = opt.getAsInt();
} catch (NoSuchElementException e) {
System.out.println(e);
}
// Result
// ↓
//java.util.NoSuchElementException: No value present
int hashCode ()
final var ret1 = OptionalInt.empty().hashCode();
System.out.println(ret1); // 0
final var ret2 = OptionalInt.of(123).hashCode();
System.out.println(ret2); // 123
final var ret3 = OptionalInt.of(-789).hashCode();
System.out.println(ret3); // -789
void ifPresent (IntConsumer action)
final var sample = new IntSample(123);
System.out.println("-- ifPresent --");
sample.getValue().ifPresent(value -> {
System.out.println(" action : " + value);
});
System.out.println("-- end --");
// Result
// ↓
//-- ifPresent --
// action : 123
//-- end --
final var sample = new IntSample();
System.out.println("-- ifPresent --");
sample.getValue().ifPresent(value -> {
System.out.println(" action : " + value);
});
System.out.println("-- end --");
// Result
// ↓
//-- ifPresent --
//-- end --
void ifPresentOrElse (IntConsumer action, Runnable emptyAction)
final var sample = new IntSample(123);
System.out.println("-- ifPresentOrElse --");
sample.getValue().ifPresentOrElse(value -> {
System.out.println(" action : " + value);
}, () -> {
System.out.println(" emptyAction!");
});
System.out.println("-- end --");
// Result
// ↓
//-- ifPresentOrElse --
// action : 123
//-- end --
final var sample = new IntSample();
System.out.println("-- ifPresentOrElse --");
sample.getValue().ifPresentOrElse(value -> {
System.out.println(" action : " + value);
}, () -> {
System.out.println(" emptyAction!");
});
System.out.println("-- end --");
// Result
// ↓
//-- ifPresentOrElse --
// emptyAction!
//-- end --
boolean isEmpty ()
final var sample = new IntSample(123);
if (sample.getValue().isEmpty()) {
System.out.println("empty!");
} else {
System.out.println("present!");
}
// Result
// ↓
//present!
final var sample = new IntSample();
if (sample.getValue().isEmpty()) {
System.out.println("empty!");
} else {
System.out.println("present!");
}
// Result
// ↓
//empty!
boolean isPresent ()
final var sample = new IntSample(123);
if (sample.getValue().isPresent()) {
System.out.println("present!");
} else {
System.out.println("empty!");
}
// Result
// ↓
//present!
final var sample = new IntSample();
if (sample.getValue().isPresent()) {
System.out.println("present!");
} else {
System.out.println("empty!");
}
// Result
// ↓
//empty!
static OptionalInt of (int value)
final var opt1 = OptionalInt.of(123);
System.out.println(opt1); // OptionalInt[123]
final var opt2 = OptionalInt.of(-789);
System.out.println(opt2); // OptionalInt[-789]
int orElse (int other)
final var sample = new IntSample(100);
final var ret = sample.getValue().orElse(9999);
System.out.println(ret); // 100
final var sample = new IntSample();
final var ret = sample.getValue().orElse(9999);
System.out.println(ret); // 9999
int orElseGet (IntSupplier supplier)
final var sample = new IntSample(100);
final var ret = sample.getValue().orElseGet(() -> {
System.out.println("action!");
return 9999;
});
System.out.println("ret = " + ret);
// Result
// ↓
//ret = 100
final var sample = new IntSample();
final var ret = sample.getValue().orElseGet(() -> {
System.out.println("action!");
return 9999;
});
System.out.println("ret = " + ret);
// Result
// ↓
//action!
//ret = 9999
int orElseThrow ()
final var sample = new IntSample(123);
final var opt = sample.getValue();
if (opt.isPresent()) {
System.out.println(opt.orElseThrow()); // 123
}
final var sample = new IntSample();
final var opt = sample.getValue();
try {
final var value = opt.orElseThrow();
} catch (NoSuchElementException e) {
System.out.println(e);
}
// Result
// ↓
//java.util.NoSuchElementException: No value present
<X extends Throwable> int orElseThrow (Supplier<? extends X> exceptionSupplier)
final var sample = new IntSample(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 IntSample();
try {
final var value = sample.getValue().orElseThrow(() -> {
throw new IllegalStateException("empty!");
});
} catch (IllegalStateException e) {
System.out.println(e);
}
// Result
// ↓
//java.lang.IllegalStateException: empty!
IntStream stream ()
final var sample = new IntSample(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 IntSample();
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 = OptionalInt.empty().toString();
System.out.println(ret1); // OptionalInt.empty
final var ret2 = OptionalInt.of(123).toString();
System.out.println(ret2); // OptionalInt[123]
final var ret3 = OptionalInt.of(-789).toString();
System.out.println(ret3); // OptionalInt[-789]
Related posts
- API Examples