Convert an array to a Stream in Java
This post will discuss various methods to convert array to stream in Java.
1. Convert Boxed Array to Stream
A Stream<T>
can be constructed from a boxed array using one of the following methods:
⮚ Using Arrays.stream()
1 2 3 4 |
// Generic method to convert array to stream in Java 8 and above public static <T> Stream<T> getStream(T[] arr) { return Arrays.stream(arr); } |
⮚ Using Stream.of()
1 2 3 4 5 |
// Generic method to convert array to stream in Java 8 and above public static <T> Stream<T> getStream(T[] arr) { // `Stream.of()` internally uses `Arrays.stream()` return Stream.of(arr); } |
⮚ Using List.stream()
1 2 3 4 |
// Generic method to convert array to stream in Java 8 and above public static <T> Stream<T> getStream(T[] arr) { return Arrays.asList(arr).stream(); } |
2. Convert Primitive Array to Stream
When we convert primitive array to stream, we’ll get primitive streams like IntStream
, DoubleStream
, and LongStream
. We can obtain these primitive streams by using one of the following methods:
⮚ Using Arrays.stream()
1 2 3 4 |
// Generic method to convert array to stream in Java 8 and above public static IntStream getStream(int[] arr) { return Arrays.stream(arr); } |
⮚ Using IntStream.of()
1 2 3 4 |
// Generic method to convert array to stream in Java 8 and above public static IntStream getStream(int[] arr) { return IntStream.of(arr); } |
Note – Arrays.asList(arr).stream()
and Stream.of(arr)
will return Stream<int[]>
not IntStream
.
3. Filter Array using Stream
We can also filter an array to match certain criteria using streams, 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.stream.Stream; class Main { public static void main(String[] args) { String[] arr = { "NYC", "Mexico", "Beijing", "New Delhi", "Tokyo" }; Stream<String> stream = Arrays.asList(arr).stream(); stream.filter(s -> s.startsWith("N")) .forEach(System.out::println); } } |
Output:
NYC
New Delhi
4. Convert stream back to array
We might often have a stream of elements and want to convert it back to the array. We can easily do so, 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.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { Stream<String> stream = Stream.of("NYC", "Mexico", "Beijing", "New Delhi", "Tokyo"); String[] str = stream.collect(Collectors.toList()).toArray(new String[0]); System.out.println(Arrays.toString(str)); } } |
Output:
[NYC, Mexico, Beijing, New Delhi, Tokyo]
This is equivalent to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.stream.Stream; class Main { public static void main(String[] args) { Stream<String> stream = Stream.of("NYC", "Mexico", "Beijing", "New Delhi", "Tokyo"); String[] str = stream.toArray(String[]::new); System.out.println(Arrays.toString(str)); } } |
Output:
[NYC, Mexico, Beijing, New Delhi, Tokyo]
That’s all about converting an array to 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 :)