DoubleStream generate() method in Java

DoubleStream generate(DoubleSupplier s) returns an infinite sequential unordered stream where each element is generated by the provided DoubleSupplier(a supplier of double-valued results). This is suitable for generating constant streams, streams of random elements, etc.

Syntax :

static DoubleStream generate(DoubleSupplier s)

Parameters :



  1. DoubleStream : a sequence of primitive double-valued elements.
  2. DoubleSupplier : a supplier of double-valued elements.
  3. s : DoubleSupplier for generated elements.

Return Value : a new infinite sequential unordered DoubleStream.

Example 1 : To generate stream of random doubles.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for DoubleStream.generate()
// to generate an infinite sequential
// unordered DoubleStream
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // using DoubleStream.generate() method
        // to generate 7 random Double values
        DoubleStream stream = DoubleStream.generate(()
           -> { return (double)(Math.random() * 10000); });
  
        // Displaying the randomly generated values
        stream.limit(7).forEach(System.out::println);
    }
}

chevron_right


Output:

2428.4470875214092
1339.9762523410686
302.44560518440267
9004.604086737152
3627.071411231598
2865.762726856176
4029.711939243109


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.