In Java flatmap() is a function in the Stream class that can be used to flatten a Stream of Collections into a single Stream of Objects.
✏️Note: flatmap() is available only since Java 1.8
Example 1: Concat a 2-Dimensional Integer Array into a single 1-Dimentaional Array
import java.util.Arrays;
import java.util.stream.Stream;
public class JavaFlatMapExample {
public static void main(String[] args) {
Integer[][] array2D = new Integer[][]{{100, 200,300}, {400,500,600}, {700, 800}};
System.out.println(Arrays.deepToString(array2D));
Stream<Integer[]> streamOf2DArray = Arrays.stream(array2D);
Integer[] array1D = streamOf2DArray.flatMap(Stream::of).toArray(Integer[]::new);
System.out.println(Arrays.toString(array1D));
}
}
Output:
Input: [[100, 200, 300], [400, 500, 600], [700, 800]]
Result: [100, 200, 300, 400, 500, 600, 700, 800]
Example 2: Concat a 2-Dimensional String Array into a single 1-Dimentaional Array
public static void main(String[] args) {
String[][] array2D = new String[][]{{"one", "two"}, {"three","four","five"}, {"six", "seven","eight","nine","ten"}};
System.out.println(Arrays.deepToString(array2D));
Stream<String[]> streamOf2DArray = Arrays.stream(array2D);
String[] array1D = streamOf2DArray.flatMap(Stream::of).toArray(String[]::new);
System.out.println(Arrays.toString(array1D));
}
Output:
Input: [[one, two], [three, four, five], [six, seven, eight, nine, ten]]
Result: [one, two, three, four, five, six, seven, eight, nine, ten]

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Android Launch! The connection to adb is down, and a severe error has occured - Android
- AWS SNS CLI Command Publish Message Attributes - AWS
- How to customize SharePoint Modern list form using JSON formatting - SharePoint
- Python: Pandas Merge Indicator (Left, Right and Both) Example - Python
- Duplicate id @+id/textView1, already defined earlier in this layout Android Error - Android
- Java get day of the week as an int using DayOfWeek - Java
- List of Java Keywords - Java
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException - Android