Generate Infinite Stream of Double in Java using DoubleStream.iterate()

Java 8Object Oriented ProgrammingProgramming

The DoubleStream.iterate() returns an infinite sequential ordered DoubleStream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed.

The syntax is as follows

static DoubleStream iterate(double seed, DoubleUnaryOperator f)

Here, seed is the initial element and f is a function to be applied to the previous element to produce a new element.

To use the DoubleStream class in Java, import the following package

import java.util.stream.DoubleStream;

The following is an example to generate infinite stream of Double in Java with DoubleStream.iterate()

Example

import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream.iterate(5, i -> i + 1).forEach(System.out::println);
   }
}

Here is the output displaying infinite stream of Double

Output

5.0
6.0
7.0
.
.
.
raja
Published on 27-Mar-2019 08:39:51
Advertisements