Flatten Streams using flatMap() method in Java
This post will discuss the flatMap() method in Java, which is used to flatten streams in Java.
We know that streams in Java is capable of holding different types of data. For instance,
Stream<T>– Stream where each element is an object.IntStream,DoubleStream, orLongStream– Stream where each element is a primitive data-type.Stream<int[]>– Stream where each element is a primitive array.Stream<T[]>– Stream where each element is an object array.Stream<List<T>>– Stream where each element is a list.Stream<Collection<T>>– Stream where each element is a Collection.
The flatMap() method can be used for flattening streams in Java, as shown below:
Stream<T[]> –> Stream<T>
Stream<List<T>> –> Stream<T>
Basically, a flatMap() can convert a stream of { {"A", "B", "C"}, {"D", "E", "F"} } to
{ "A", "B", "C", "D", "E", "F" }
The flatMap() takes a mapping method that will be applied to each element of the stream to facilitate the flattening of the stream. Let’s now explore various problems in Java that can be solved by flattening a stream of arrays or list into a single continuous stream:
1. Flatten a stream of two arrays of the same type
|
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 |
import java.util.Arrays; import java.util.stream.Stream; class Main { // Flatten a stream of two arrays of the same type in Java public static<T> Stream<T> flatten(T[] a, T[] b) { Stream<T> stream = Stream.of(a, b) .flatMap(Arrays::stream); return stream; } public static void main(String[] args) { String[] a = { "A", "B", "C" }; String[] b = { "D", "E", "F" }; String[] s = flatten(a, b) .toArray(String[]::new); System.out.println(Arrays.toString(s)); // [A, B, C, D, E, F] } } |
2. Flatten a stream of two or more arrays of the same type
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.stream.Stream; class Main { // Flatten a stream of multiple arrays of the same type in Java public static<T> Stream<T> flatten(T[] ... arrays) { Stream<T> stream = Stream.of(arrays).flatMap(Arrays::stream); return stream; } public static void main(String[] args) { String[] a = { "A", "B" }; String[] b = { "C", "D" }; String[] c = { "E", "F" }; String[] s = flatten(a, b, c).toArray(String[]::new); System.out.println(Arrays.toString(s)); // [A, B, C, D, E, F] } } |
3. Flatten a stream of two lists of the same type
|
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 |
import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { // Flatten a stream of two lists of the same type in Java public static<T> Stream<T> flatten(List<T> a, List<T> b) { Stream<T> stream = Stream.of(a, b) .flatMap(List::stream); // or use flatMap(x -> x.stream()) return stream; } public static void main(String[] args) { List<String> a = Arrays.asList("A", "B", "C"); List<String> b = Arrays.asList("D", "E", "F"); List<String> s = flatten(a, b) .collect(Collectors.toList()); System.out.println(s); // [A, B, C, D, E, F] } } |
4. Flatten a stream of two or more lists of the same type
|
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 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { // Flatten a stream of multiple lists of the same type in Java public static<T> Stream<T> flatten(List<T>... lists) { Stream<T> stream = Stream.of(lists) .flatMap(List::stream); // or use flatMap(x -> x.stream()) return stream; } public static void main(String[] args) { List<String> a = Arrays.asList("A", "B"); List<String> b = Arrays.asList("C", "D"); List<String> c = Arrays.asList("E", "F"); List<String> s = flatten(a, b, c) .collect(Collectors.toList()); System.out.println(s); // [A, B, C, D, E, F] } } |
5. Flatten a map containing a list of items as values
|
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 |
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { // Flatten a map containing a list of items as values in Java public static<T> Stream<T> flatten(Collection<List<T>> values) { Stream<T> stream = values.stream() .flatMap(x -> x.stream()); return stream; } public static void main(String[] args) { Map<Integer, List<String>> map = new HashMap<>(); map.put(1, Arrays.asList("A", "B", "C")); map.put(2, Arrays.asList("D", "E", "F")); List<String> s = flatten(map.values()) .collect(Collectors.toList()); System.out.println(s); // [A, B, C, D, E, F] } } |
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.*; class Main { public static void main(String[] args) { Map<Integer, List<String>> map = new HashMap<>(); map.put(1, Arrays.asList("A", "B", "C")); map.put(2, Arrays.asList("D", "E", "F")); List<String> result = new ArrayList<>(); for (List<String> x : map.values()) { for (String s : x) { result.add(s); } } System.out.println(result); // [A, B, C, D, E, F] } } |
Using flatMapToInt()
We know that calling Stream.of() method on a primitive integer array returns Stream<int[]>. We can use flatMapToInt() method to convert a stream of primitive arrays Stream<int[]> to IntStream before consuming, 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 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; class Main { // Flatten a stream of a primitive array in Java public static<T> IntStream flatten(int[] array) { IntStream stream = Stream.of(array) // returns `Stream<int[]>` .flatMapToInt(Arrays::stream); return stream; } public static void main(String[] args) { int[] array = { 1, 2, 3, 4, 5 }; List<Integer> list = flatten(array) .boxed() .collect(Collectors.toList()); System.out.println(list); // [1, 2, 3, 4, 5] } } |
Similarly, flatMapToDouble() can be used to flatten Stream<double[]> to DoubleStream and flatMapToLong() for flattening Stream<long[]> to LongStream.
That’s all about flattening a Java Stream using the flatMap() method.
Also See:
Flatten Stream of arrays or lists in Java using 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 :)