Java Streams - LongStream iterate(long seed, LongUnaryOperator f) example








LongStream iterate(long seed, LongUnaryOperator f) returns an infinite sequential ordered LongStream by iterative applying function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.

Syntax

iterate has the following syntax.

static LongStream iterate(long seed,   LongUnaryOperator f)

Example

The following example shows how to use iterate.

import java.util.stream.LongStream;

public class Main {
  public static void main(String[] args) {
    LongStream b = LongStream.iterate(1,(n)->{return n+1;});

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

The code above generates the following result.