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

Java 8Object Oriented ProgrammingProgramming

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
.
.
.
raja
Published on 27-Mar-2019 08:46:09
Advertisements