Stream generate() method in Java with examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes 5 Likes 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 Create Quiz Comment S Sahil_Bansall Follow 5 Improve S Sahil_Bansall Follow 5 Improve Article Tags : Misc Java Java - util package Java-Functions java-stream Java-Stream interface +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like