Generate Infinite Stream of Integers in Java

In this post, we will discuss how to generate infinite stream of integers in Java.


 

Till now, many of you have generated and operated only on the finite streams. This post provides an overview of infinite stream 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 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. IntStream.iterate()

The most common approach to generate an infinite sequential Stream is to call the iterate() method provided by 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 next element in the Stream will be determined by the result of applying that function 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

Above code is equivalent to below 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 elements of the stream. This is demonstated below:

Download   Run Code

 

2. Random.ints()

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

Download   Run Code

 
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 number of random values returned by ints() by passing the number of values to generate as an argument, as shown below:

 

Download   Run Code

 

3. IntStream.generate()

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 function 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 generates a stream of random elements and the last two generates a stream of consecutive integers that are increasing.

 

1. ThreadLocalRandom.current().nextInt()

 

Download   Run Code

2. Random.nextInt()

 

Download   Run Code

3. Using AtomicInteger.incrementAndGet()

 

Download   Run Code

4. PrimitiveIterator.OfInt nextInt()

 

Download   Run Code

 

 
Exercise: Create an Infinite Stream of Doubles in Java.

 
Thanks for reading.

Please use our online compiler to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of