Java : LongSupplier with Examples
LongSupplier (Java SE 21 & JDK 21) with Examples.
You will find code examples on most LongSupplier methods.
Summary
Represents a supplier of long-valued results. This is the long-producing primitive specialization of Supplier.
final var opt1 = LongStream.of(123, 456).findFirst();
final var opt2 = LongStream.empty().findFirst();
final var other = new LongSupplier() {
@Override
public long getAsLong() {
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 = LongStream.empty().findFirst();
System.out.println(opt.orElseGet(() -> 789)); // 789
Methods
long getAsLong ()
Gets a result.
final var opt1 = LongStream.of(123, 456).findFirst();
final var opt2 = LongStream.empty().findFirst();
final var other = new LongSupplier() {
@Override
public long getAsLong() {
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 = LongStream.empty().findFirst();
System.out.println(opt.orElseGet(() -> 789)); // 789
Related posts
- API Examples
- @FunctionalInterface
- BiConsumer
- BiFunction
- BiPredicate
- BooleanSupplier
- Consumer
- DoubleConsumer
- DoubleFunction
- DoublePredicate
- DoubleSupplier
- Function
- IntConsumer
- IntFunction
- IntPredicate
- IntSupplier
- IntToLongFunction
- LongConsumer
- LongFunction
- LongPredicate
- Predicate
- Runnable
- Supplier
- ToDoubleFunction
- ToIntFunction
- ToLongFunction