Thursday, September 4, 2025
HomeLanguagesJavaGenerate Infinite Stream of Double in Java

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():
    1. Using the DoubleStream.iterate() method, iterate the DoubleStream with i by incrementing the value with 1.
    2. Print the DoubleStream with the help of forEach() method.




    import java.util.stream.*;
      
    public class GFG {
      
        // Main method
        public static void main(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():
    1. Get the next double using doubles() method
    2. Print the DoubleStream with the help of forEach() method.




    import java.util.stream.*;
    import java.util.*;
      
    public class GFG {
      
        // Main method
        public static void main(String[] args)
        {
      
            // Create a Random object
            Random random = new Random();
      
            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:
    1. Generate the next double using DoubleStream.generate() and Random.nextDouble()
    2. Print the DoubleStream with the help of forEach() method.




    import java.util.stream.*;
    import java.util.*;
      
    public class GFG {
      
        // Main method
        public static void main(String[] args)
        {
      
            // Create a Random object
            Random random = new Random();
      
            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
    .
    .
    .
    
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS