Java 8 Streams can holds different types of data. For example, Stream<T>
, Stream<T[]>
, Stream<List<T>>
– all these are valid streams in Java 8.
We have seen in the previous post that flatMap() method can be used for flattening Streams in Java. In this post, we will flatten Stream of Arrays or Lists using Stream.concat() method in Java.
1. Flatten a Stream of two or more arrays in Java 8
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 39 |
import java.util.Arrays; import java.util.stream.Stream; // Flatten a Stream of two or more arrays in Java using concat() method class StreamUtils { // 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, 9, 10 }; Integer[] merged = flatten(x, y, z) .toArray(Integer[]::new); System.out.println(Arrays.toString(merged)); } } |
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2. Flatten a Stream of two or more lists in Java 8
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 39 40 |
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; // Flatten a Stream of two or more lists in Java using concat() method class StreamUtils { // 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)); } } |
Output:
[1, 2, 3, 4, 5, 6, 7]
3. Flatten a Map containing List of items as values in Java 8
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 |
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; // Flatten a Map containing List of items as values in Java // using concat() method class StreamUtils { public static<T> Stream<T> flatten(Collection<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) { Map<String, List<Integer>> map = new HashMap<>(); map.put("A", Arrays.asList(1, 2, 3)); map.put("B", Arrays.asList(4, 5)); map.put("C", Arrays.asList(6, 7, 8)); List<Integer> list = flatten(map.values()) .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
Thanks for reading.
Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
Leave a Reply
why not just do:
public static Stream flatten(T[]... arrays){
return Arrays.stream(arrays).map(Arrays::stream).reduce(Stream::concat).get();
}
public static List combine(T[]... arrays){
return flatten(arrays).collect(Collectors.toList());
}