A multi-dimensional array (like a 2D array) organizes data in rows and columns. While this is great for representing grids, it makes searching, filtering, and sorting all the elements together complex. In Java, flatMap is an elegant functional operation that "flattens" multiple arrays nested inside a container into a single continuous stream of data.
In this guide, we will explain how 2D array flattening works using a simple dresser-drawers analogy and trace the execution path in Java.
Imagine you have a dresser with three drawers (representing a 2D array). Each drawer holds various numbers of toys marked with numbers. If you want to sort all these toys sequentially, you cannot easily do it while they are divided into separate drawers.
To sort them, you do this:
- Dumping (`flatMap`): You pull open all three drawers and pour all the toys onto a single long table (a flat 1D stream).
- Sorting (`sorted`): Now that all the toys are lying on the same flat surface, you line them up from smallest to largest.
The act of pouring the nested contents out onto a single flat table is exactly what flatMap does!
Walkthrough of the Main Method Scenario
Let's trace how the program executes step-by-step from the entry point of the main method:
The program sets up a 2D array where each row has a different length (known as a jagged array):
int[][] arr = {
{1, 2, 3},
{-2, 0, 5, 7},
{-3, 4, 6, 8, 9}
};
We have 3 nested arrays containing a total of 12 elements.
The program wraps the 2D array in a stream, flattens it, and sorts it in one fluent pipeline:
IntStream sorted = Stream.of(arr)
.flatMapToInt(Arrays::stream)
.sorted();
Stream.of(arr)creates a stream of typeStream<int[]>containing 3 array references (one for each row).flatMapToInt(Arrays::stream)processes each array row, converts it into a primitiveIntStream, and merges them. The output is a single flat stream:[1, 2, 3, -2, 0, 5, 7, -3, 4, 6, 8, 9]..sorted()sorts this single stream in natural ordering:[-3, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9].
Finally, the sorted elements are printed to the console one by one:
sorted.forEach(eachElement -> System.out.println(eachElement));
This prints the numbers sequentially from -3 up to 9.
Java Implementation
Below is the complete Java code demonstrating 2D array flattening and sorting using Streams:
package io.practise.accolite;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class FlatteningTwoDimentionalArray {
public static void main(String[] args) {
int[][] arr = {
{1, 2, 3},
{-2, 0, 5, 7},
{-3, 4, 6, 8, 9}
};
IntStream sorted = Stream.of(arr)
.flatMapToInt(Arrays::stream)
.sorted();
sorted.forEach(eachElement -> System.out.println(eachElement));
}
}
Conclusion
Java's flatMapToInt() is a powerful method for processing multi-dimensional arrays. By using Arrays::stream inside the flatmap call, we cleanly transform nested groupings of primitive integers into a single, flat stream. This allows us to perform collection-wide operations like sorting, filtering, or reduction efficiently without nested loops.