Java Streams - LongStream generate(LongSupplier s) example








LongStream generate(LongSupplier s) returns an infinite sequential unordered stream where each element is generated by the provided LongSupplier.

Syntax

generate has the following syntax.

static LongStream generate(LongSupplier s)

Example

The following example shows how to use generate.

import java.util.stream.LongStream;

public class Main {
  public static void main(String[] args) {
    LongStream b = LongStream.generate(()->{return 2L;});

    b.limit(10).forEach(System.out::println);
  }
}

The code above generates the following result.