This article provides an overview of how to generate an infinite stream of doubles in Java. An infinite stream is a sequential, unordered stream that doesn’t end.

Until now, many of we might have operated only on the finite streams. If a stream in Java is not properly bounded, then calling any terminal operation upon it may cause the program to enter into an infinite loop. We should always ensure that a stream is properly bounded. If not, then we should limit the stream only to return the first few terms from the beginning. Let’s now discuss how to generate an infinite stream of doubles in Java:

1. Using DoubleStream.iterate() method

A simple way is using the DoubleStream.iterate() method that takes a seed value and a method that generates the next value based on the previous one. It accepts two parameters: a seed which would be the first term in the stream, and a method to be applied to the previous term of the stream to produce the value of the next term in the stream. For example, the following code generates an infinite stream of doubles starting from 0.0 and increasing by 0.01:

Download Code

2. Using DoubleStream.generate() method

Another alternative is to use the DoubleStream.generate() method that returns an infinite stream. It accepts a Supplier<T>, rather than a method, to generate a value each time it is invoked. Several suppliers are available in Java that can be passed as a parameter to the DoubleStream.generate() method to generate an infinite stream of doubles. For example, the following code generates an infinite stream of random doubles between 0.0 (included) and 1.0 (excluded) using Math.random() method:

Download Code

 
We can also use the Random.nextDouble() or the ThreadLocalRandom.current().nextDouble() method as a supplier for the DoubleStream.generate(). Both methods returns the next pseudorandom, uniformly distributed double value from the range 0.0 (inclusive) to 1.0 (exclusive). For example:

Download Code

 
Finally, we can also use the PrimitiveIterator.OfDouble().nextDouble() method as a supplier for the DoubleStream.generate() method to generate new values for the stream. This method can return the next double value from the iterator. For example, we can create a custom PrimitiveIterator.OfDouble() that increments a double value by 0.01 each time, and then use it to generate an infinite stream of doubles as follows:

Download Code

3. Using Random.doubles() method

Finally, we can also use the Random.doubles() method to generate an infinite stream of pseudorandom double values between 0 (inclusive) and 1 (exclusive). For example, the following code generates an infinite stream of random doubles greater than or equal to 0.0 and less than 1.0:

Download Code

 
The Random.doubles() method is overloaded to accept a range for generating the pseudorandom double values. For example, the following code sets the range of returned random values to [1, 2):

Download Code

 
Note that all above methods will return an infinite stream, which means that they will never terminate unless we limit them with a terminal operation such as limit(), takeWhile(), or findFirst(). Otherwise, our program may enter into an infinite loop and consume a lot of memory. We should always ensure that our stream is properly bounded or use a short-circuiting operation to avoid this problem.

 
Exercise: Generate an infinite stream of integers in Java.

That’s all about generating an infinite stream of doubles in Java.