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

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



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
.
.
.
Updated on: 2019-07-30T22:30:25+05:30

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements