Supplier
A functional interface whose get method takes no arguments and returns a value, used to produce something on demand.
In more detail
orElseGet runs its supplier only when the Optional is empty, and CompletableFuture.supplyAsync runs one on another thread. Passing a supplier instead of a value lets the callee decide whether and when to compute it.
Example
() -> "ready"Related terms
- functional interface — An interface with exactly one abstract method, such as Runnable, Supplier or Predicate, which a lambda or method reference can implement.
- orElse — The Optional methods that turn an Optional back into a plain value: orElse with a fallback, orElseGet with a fallback computed only if needed, and orElseThrow with an exception.
- supplyAsync — The CompletableFuture method that starts a task producing a value on another thread and returns a future for it.
Taught in
- Handling Nulls With Optional — Modern Java: Virtual Threads & High-Throughput Services
- Async With CompletableFuture — Modern Java: Virtual Threads & High-Throughput Services
- Interfaces: Default, Static And Private Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Lambdas, Functional Interfaces And Method References — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Stream Pipelines, Primitive Streams And Optional — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Reduction, Grouping And Partitioning — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Lambdas And Streams — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Modules And Concurrency — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep