Java : IntSupplier with Examples

IntSupplier (Java SE 21 & JDK 21) with Examples.
You will find code examples on most IntSupplier methods.


Summary

Represents a supplier of int-valued results. This is the int-producing primitive specialization of Supplier.

Class diagram

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 ()

Gets a result.

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

Related posts

To top of page