Generate Infinite Stream of Integers in Java

Given the task is to generate an infinite sequential unordered stream of integers in Java.
This can be done in following ways:
- Using IntStream.iterate():
- Using the IntStream.iterate() method, iterate the IntStream with i by incrementing the value with 1.
- Print the IntStream with the help of forEach() method.
importjava.util.stream.*;ÂÂpublicclassGFG {   Â// Main method   Âpublicstaticvoidmain(String[] args)   Â{       ÂIntStream           Â// Iterate the IntStream with i           Â// by incrementing the value with 1           Â.iterate(0, i -> i +1)           Â// Print the IntStream           Â// using forEach() method.           Â.forEach(System.out::println);   Â}}Output:
0 1 2 3 4 5 . . .
- Using Random.ints():
- Get the next integer using ints() method
- Print the IntStream with the help of forEach() method.
importjava.util.stream.*;importjava.util.*;ÂÂpublicclassGFG {   Â// Main method   Âpublicstaticvoidmain(String[] args)   Â{       Â// Create a Random object       ÂRandom random =newRandom();       Ârandom           Â// Get the next integer           Â// using ints() method           Â.ints()           Â// Print the IntStream           Â// using forEach() method.           Â.forEach(System.out::println);   Â}}Output:
-1214876682 911266110 1224044471 -1867062635 1893822159 1226183018 741533414 . . .
- Using IntStream.generate():
- Generate the next integer using IntStream.generate() and Random.nextInt()
- Print the IntStream with the help of forEach() method.
importjava.util.stream.*;importjava.util.*;ÂÂpublicclassGFG {   Â// Main method   Âpublicstaticvoidmain(String[] args)   Â{       Â// Create a Random object       ÂRandom random =newRandom();       ÂIntStream           Â// Generate the next integer           Â// using IntStream.generate()           Â// and Random.nextInt()           Â.generate(random::nextInt)           Â// Print the IntStream           Â// using forEach() method.           Â.forEach(System.out::println);   Â}}Output:
-798555363 -531857014 1861939261 273120213 -739170342 1295941815 870955405 -631166457 . . .



