Flatten a Stream of Map in Java using forEach loop

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

Examples:

Input: map = {1=[1, 2], 2=[3, 4, 5, 6], 3=[7, 8, 9]}
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Input: map = {1=[G, e, e, k, s], 2=[F, o, r], 3=[G, e, e, k, s]}
Output: [G, e, e, k, s, F, o, r]

Approach:

  1. Get the Map to be flattened.
  2. Create an empty list to collect the flattened elements.
  3. With the help of forEach loop, convert each elements of the Map values into stream and add it to the list
  4. This list is the required flattened map.

Below is the implementation of the above approach:

Example 1: Using lists of integer.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to flatten a stream of map
// using forEach() method
  
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    // Function to flatten a Stream of Map
    public static <T> List<T> flattenStream(Collection<List<T> > lists)
    {
  
        // Create an empty list to collect the stream
        List<T> finalList = new ArrayList<>();
  
        // Using forEach loop
        // convert each list into stream
        // and add the stream into list
        for (List<T> list : lists) {
            list.stream()
                .forEach(finalList::add);
        }
  
        // Return the final flattened list
        return finalList;
    }
  
    public static void main(String[] args)
    {
  
        // Get the map to be flattened.
        Map<Integer, List<Integer> > map = new HashMap<>();
        map.put(1, Arrays.asList(1, 2));
        map.put(2, Arrays.asList(3, 4, 5, 6));
        map.put(3, Arrays.asList(7, 8, 9));
  
        // Flatten the Stream
        List<Integer> flatList = flattenStream(map.values());
  
        // Print the flattened list
        System.out.println(flatList);
    }
}

chevron_right


Output:

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

Example 2: Using lists of Characters.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to flatten a stream of map
// using forEach() method
  
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    // Function to flatten a Stream of Map
    public static <T> List<T> flattenStream(Collection<List<T> > lists)
    {
  
        // Create an empty list to collect the stream
        List<T> finalList = new ArrayList<>();
  
        // Using forEach loop
        // convert each list into stream
        // and add the stream into list
        for (List<T> list : lists) {
            list.stream()
                .forEach(finalList::add);
        }
  
        // Return the final flattened list
        return finalList;
    }
  
    public static void main(String[] args)
    {
  
        // Get the map to be flattened.
        Map<Integer, List<Character> > map = new HashMap<>();
        map.put(1, Arrays.asList('G', 'e', 'e', 'k', 's'));
        map.put(2, Arrays.asList('F', 'o', 'r'));
        map.put(3, Arrays.asList('G', 'e', 'e', 'k', 's'));
  
        // Flatten the Stream
        List<Character> flatList = flattenStream(map.values());
  
        // Print the flattened list
        System.out.println(flatList);
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.