Java 8 Supplier

  • Stream's generate method returns an infinite sequential stream where supplier generates each element.
  • Following are the primitive Specializations for Supplier interface in java.util.function package.
    • IntSupplier  – Represents a supplier of int-valued results. Having one method getAsInt().
    • LongSupplier – Represents a supplier of long-valued results. Having one method getAsLong().
    • DoubleSupplier – Represents a supplier of double-valued results. Having one method getAsDouble().
    • BooleanSupplier  – Represents a supplier of boolean-valued results. Having one method getAsBoolean().

Syntax :

  • Let’s say you want to generate 5 random numbers between 0 to 10.
  • Stream's generate method returns an infinite sequential stream where supplier generates each element.

Example :

Example: Returns a supplier

Example: Factory

Leave a Comment