Java : IntSupplier with Examples
IntSupplier (Java SE 21 & JDK 21) with Examples.
You will find code examples on most IntSupplier methods.
Summary
final var opt1 = IntStream.of(123, 456).findFirst();
final var opt2 = IntStream.empty().findFirst();
final var other = new IntSupplier() {
@Override
public int getAsInt() {
return 789;
}
};
System.out.println(opt1.orElseGet(other)); // 123
System.out.println(opt2.orElseGet(other)); // 789
// An example with a lambda expression.
final var opt = IntStream.empty().findFirst();
System.out.println(opt.orElseGet(() -> 789)); // 789
Methods
int getAsInt ()
final var opt1 = IntStream.of(123, 456).findFirst();
final var opt2 = IntStream.empty().findFirst();
final var other = new IntSupplier() {
@Override
public int getAsInt() {
return 789;
}
};
System.out.println(opt1.orElseGet(other)); // 123
System.out.println(opt2.orElseGet(other)); // 789
// An example with a lambda expression.
final var opt = IntStream.empty().findFirst();
System.out.println(opt.orElseGet(() -> 789)); // 789