This post will discuss how to generate an infinite stream of integers in Java.

Until now, many of you have generated and operated only on the finite streams. This post provides an overview of infinite streams in Java, which is nothing but a sequential unordered stream that doesn’t end.

If a stream in Java is not properly bounded, then calling any terminal operation upon it may cause the program to enter into an endless state. To avoid it, ensure that the stream is properly bounded or truncate the stream to return only the first few elements in the encounter order.

 
There are many ways to generate an infinite sequential unordered stream in Java, which are discussed below:

1. Using IntStream.iterate() method

The most common approach to generate an infinite sequential stream is to call the iterate() method provided by the IntStream interface. It takes two parameters – an initial value, also known as the seed, which would be the first element of the stream, and a function to be applied to the previous element to produce a new element. In other words, the value of the next element in the stream will be determined by the result of applying that method to the previous element of the stream.

 
Now let’s generate an infinite stream starting from 0, whose each successive element is one more than the previous element.

Download  Run Code

 
The above code is equivalent to the following infinite for-loop:

 
We can call the limit() method to put a constraint on the size of the stream. We can also call the skip() method to discard the first few stream elements. This is demonstrated below:

Download  Run Code

2. Using Random.ints() method

We know that Java provides the Random class to generate a stream of pseudorandom numbers. To generate a stream of random int values, we can use the ints() method of the Random class, as shown below:

Download  Run Code

 
The random class has another overloaded version of the ints method, ints(start, end + 1), where we can also specify the range of returned random values.

Download  Run Code

 
We can also put a limit on the total number of random values returned by ints() by passing the total number of values to generate as an argument, as shown below:

Download  Run Code

3. Using IntStream.generate() method

The IntStream interface also has another method, generate() to generate an infinite stream. This method takes a Supplier<T> to generate new values, rather than a method that applies successively to the previous value.

There are many suppliers provided by Java that we can use, as shown below. The first two suppliers generate a stream of random elements, and the last two generates a stream of consecutive integers that are increasing.

ThreadLocalRandom.current().nextInt()

Download  Run Code

Random.nextInt()

Download  Run Code

⮚ Using AtomicInteger.incrementAndGet()

Download  Run Code

PrimitiveIterator.OfInt nextInt()

Download  Run Code

That’s all about generating an infinite Stream of Integers in Java.

 
Exercise: Create an infinite stream of Doubles in Java.