Flatten a Stream of Arrays or Lists in Java
Stream can hold different types of data, such as, Stream<T>, Stream<T[]>, Stream<List<T>> – all these are valid streams in Java. In the previous post, we have seen that the flatMap() method can be used for flattening streams in Java. This post will flatten a stream of arrays or lists using the Stream.concat() method in Java.
Flattening streams is a common and useful task in Java, as it allows us to process collections of data in a uniform and functional way. There are several methods and techniques for flattening streams in Java, such as using the concat() method.
1. Flattening a Stream of Arrays in Java
To flatten a stream of arrays into a single stream of integers, we can use the Stream.concat() method in combination with the Arrays.stream() method. The Arrays.stream() method takes an array as an argument and returns a stream of its elements. The Stream.concat() method takes two streams as arguments and returns a new stream that concatenates the elements of both streams. Here is how we can use these methods to flatten a stream of arrays in Java:
|
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 33 34 35 36 37 38 |
import java.util.Arrays; import java.util.stream.Stream; class Main { // Flatten a stream of two arrays in Java public static<T> Stream<T> flatten(T[] a, T[] b) { return Stream.concat(Arrays.stream(a), Arrays.stream(b)); } // Flatten a stream of three arrays in Java public static<T> Stream<T> flatten(T[] a, T[] b, T[] c) { return Stream.concat(Stream.concat(Arrays.stream(a), Arrays.stream(b)), Arrays.stream(c)); } // Flatten a stream of n number of arrays in Java public static<T> Stream<T> flatten(T[] ...arrays) { Stream<T> stream = Stream.of(); for (T[] array: arrays) { stream = Stream.concat(stream, Arrays.stream(array)); } return stream; } public static void main(String[] args) { Integer[] x = { 1, 2, 3 }; Integer[] y = { 4, 5 }; Integer[] z = { 6, 7, 8 }; Integer[] merged = flatten(x, y, z).toArray(Integer[]::new); System.out.println(Arrays.toString(merged)); // [1, 2, 3, 4, 5, 6, 7, 8] } } |
2. Flattening a Stream of Lists in Java
To flatten a stream of lists into a single stream of strings, we can use the same approach as before: use the Stream.concat() method in combination with the List.stream() method. Here is how we can use these methods to flatten a stream of lists in Java:
|
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 33 34 35 36 37 38 |
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; class Main { // Flatten a stream of two lists in Java public static<T> Stream<T> flatten(List<T> x, List<T> y) { return Stream.concat(x.stream(), y.stream()); } // Flatten a stream of three lists in Java public static<T> Stream<T> flatten(List<T> x, List<T> y, List<T> z) { return Stream.concat(Stream.concat(x.stream(), y.stream()), z.stream()); } // Flatten a stream of n number of lists in Java public static<T> Stream<T> flatten(List<T> ...lists) { Stream<T> stream = Stream.of(); for (List<T> list: lists) { stream = Stream.concat(stream, list.stream()); } return stream; } public static void main(String[] args) { List<Integer> x = Arrays.asList(1, 2, 3); List<Integer> y = Arrays.asList(4, 5); List<Integer> z = Arrays.asList(6, 7); Integer[] merged = flatten(x, y, z) .toArray(Integer[]::new); System.out.println(Arrays.toString(merged)); // [1, 2, 3, 4, 5, 6, 7] } } |
The advantage of using this method is that it is simple and concise. It uses only two built-in methods that are easy to understand and use. It also works for any type of array, not just integers.
That’s all about flattening the Stream of arrays or lists in Java using the Stream.concat() method.
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 :)