Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaIntStream generate() method in Java

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.




// 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);
    }
}


Output :

4247
6827
2676
9129
3360
8718
7925
RELATED ARTICLES

Most Popular

Recent Comments