Java Streams - IntStream iterate(int seed, IntUnaryOperator f) example








IntStream iterate(int seed, IntUnaryOperator f) returns an infinite sequential ordered IntStream produced by iterative applying f to seed.

It produces a Stream consisting of seed, f(seed), f(f(seed)), etc.

Syntax

iterate has the following syntax.

static IntStream iterate(int seed,   IntUnaryOperator f)

Example

The following example shows how to use iterate.

import java.util.stream.IntStream;


public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.iterate(1,n->n+1);
    i.limit(10).forEach(System.out::println);
  }
}

The code above generates the following result.