Programming & Development / April 12, 2025

Advanced flatMap() Example in Java: Flattening Nested Collections

Java flatMap advanced example flatMap nested collections flatten list of lists Java streams processing nested collections Java flatMap person pets unique pet names Java functional programming Java Java stream operations flattening with flatMap

In Java, the flatMap() method is not only useful for simple transformations but also for processing complex, nested collections. This article dives into an advanced use case of flatMap(), where we flatten nested lists to extract data from a more intricate structure, such as a list of people, where each person owns multiple pets.

Advanced Example: Flattening Nested Collections with flatMap()

Imagine we have a list of Person objects, where each person has a list of Pet objects. We want to extract a list of all the unique names of the pets owned by these people. Using flatMap(), we can flatten these nested lists into a single stream, making it easy to process further.

Code Example:

java

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

class Person {
    private String name;
    private List<Pet> pets;

    public Person(String name) {
        this.name = name;
        this.pets = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public List<Pet> getPets() {
        return pets;
    }

    public void addPet(Pet pet) {
        pets.add(pet);
    }
}

class Pet {
    private String name;

    public Pet(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class FlatMapAdvancedExample {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        
        // Creating Person and Pet objects
        Person person1 = new Person("Alice");
        person1.addPet(new Pet("Max"));
        person1.addPet(new Pet("Bella"));

        Person person2 = new Person("Bob");
        person2.addPet(new Pet("Charlie"));
        person2.addPet(new Pet("Lucy"));

        people.add(person1);
        people.add(person2);

        // Extracting unique pet names using flatMap
        List<String> petNames = people.stream()
                .flatMap(person -> person.getPets().stream())  // Flattening the list of pets for each person
                .map(Pet::getName)  // Mapping to pet names
                .distinct()  // Removing duplicates
                .collect(Collectors.toList());  // Collecting into a list

        System.out.println(petNames);
    }
}

Output:

csharp

[Max, Bella, Charlie, Lucy]

Explanation:

  1. Person and Pet Classes:
  • We define two classes: Person and Pet. A Person object can have multiple Pet objects, representing a real-world scenario where individuals own pets.
  1. Creating Data:
  • We create a list of Person objects, each having a list of Pet objects.
  • The list of pets for each person is stored in the pets field, and we add some Pet objects to each Person.
  1. Flattening with flatMap():
  • The flatMap() method is used to flatten the list of pets for each person. Instead of having a stream of lists of pets, we get a single stream of pets.
  • person.getPets().stream() converts each person's list of pets into a stream of pet objects.
  1. Mapping to Pet Names:
  • After flattening, we use the map() operation to transform the stream of Pet objects into a stream of pet names by calling Pet::getName.
  1. Removing Duplicates:
  • The distinct() operation ensures that only unique pet names are included in the result.
  1. Collecting Results:
  • Finally, we collect the pet names into a list using the Collectors.toList() method.

When to Use flatMap() for Nested Collections

You should use flatMap() when:

  • You need to flatten a nested collection into a single stream.
  • You're processing data that has multiple levels of nesting, such as lists of lists, maps of lists, or other complex data structures.
  • You want to combine multiple elements from nested collections into a single collection or stream for further processing.

Conclusion

In this advanced example, we used flatMap() to flatten a list of pets owned by different people into a single stream of pet names, demonstrating the power of Java streams in handling complex data structures. flatMap() is an essential tool when dealing with nested collections, allowing for cleaner, more efficient data processing.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex