How to create a Stream in Java
The Stream is the most important addition in Java 8 and above. This post will discuss various ways to create a stream in Java.
There are many ways to create a stream in Java, which are discussed below.
1. Create a stream from Collection
The Java Collection framework provides two methods, stream() and parallelStream(), to create a sequential and parallel stream from any collection, respectively.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; // Program to create a stream in Java class Main { public static void main(String[] args) { // input collection List<String> collection = Arrays.asList("Java", "8", "Stream", "API"); // create a sequential stream from the collection Stream<String> stream = collection.stream(); System.out.println(Arrays.toString(stream.toArray(String[]::new))); // create a parallel stream from the collection Stream<String> parallel_stream = collection.parallelStream(); System.out.println(Arrays.toString(parallel_stream.toArray())); } } |
Output:
[Java, 8, Stream, API]
[Java, 8, Stream, API]
2. Create a stream from specified values
We can create a sequential stream from the specified values using the Stream.of() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; import java.util.stream.Stream; // Program to create a stream in Java class Main { public static void main(String[] args) { // create a sequential stream from the specified values Stream<Integer> stream = Stream.of(1, 4, 2, 5, 8); System.out.println(Arrays.toString(stream.toArray())); } } |
Output:
[1, 4, 2, 5, 8]
3. Create stream from an array
There are two methods to create a stream from an array:
1. Using Arrays.stream() method
We can use Arrays.stream() to create a sequential stream with the specified array as its source. stream() method is overloaded – the first version takes the whole array, and the other version takes a part of an array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.stream.Stream; // Program to create a stream in Java class Main { public static void main(String[] args) { String[] arr = { "Java", "8", "Stream", "API" }; // create a sequential stream from the specified array Stream<String> stream = Arrays.stream(arr); System.out.println(Arrays.toString(stream.toArray())); // create a sequential stream from the specified subarray [start, end) int start = 0; // inclusive int end = 2; // exclusive stream = Arrays.stream(arr, start, end); System.out.println(Arrays.toString(stream.toArray())); } } |
Output:
[Java, 8, Stream, API]
[Java, 8]
2. Using Stream.of() method
We can also pass an array to the Stream.of() method, which takes varargs.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.stream.Stream; // Program to create a stream in Java class Main { public static void main(String[] args) { String[] arr = { "Java", "8", "Stream", "API" }; // create a sequential stream from the array Stream<String> stream = Stream.of(arr); System.out.println(Arrays.toString(stream.toArray(String[]::new))); } } |
Output:
[Java, 8, Stream, API]
4. Create an empty stream
We can use Stream.empty() or Stream.of() method to create an empty stream in Java.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; import java.util.stream.Stream; class Main { public static void main(String[] args) { // create an empty stream Stream<String> emptyStream = Stream.empty(); System.out.println(Arrays.toString(emptyStream.toArray())); } } |
Output:
[]
5. Create a stream from Builder
We can use a mutable builder for creating an ordered Stream. A stream builder has a lifecycle, which starts in a building phase, during which we can add elements in the required order, and then transitions to a built phase when the build() method is called.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Arrays; import java.util.stream.Stream; class Main { public static void main(String[] args) { Stream.Builder<String> builder = Stream.builder(); builder.add("Java"); builder.add("8"); Stream<String> stream = builder.build(); System.out.println(Arrays.toString(stream.toArray())); // shorthand for above Stream<String> streamBuilder = Stream.<String>builder() .add("Stream") .add("API") .build(); System.out.println(Arrays.toString(streamBuilder.toArray())); } } |
Output:
[Java, 8]
[Stream, API]
6. Create an infinite stream using Stream.iterate() method
We can use the iterate() method to create an infinite stream that accepts two parameters – a seed which is the first term in the stream, and a function to be applied to the previous term of the stream to produce the value of the next term in the stream. We can limit the stream using the limit() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.stream.Stream; class Main { public static void main(String[] args) { Stream<Integer> IntegerStream = Stream.iterate(0, n -> n + 1) .limit(10); Stream<Double> DoubleStream = Stream.iterate(0.0, n -> n + 1.0) .limit(5); System.out.println(Arrays.toString(IntegerStream.toArray())); System.out.println(Arrays.toString(DoubleStream.toArray())); } } |
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0.0, 1.0, 2.0, 3.0, 4.0]
7. Create an infinite stream using Stream.generate() method
Another way of creating an infinite stream of any custom type elements is by passing a method of a Supplier interface to a generate() method on a stream. There are many suppliers provided by Java that we can use, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Arrays; import java.util.Random; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Stream; class Main { public static void main(String[] args) { // generate a stream of UUIDs Stream<UUID> UUIDStream = Stream.generate(UUID::randomUUID) .limit(2); System.out.println(Arrays.toString(UUIDStream.toArray())); // generates a stream of random doubles between 0 and 1 Stream<Double> randomDoubleStream = Stream.generate(Math::random) .limit(4); System.out.println(Arrays.toString(randomDoubleStream.toArray())); // generate a stream of random integers Stream<Integer> randomIntStream = Stream.generate(ThreadLocalRandom.current()::nextInt).limit(2); System.out.println(Arrays.toString(randomIntStream.toArray())); // generates a stream of random integers Random random = new Random(); randomIntStream = Stream.generate(random::nextInt) .limit(5); System.out.println(Arrays.toString(randomIntStream.toArray())); } } |
Output (will vary):
[476ecc82-de26-4af6-aac0-1ca6dceaa6a0, f381352c-07e5-443e-a183-f697609244e2]
[0.914389687213923, 0.580971605583907, 0.8540320907003832, 0.14422141325963556]
[-2028513234, -1113287020]
[313246806, -895656428, -1799486714, -1662161615, -2062125349]
8. Create a stream from a sequence that matches a pattern
We can create a stream from an input sequence that matches a pattern using the Pattern.splitAsStream() method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.regex.Pattern; import java.util.stream.Stream; class Main { public static void main(String[] args) { String input = "Techie Delight"; Stream<String> stream = Pattern.compile("\\s") .splitAsStream(input); System.out.println(Arrays.toString(stream.toArray())); } } |
Output:
[Techie, Delight]
Also see:
That’s all about creating a Stream in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)