Skip to content
Related Articles

Related Articles

Improve Article

IntStream generate() method in Java

  • Last Updated : 21 Mar, 2018

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.

// Java code for IntStream.generate()
// to generate an infinite sequential
// unordered IntStream
import java.util.*;
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

Output :


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :