How to generate Infinite Stream of Double in Java using DoubleStream.generate()


The DoubleStream.generate() method returns an infinite sequential unordered stream where each element is generated by the provided DoubleSupplier.

The syntax is as follows −

static DoubleStream generate(DoubleSupplier s)

Here, s is the DoubleSupplier for generated elements. The DoubleSupplier represents a supplier of double-valued results.

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.generate() method −

Example

import java.util.stream.*;
import java.util.*;

public class Demo {
   public static void main(String[] args) {
      Random r = new Random();
      DoubleStream.generate(r::nextDouble).forEach(System.out::println);
   }
}

Here is the output displaying an infinite stream of Double −

Output

0.387687687514162978
0.545465653820283890
0.17845763929620120
.
.
.

Updated on: 30-Jul-2019

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements