How Can You Flatten a Nested Collection in Java?

How Can You Flatten a Nested Collection in Java? Complete Guide with Code Examples

Learn how to flatten a multi-dimensional collection in Java using the powerful Stream API. This guide includes practical code examples and best practices for collection manipulation.

Introduction

In Java, collections are widely used to store and manipulate groups of data. Sometimes, these collections are nested—meaning one collection contains other collections as elements. These nested structures are commonly encountered in scenarios like handling multi-dimensional arrays or hierarchical data structures. However, working with nested collections can be cumbersome, especially when you need to process or manipulate them as flat, single-level collections.

Flattening a nested collection involves converting a multi-level data structure into a single-level collection, making it easier to perform operations like filtering, mapping, or reducing. In Java, you can efficiently flatten a collection using the Stream API, which was introduced in Java 8. The flatMap() method is particularly useful for flattening nested collections.

This article provides a comprehensive guide on how to flatten nested collections in Java, with various code examples and explanations to help you understand the concept and apply it in your own projects.

Understanding Nested Collections

A nested collection is simply a collection that contains other collections as its elements. For example, a List of List objects or a Map with List values is considered a nested collection. These types of structures are often used to represent complex data, such as matrices or hierarchical relationships.

Here is an example of a nested List of List in Java:

        List> nestedList = Arrays.asList(
            Arrays.asList(1, 2, 3),
            Arrays.asList(4, 5),
            Arrays.asList(6, 7, 8)
        );
        

In this example, nestedList is a list of lists, where each inner list contains integers. To process the elements in a flat, single-level manner, we need to flatten this structure.

Flattening Nested Collections in Java with Stream API

The Stream API provides a powerful mechanism to transform data. When dealing with nested collections, the flatMap() method is key to flattening the structure. The flatMap() method takes a function that maps each element of the stream to another stream. It then “flattens” these resulting streams into a single stream.

Let’s see how flatMap() works in practice.

Example 1: Flattening a List of Lists

Let’s flatten a List> into a single List using flatMap():

        import java.util.*;
        import java.util.stream.*;

        public class FlattenExample {
            public static void main(String[] args) {
                List> nestedList = Arrays.asList(
                    Arrays.asList(1, 2, 3),
                    Arrays.asList(4, 5),
                    Arrays.asList(6, 7, 8)
                );
                
                // Flattening the nested list using flatMap
                List flatList = nestedList.stream()
                                                   .flatMap(Collection::stream)
                                                   .collect(Collectors.toList());

                System.out.println("Flattened List: " + flatList);
            }
        }
        

Output:

        Flattened List: [1, 2, 3, 4, 5, 6, 7, 8]
        

In this example, the flatMap() method is used to convert each inner list into a stream. The Collection::stream method reference is passed to flatMap(), which turns each inner List into a stream. The resulting stream of elements is then collected into a flat list using collect().

Example 2: Flattening a List of Maps

Let’s take a look at flattening a more complex nested structure—a List>—where each map contains key-value pairs. We will extract the values and flatten them into a single list of strings:

        import java.util.*;
        import java.util.stream.*;

        public class FlattenMapExample {
            public static void main(String[] args) {
                List> listOfMaps = Arrays.asList(
                    Map.of(1, "One", 2, "Two"),
                    Map.of(3, "Three", 4, "Four"),
                    Map.of(5, "Five")
                );
                
                // Flattening the List of Maps into a List of Strings (values)
                List flatList = listOfMaps.stream()
                                                  .flatMap(map -> map.values().stream())
                                                  .collect(Collectors.toList());

                System.out.println("Flattened List: " + flatList);
            }
        }
        

Output:

        Flattened List: [One, Two, Three, Four, Five]
        

In this case, we used flatMap() to extract the values from each map. The function passed to flatMap() is map -> map.values().stream(), which converts the collection of map values into a stream. The resulting stream is then flattened into a single list of strings.

Example 3: Flattening a List of Arrays

Flattening can also be performed on collections that contain arrays. For instance, let’s flatten a List of String[] arrays:

        import java.util.*;
        import java.util.stream.*;

        public class FlattenArrayExample {
            public static void main(String[] args) {
                List listOfArrays = Arrays.asList(
                    new String[]{"apple", "banana"},
                    new String[]{"cherry", "date"},
                    new String[]{"elderberry"}
                );
                
                // Flattening the List of Arrays into a List of Strings
                List flatList = listOfArrays.stream()
                                                    .flatMap(Arrays::stream)
                                                    .collect(Collectors.toList());

                System.out.println("Flattened List: " + flatList);
            }
        }
        

Output:

        Flattened List: [apple, banana, cherry, date, elderberry]
        

In this example, Arrays::stream is used to flatten the arrays into a stream of strings. The final result is a single list containing all the elements from the arrays.

Use Cases for Flattening Nested Collections

Flattening nested collections is useful in several scenarios, including:

  • Processing multi-dimensional data structures, such as matrices or tables.
  • Combining data from different sources into a single collection for further analysis.
  • Simplifying hierarchical or nested data representations for easier processing and manipulation.
  • Flattening complex objects for storage or transmission in a simplified format.

Performance Considerations

While flattening collections is a powerful tool, it is important to consider the performance implications, especially when dealing with large datasets. Flattening a deeply nested collection can lead to performance bottlenecks if not handled properly. Some best practices include:

  • Minimize unnecessary intermediate collections and transformations during flattening.
  • Use parallel streams for large datasets where parallel processing can provide performance benefits.
  • Profile the performance of your flattening operations using appropriate Java tools.

Conclusion

Flattening nested collections in Java is a powerful technique for transforming multi-dimensional or hierarchical data structures into flat, easy-to-manage collections. With the flatMap() method in the Stream API, Java makes it simple to flatten nested collections in a declarative and functional way. Whether you are working with lists, maps, or arrays, flattening allows you to streamline your data processing and simplify your code.

© 2025 Tech Interview Guide. All rights reserved.

Please follow and like us:

Leave a Comment