IntStream iterate(int seed, IntUnaryOperator f) example

Description

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;
/*ww  w.j  ava  2 s  .c o m*/

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.





















Home »
  Java Streams »
    Reference »




Collectors
DoubleStream
DoubleStream.Builder
IntStream
IntStream.Builder
LongStream
LongStream.Builder
Stream
Stream.Builder