Flattening an array or a list using forEach() method in Java
We have already covered how to flatten a stream using flatMap() and concat() methods in Java. This post will flatten an array or a list using the Stream.forEach() method in Java 8 and above.
1. Flattening Arrays
We can flatten two or more arrays of the same type using the forEach() method in Java. The idea is to create an empty list to store the flattened elements, and use a forEach() loop to iterate over the arrays and convert each array into a stream using the Arrays.stream() method. Then, using the forEach() method again, add each element of the stream to the list. Finally, we can convert the list into an array using the toArray() method. Here is a complete example:
|
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 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; class Main { @SafeVarargs public static<T> Stream<T> flatten(T[] ... arrays) { List<T> list = new ArrayList<>(); Arrays.stream(arrays).forEach(array -> Arrays.stream(array).forEach(list::add)); return list.stream(); } public static void main(String[] args) { Character[] a = { 'A', 'B' }; Character[] b = { 'C', 'D', 'E' }; Character[] c = { 'F', 'G' }; Character[] chars = flatten(a, b, c).toArray(Character[]::new); System.out.println(Arrays.toString(chars)); // [A, B, C, D, E, F, G] } } |
2. Flattening Lists
To flatten two or more lists of the same type using the forEach() method in Java, we can create a new list that will store the flattened elements. Then use a nested forEach() loop to iterate over each list and add its elements to the new list. Here is an example of how to flatten multiple lists of characters using this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { @SafeVarargs public static<T> List<T> flatten(List<T>... lists) { List<T> list = new ArrayList<>(); Arrays.stream(lists).forEach(l -> l.forEach(list::add)); return list; } public static void main(String[] args) { List<Character> a = Arrays.asList('A', 'B'); List<Character> b = Arrays.asList('C', 'D', 'E'); List<Character> c = Arrays.asList('F', 'G'); List<Character> chars = flatten(a, b, c); System.out.println(chars); // [A, B, C, D, E, F, G] } } |
If we have a map containing a list of items as values, we can create an empty list to collect the flattened elements and use the forEach() method to iterate over the map values, which are lists of items. Then use a nested forEach() loop to iterate over each list and add its elements to the empty list. Here is an example of how to do this:
|
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.*; class Main { public static<T> List<T> flatten(Collection<List<T>> collection) { List<T> flatList = new ArrayList<>(); collection.forEach(list -> list.forEach(flatList::add)); return flatList; } public static void main(String[] args) { Map<Integer, List<Character>> map = new HashMap<>(); map.put(1, Arrays.asList('A')); map.put(2, Arrays.asList('B', 'C', 'D')); map.put(3, Arrays.asList('E', 'F')); List<Character> chars = flatten(map.values()); System.out.println(chars); // [A, B, C, D, E, F] } } |
That’s all about flattening a Java Stream using the forEach() 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 :)