Flatten a Stream of Map in Java using forEach loop
Last Updated :
11 Dec, 2018
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 the Map to be flattened.
- Create an empty list to collect the flattened elements.
- With the help of forEach loop, convert each elements of the Map values into stream and add it to the list
- This list is the required flattened map.
Below is the implementation of the above approach:
Example 1: Using lists of integer.
Java
// 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);
}
}
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Example 2: Using lists of Characters.
Java
// 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);
}
}
Output:
[G, e, e, k, s, F, o, r, G, e, e, k, s]
Similar Reads
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 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:
3 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
Initialize a static Map using Stream in Java In this article, a static map is created and initialized in Java using Stream. Static Map in Java A static map is a map which is defined as static. It means that the map becomes a class member and can be easily used using class. Stream In Java Introduced in Java 8, the Stream API is used to process
2 min read
Stream forEachOrdered() method in Java with examples Stream forEachOrdered(Consumer action) performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order. Stream forEachOrdered(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-ef
2 min read
Initialize a static Map using Java 9 Map.of() In this article, a static map is created and initialised in Java using Java 9. Static Map in Java A static map is a map which is defined as static. It means that the map becomes a class member and can be easily used using class. Java 9 feature - Map.of() method In Java 9, Map.of() was introduced whi
2 min read
How to Implement MultiMap in Java Using TreeMap? Map is an interface in java. Present in java.util package. The map is the collection of keys and values and it stores the data as a Key-Value pair. It is implemented by HashMap, TreeMap, and LinkedHashMap. Each class has its own features. It doesn't keep the order of keys. What is TreeMap in java? T
5 min read
Program to convert a Set to Stream in Java using Generics Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
3 min read
Difference Between map() And flatMap() In Java Stream In Java, the Stream interface has a map() and flatmap() methods and both have intermediate stream operation and return another stream as method output. Both of the functions map() and flatMap are used for transformation and mapping operations. map() function produces one output for one input value,
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