Flatten a Stream of Arrays in Java using forEach loop
Last Updated :
11 Dec, 2018
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:
- Get the Arrays in the form of 2D array.
- Create an empty list to collect the flattened elements.
- With the help of forEach loop, convert each elements of the array 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 array using toArray() method.
Below is the implementation of the above approach:
Example 1: Using arrays of integer.
Java
// 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
// 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]
Similar Reads
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: Get
3 min read
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: Get
3 min read
Flatten a Stream of Lists in Java using forEach loop 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 Li
3 min read
Flatten a Stream of Lists in Java using forEach loop 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 Li
3 min read
Difference between Stream.of() and Arrays.stream() method in Java Arrays.stream() The stream(T[] array) method of Arrays class in Java, is used to get a Sequential Stream from the array passed as the parameter with its elements. It returns a sequential Stream with the elements of the array, passed as parameter, as its source. Example:Â Java // Java program to demo
5 min read
Stream forEach() method in Java with examples Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int
2 min read