Saturday, October 11, 2025
HomeLanguagesJavaFlatten a Stream of Arrays in Java using forEach loop

Flatten a Stream of Arrays in Java using forEach loop

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

Examples:

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

Input: arr[][] = {{'G', 'e', 'e', 'k', 's'}, {'F', 'o', 'r'}}
Output: [G, e, e, k, s, F, o, r]

Approach:

  1. Get the Arrays in the form of 2D array.
  2. Create an empty list to collect the flattened elements.
  3. With the help of forEach loop, convert each elements of the array 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 array using toArray() method.

Below is the implementation of the above approach:

Example 1: Using arrays of integer.




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


Output:

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

Example 2: Using arrays of Characters.




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


Output:

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

Most Popular

Dominic
32351 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11882 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7102 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS