Streams from Functions

Description

We can have a function that can generate values on demand.

The following two static methods from Stream interface generates an infinite stream from a function.


<T> Stream<T> iterate(T  seed, UnaryOperator<T>  f)
<T> Stream<T> generate(Supplier<T> s)

iterate() method creates a sequential ordered stream.

generate() method creates a sequential unordered stream.

IntStream, LongStream, and DoubleStream contain iterate() and generate() methods that take parameters specific to their primitive types and generate values.

For example, the following two methods are defined in the IntStream interface:


IntStream iterate(int seed,  IntUnaryOperator f)
IntStream generate(IntSupplier s)




















Home »
  Java Streams »
    Tutorial »




Java Streams Tutorial