Generate Infinite Stream of Double in Java

Given the task is to generate an infinite sequential unordered stream of double in Java.
This can be done in following ways:
- Using DoubleStream.iterate():
- Using the DoubleStream.iterate() method, iterate the DoubleStream with i by incrementing the value with 1.
- Print the DoubleStream with the help of forEach() method.
importjava.util.stream.*;ÂÂpublicclassGFG {   Â// Main method   Âpublicstaticvoidmain(String[] args)   Â{       ÂDoubleStream           Â// Iterate the DoubleStream with i           Â// by incrementing the value with 1           Â.iterate(0, i -> i +1)           Â// Print the DoubleStream           Â// using forEach() method.           Â.forEach(System.out::println);   Â}}Output:
0.0 1.0 2.0 3.0 4.0 5.0 6.0 . . .
- Using Random.doubles():
- Get the next double using doubles() method
- Print the DoubleStream 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 double           Â// using doubles() method           Â.doubles()           Â// Print the DoubleStream           Â// using forEach() method.           Â.forEach(System.out::println);   Â}}Output:
0.3668625445505631 0.4385898887922953 0.23333911864591927 0.7134958163360963 0.6945667694181287 0.6898427735417596 0.9243923588584183 . . .
- Using DoubleStream.generate() method:
- Generate the next double using DoubleStream.generate() and Random.nextDouble()
- Print the DoubleStream with the help of forEach() method.
importjava.util.stream.*;importjava.util.*;ÂÂpublicclassGFG {   Â// Main method   Âpublicstaticvoidmain(String[] args)   Â{       Â// Create a Random object       ÂRandom random =newRandom();       ÂDoubleStream           Â// Generate the next double           Â// using DoubleStream.generate()           Â// and Random.nextDouble()           Â.generate(random::nextDouble)           Â// Print the DoubleStream           Â// using forEach() method.           Â.forEach(System.out::println);   Â}}Output:
0.025801080723973246 0.5115037630832635 0.030815898624858784 0.5441584143944648 0.6984267528746901 0.5821292304544626 . . .



