IntStream generate() method in Java

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

Syntax :

static IntStream generate(IntSupplier s)

Parameters :



  1. IntStream : a sequence of primitive int-valued elements.
  2. IntSupplier : a supplier of int-valued elements.
  3. s : IntSupplier for generated elements.

Return Value : a new infinite sequential unordered IntStream.

Example 1 : To generate stream of random integer.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

4247
6827
2676
9129
3360
8718
7925


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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.