In Java, there can be instances where there arises a need to update the records from time to time as per the requirement. More specifically, when the update is required at short intervals. In such scenarios, the Stream “flatMap()” method in Java is assistive in returning a flattened stream in the form of a single list comprising all the records in a streamlined manner.
This article will elaborate on the working of the “flatMap()” method in Java.
What is Java Stream flatMap()?
The “flatMap()” method in Java returns a stream that comprises the contents of a mapped stream. It is created by applying the mapping function to each element of the original stream.
Note: The “flatMap()” method involves “flattening”. It is the methodology of transforming several lists of lists and merging/appending all those lists to create a single list comprising all the list elements.
Syntax
In this syntax:
- “E” refers to the type of element in the new stream.
- “Stream” indicates an interface.
- “Type” corresponds to the stream type and “map” is a function that needs to be applied to each element and the function gives the new stream.
Before proceeding to the examples, import the below-stated packages to access all the classes within the “java.util” package and work with streams, respectively:
import java.util.stream.Collectors;
Example 1: Applying the Stream “flatMap()” Method Upon the Integer Values
This example applies the Stream “flatMap()” method to map the stream and flatten the list elements:
public static void main(String[] args){
List<List<Integer>> values = new ArrayList<>();
values.add(Arrays.asList(1, 2));
values.add(Arrays.asList(3, 4));
System.out.println("Before FlatMap -> " + values);
List<Integer> flatMap = values.stream().flatMap(list -> list.stream())
.collect(Collectors.toList());
System.out.println("After FlatMap -> " + flatMap);
}}
In this code snippet:
- Firstly, create an ArrayList comprising the “Integer” type values.
- In the next step, associate the “asList()” method that converts the array into a list with the stated integer values.
- Now, apply the combined “stream()” and “flatMap()” methods to map and flatten the list as a single list.
- Also, apply the “collect()” method to collect the items in the flattened stream and retrieve the list.
Output
Here, it can be analyzed that the stream is mapped and flattened accordingly.
Example 2: Applying the Stream “flatMap()” Method Upon the String Values
This example applies the discussed method to return a flattened stream of the “String” values:
public static void main(String[] args){
List<List<String>> values = new ArrayList<>();
values.add(Arrays.asList("Harry", "David"));
values.add(Arrays.asList("John", "Sara"));
System.out.println("Before FlatMap -> " + values);
List<String> flatMap = values.stream().flatMap(list -> list.stream())
.collect(Collectors.toList());
System.out.println("After FlatMap -> " + flatMap);
}}
In the above code block, repeat the discussed steps in the previous example upon the “String” type list values instead and retrieve the mapped flattened list instead.
Note: If there is a need to “flatMap” both the “Integer” and “String” values, utilize the “Object” type instead in the list.
Output
In the generated outcome, it can be signified that the string values in the list are mapped successfully.
Conclusion
The “flatMap()” method in Java returns a stream that comprises the contents of a mapped stream that are flattened. It is achieved by implementing the mapping function to each element of the original stream. It is such that the values are accumulated in a single list as a result. This blog elaborated on the usage and working of the “flatMap()” method in Java.