Related Articles
Flatten a Stream of Lists in Java using forEach loop
• Last Updated : 11 Dec, 2018

Given a Stream of Lists in Java, the task is to Flatten the Stream using forEach() method.

Examples:

```Input: lists = [ [1, 2], [3, 4, 5, 6], [8, 9] ]
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Input: lists = [ ['G', 'e', 'e', 'k', 's'], ['F', 'o', 'r'] ]
Output: [G, e, e, k, s, F, o, r]
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Get the Lists in the form of 2D list.
2. Create an empty list to collect the flattened elements.
3. With the help of forEach loop, convert each elements of the list into stream and add it to the list
4. Now convert this list into stream using stream() method.
5. Now flatten the stream by converting it into list using collect() method.

Below is the implementation of the above approach:

Example 1: Using lists of integer.

 `// Java program to flatten a stream of lists ` `// using forEach() method ` ` `  `import` `java.util.ArrayList; ` `import` `java.util.Arrays; ` `import` `java.util.List; ` `import` `java.util.stream.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to flatten a Stream of Lists ` `    ``public` `static` ` Stream flattenStream(List > lists) ` `    ``{ ` ` `  `        ``// Create an empty list to collect the stream ` `        ``List finalList = ``new` `ArrayList<>(); ` ` `  `        ``// Using forEach loop ` `        ``// convert the list into stream ` `        ``// and add the stream into list ` `        ``for` `(List list : lists) { ` `            ``list.stream() ` `                ``.forEach(finalList::add); ` `        ``} ` ` `  `        ``// Convert the list into Stream and return it ` `        ``return` `finalList.stream(); ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// Get the lists to be flattened. ` `        ``List a = Arrays.asList(``1``, ``2``); ` `        ``List b = Arrays.asList(``3``, ``4``, ``5``, ``6``); ` `        ``List c = Arrays.asList(``7``, ``8``, ``9``); ` ` `  `        ``List > arr = ``new` `ArrayList >(); ` `        ``arr.add(a); ` `        ``arr.add(b); ` `        ``arr.add(c); ` ` `  `        ``// Flatten the Stream ` `        ``List flatList = ``new` `ArrayList(); ` `        ``flatList = flattenStream(arr) ` `                       ``.collect(Collectors.toList()); ` ` `  `        ``// Print the flattened list ` `        ``System.out.println(flatList); ` `    ``} ` `} `

Output:

```[1, 2, 3, 4, 5, 6, 7, 8, 9]
```

Example 2: Using lists of Characters.

 `// Java program to flatten a stream of lists ` `// using forEach() method ` ` `  `import` `java.util.ArrayList; ` `import` `java.util.Arrays; ` `import` `java.util.List; ` `import` `java.util.stream.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to flatten a Stream of Lists ` `    ``public` `static` ` Stream flattenStream(List > lists) ` `    ``{ ` ` `  `        ``// Create an empty list to collect the stream ` `        ``List finalList = ``new` `ArrayList<>(); ` ` `  `        ``// Using forEach loop ` `        ``// convert the list into stream ` `        ``// and add the stream into list ` `        ``for` `(List list : lists) { ` `            ``list.stream() ` `                ``.forEach(finalList::add); ` `        ``} ` ` `  `        ``// Convert the list into Stream and return it ` `        ``return` `finalList.stream(); ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// Get the lists to be flattened. ` `        ``List a = Arrays.asList(``'G'``, ``'e'``, ``'e'``, ``'k'``, ``'s'``); ` `        ``List b = Arrays.asList(``'F'``, ``'o'``, ``'r'``); ` `        ``List c = Arrays.asList(``'G'``, ``'e'``, ``'e'``, ``'k'``, ``'s'``); ` ` `  `        ``List > arr = ``new` `ArrayList >(); ` `        ``arr.add(a); ` `        ``arr.add(b); ` `        ``arr.add(c); ` ` `  `        ``// Flatten the Stream ` `        ``List flatList = ``new` `ArrayList(); ` `        ``flatList = flattenStream(arr) ` `                       ``.collect(Collectors.toList()); ` ` `  `        ``// Print the flattened list ` `        ``System.out.println(flatList); ` `    ``} ` `} `

Output:

```[G, e, e, k, s, F, o, r, G, e, e, k, s]
```

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :