Generate Infinite Stream of Integers in Java - GeeksforGeeks

11 Dec, 2018
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.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 the IntStream.iterate() method, iterate the IntStream with i by incrementing the value with 1.Print the IntStream with the help of forEach() method.
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.
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() methodPrint the IntStream with the help of forEach() method.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
.
.
.

Get the next integer using ints() methodPrint the IntStream with the help of forEach() method.
Get the next integer using ints() method
Print the IntStream with the help of forEach() method.
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.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
.
.
.

Generate the next integer using IntStream.generate() and Random.nextInt()Print the IntStream with the help of forEach() method.
Generate the next integer using IntStream.generate() and Random.nextInt()
Print the IntStream with the help of forEach() method.
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
.
.
.

java-intstream
java-stream
Java-Stream-programs
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Singleton Class in Java
Set in Java
Multithreading in Java
Collections in Java