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 :
- IntStream : a sequence of primitive int-valued elements.
- IntSupplier : a supplier of int-valued elements.
- 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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- IntStream forEach() method in Java
- IntStream forEachOrdered() method in Java
- IntStream.Builder add() method in Java
- IntStream.Builder accept() method in Java
- IntStream codePoints() method in Java with Examples
- java.util.stream.IntStream/LongStream | Search an element
- IntStream iterator() in Java
- IntStream rangeClosed() in Java
- IntStream map(IntUnaryOperator mapper) in Java
- IntStream findFirst() in Java
- IntStream parallel() in Java
- IntStream empty() in Java with examples
- IntStream distinct() in Java with examples
- IntStream peek() in Java with examples
- IntStream average() in Java with Examples
- IntStream max() in Java with examples
- IntStream min() in Java with Examples
- IntStream count() in Java with examples
- IntStream skip() in Java
- IntStream filter() in Java with examples
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.

