Flatten a Stream of Lists in Java using forEach loop - GeeksforGeeks

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]

Approach:
Get the Lists in the form of 2D list.Create an empty list to collect the flattened elements.With the help of forEach loop, convert each elements of the list into stream and add it to the listNow convert this list into stream using stream() method.Now flatten the stream by converting it into list using collect() method.
Get the Lists in the form of 2D list.
Create an empty list to collect the flattened elements.
With the help of forEach loop, convert each elements of the list into stream and add it to the list
Now convert this list into stream using stream() method.
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 <T> Stream<T> flattenStream(List<List<T> > lists)    {          // Create an empty list to collect the stream        List<T> finalList = new ArrayList<>();          // Using forEach loop        // convert the list into stream        // and add the stream into list        for (List<T> 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<Integer> a = Arrays.asList(1, 2);        List<Integer> b = Arrays.asList(3, 4, 5, 6);        List<Integer> c = Arrays.asList(7, 8, 9);          List<List<Integer> > arr = new ArrayList<List<Integer> >();        arr.add(a);        arr.add(b);        arr.add(c);          // Flatten the Stream        List<Integer> flatList = new ArrayList<Integer>();        flatList = flattenStream(arr)                       .collect(Collectors.toList());          // Print the flattened list        System.out.println(flatList);    }}
[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 <T> Stream<T> flattenStream(List<List<T> > lists)    {          // Create an empty list to collect the stream        List<T> finalList = new ArrayList<>();          // Using forEach loop        // convert the list into stream        // and add the stream into list        for (List<T> 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<Character> a = Arrays.asList('G', 'e', 'e', 'k', 's');        List<Character> b = Arrays.asList('F', 'o', 'r');        List<Character> c = Arrays.asList('G', 'e', 'e', 'k', 's');          List<List<Character> > arr = new ArrayList<List<Character> >();        arr.add(a);        arr.add(b);        arr.add(c);          // Flatten the Stream        List<Character> flatList = new ArrayList<Character>();        flatList = flattenStream(arr)                       .collect(Collectors.toList());          // Print the flattened list        System.out.println(flatList);    }}
[G, e, e, k, s, F, o, r, G, e, e, k, s]

Java - util package
java-list
Java-List-Programs
java-stream
Java-Stream-programs
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
Stream In Java
How to iterate any Map in Java
Initializing a List in Java
Convert a String to Character Array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class