Stream generate() method in Java with examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes Like Article Like Report Stream generate(Supplier<T> s) returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc. Syntax : static <T> Stream<T> generate(Supplier<T> s) Where, Stream is an interface and T is the type of stream elements. s is the Supplier of generated elements and the return value is a new infinite sequential unordered Stream. Example 1 : To generate stream of random integer. Java // Java code for Stream.generate() // to generate an infinite sequential // unordered stream import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // using Stream.generate() method // to generate 5 random Integer values Stream.generate(new Random()::nextInt) .limit(5).forEach(System.out::println); } } Output : 697197501 50139200 321540264 1042847655 -770409472 Example 2 : To generate stream of random Double. Java // Java code for Stream.generate() // to generate an infinite sequential // unordered stream import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // using Stream.generate() method // to generate 8 random Double values Stream.generate(new Random()::nextDouble) .limit(8).forEach(System.out::println); } } Output : 0.5390254520295368 0.8477297185718798 0.23703352435894398 0.09156832989674057 0.9671295321757734 0.9989670394813547 0.8909416330715489 0.08177639888829968 Comment More infoAdvertise with us Next Article Stream generate() method in Java with examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream Java-Stream interface +2 More Practice Tags : JavaMisc Similar Reads Stream forEach() method in Java with examples Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int 2 min read Stream forEachOrdered() method in Java with examples Stream forEachOrdered(Consumer action) performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order. Stream forEachOrdered(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-ef 2 min read Stream skip() method in Java with examples Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) 3 min read Stream peek() Method in Java with Examples In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea 2 min read Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read Like