Java Streams - Stream generate(Supplier s) example








Stream generate(Supplier<T> s) returns an infinite sequential unordered stream where each element is generated by the provided Supplier.

Syntax

generate has the following syntax.

static <T> Stream<T> generate(Supplier<T> s)

Example

The following example shows how to use generate.

import java.util.Random;
import java.util.stream.Stream;
/*from  w  w  w.  ja va  2 s  .com*/
public class Main {
  public static void main(String[] args) {
    Stream.generate(new Random()::nextDouble)
          .limit(10)
          .forEach(System.out::println); 

  }
}

The code above generates the following result.