LongStream iterate(long seed, LongUnaryOperator f) example

Description

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





















Home »
  Java Streams »
    Reference »




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