Generate Infinite Stream of Integers in Java using Random.ints()


To generate Infinite Stream of Integers, you can use the Random class and its ints() method

Random.ints()

Here, we have used the ints() method to get the next integer.

The following is an example displaying how to generate Infinite Stream of Integers with Random.ints() in Java

Example

import java.util.stream.*;
import java.util.*;
public class Demo {
   public static void main(String[] args) {
      Random r = new Random();
      r.ints().forEach(System.out::println);
   }
}

Output

78799000099
8787879898
2872737888
.
.
.

Updated on: 30-Jul-2019

147 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements