Generate Infinite Stream of Integers in Java Last Updated : 11 Dec, 2018 Comments Improve Suggest changes Like Article Like Report Given the task is to generate an infinite sequential unordered stream of integers in Java. This can be done in following ways: Using IntStream.iterate(): Using the IntStream.iterate() method, iterate the IntStream with i by incrementing the value with 1. Print the IntStream with the help of forEach() method. Java import java.util.stream.*; public class GFG { // Main method public static void main(String[] args) { IntStream // Iterate the IntStream with i // by incrementing the value with 1 .iterate(0, i -> i + 1) // Print the IntStream // using forEach() method. .forEach(System.out::println); } } Output: 0 1 2 3 4 5 . . . Using Random.ints(): Get the next integer using ints() method Print the IntStream with the help of forEach() method. Java import java.util.stream.*; import java.util.*; public class GFG { // Main method public static void main(String[] args) { // Create a Random object Random random = new Random(); random // Get the next integer // using ints() method .ints() // Print the IntStream // using forEach() method. .forEach(System.out::println); } } Output: -1214876682 911266110 1224044471 -1867062635 1893822159 1226183018 741533414 . . . Using IntStream.generate(): Generate the next integer using IntStream.generate() and Random.nextInt() Print the IntStream with the help of forEach() method. Java import java.util.stream.*; import java.util.*; public class GFG { // Main method public static void main(String[] args) { // Create a Random object Random random = new Random(); IntStream // Generate the next integer // using IntStream.generate() // and Random.nextInt() .generate(random::nextInt) // Print the IntStream // using forEach() method. .forEach(System.out::println); } } Output: -798555363 -531857014 1861939261 273120213 -739170342 1295941815 870955405 -631166457 . . . Comment More infoAdvertise with us Next Article Generate Infinite Stream of Double in Java R RishabhPrabhu Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Generate Infinite Stream of Double in Java Given the task is to generate an infinite sequential unordered stream of double in Java. This can be done in following ways: Using DoubleStream.iterate(): Using the DoubleStream.iterate() method, iterate the DoubleStream with i by incrementing the value with 1. Print the DoubleStream with the help o 2 min read Generate Infinite Stream of Double in Java Given the task is to generate an infinite sequential unordered stream of double in Java. This can be done in following ways: Using DoubleStream.iterate(): Using the DoubleStream.iterate() method, iterate the DoubleStream with i by incrementing the value with 1. Print the DoubleStream with the help o 2 min read IntStream generate() Method in Java In Java, the IntStream.generate() method is part of the java.util.stream.IntStream class. This method is used to generate an infinite sequential and unordered stream of int values. This method takes IntSupplier as an argument. Note: It is commonly used for generating random numbers, constant streams 3 min read IntStream generate() Method in Java In Java, the IntStream.generate() method is part of the java.util.stream.IntStream class. This method is used to generate an infinite sequential and unordered stream of int values. This method takes IntSupplier as an argument. Note: It is commonly used for generating random numbers, constant streams 3 min read Convert an Iterator to Stream in Java Given an Iterator, the task is to convert it into Stream in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Approach: Get the Iterator. Convert the iterator to Spliterator using Spliterators.split 1 min read Convert an Iterator to Stream in Java Given an Iterator, the task is to convert it into Stream in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Approach: Get the Iterator. Convert the iterator to Spliterator using Spliterators.split 1 min read Like