java-8consumerfunctional-interfacesupplier

Supplier Functional Interface


As we know Consumer<T> functional interface is used in forEach() so similarly does Java has a use case for Supplier other than the custom ones?


Solution

  • java.util.Optional.orElseGet(Supplier<T> supplier) can be used to provide a default value for an Optional if it is empty.

    java.util.stream.Stream.generate(Supplier<T> s) can be used to generate a stream of values.