Pages

Saturday, November 27, 2021

Nested lists with streams in Java 8 With flatMap() (list of lists)

1. Overview

In this article, You're going to learn how to work with nested list with java 8 streams.

Java 8 Stream API is added with flatMap() method which has the capability to map + flatten into objects or actual values.

let us explore more ways using flatMap() with complex objects to work with a list of lists or collection of collections.

In our example, we are using a List of objects but you can use List or Set implementations.

Nested lists with streams in Java 8


2. Flattening Complex Nested Lists with java 8 Streams


Creating a complex object.

class EmployeeFactory { private List<Employee> employees; public EmployeeFactory(List<Employee> employees) { this.employees = employees; } public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } } class Employee { private int id; private String name; private List<Address> addresses; public Employee(int id, String name, List<Address> addresses) { this.id = id; this.name = name; this.addresses = addresses; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Address> getAddresses() { return addresses; } public void setAddresses(List<Address> addresses) { this.addresses = addresses; } } class Address { private String line1; private String line2; private String town; private String city; private String pincode; public Address(String line1, String line2, String town, String city, String pincode) { this.line1 = line1; this.line2 = line2; this.town = town; this.city = city; this.pincode = pincode; } public String getLine1() { return line1; } public void setLine1(String line1) { this.line1 = line1; } public String getLine2() { return line2; } public void setLine2(String line2) { this.line2 = line2; } public String getTown() { return town; } public void setTown(String town) { this.town = town; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getPincode() { return pincode; } public void setPincode(String pincode) { this.pincode = pincode; } }

Getting the list of cities names which pincodes has char "3" or "5".

This can be done easily with the Java 8 Stream API in simple way.

package com.javaprogramto.java8.streams.nested;

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

public class NestedComplexLists {

    public static void main(String[] args) {

        List<Address> addresses = new ArrayList<>();
        addresses.add(new Address("Address 1 Line 1", "Address 1 Line 2", "Address 1  town", "Address 1 city", "12345"));
        addresses.add(new Address("Address 2 Line 1", "Address 2 Line 2", "Address 2 town", "Address 2 city", "22222"));
        addresses.add(new Address("Address 3 Line 1", "Address 3 Line 2", "Address 3 town", "Address 3 city", "33333"));

        Employee employee1 = new Employee(100, "Emp 1", addresses);

        List<Address> addresses2 = new ArrayList<>();

        addresses2.add(new Address("Address 4 Line 1", "Address 4 Line 2", "Address 4 town", "Address 4 city", "444444"));
        addresses2.add(new Address("Address 5 Line 1", "Address 5 Line 2", "Address 5 town", "Address 5 city", "55555"));
        addresses2.add(new Address("Address 6 Line 1", "Address 6 Line 2", "Address 6 town", "Address 6 city", "66666"));

        Employee employee12 = new Employee(101, "Emp 2", addresses2);
        List<Employee> empList = Arrays.asList(employee1, employee12);

        EmployeeFactory employeeFactory = new EmployeeFactory(empList);

        List<EmployeeFactory> employeeFactories = Arrays.asList(employeeFactory);
        List<String> citiesList = employeeFactories.stream()
                .flatMap(factory -> factory.getEmployees().stream())
                .flatMap(emp -> emp.getAddresses().stream())
                .filter(address -> (address.getPincode().indexOf("5") >= 0 || address.getPincode().indexOf("3") >= 0))
                .map(address -> address.getCity())
                .collect(Collectors.toList());

        System.out.println("Cities list : " + citiesList);

    }
}

Output:

Cities list : [Address 1 city, Address 3 city, Address 5 city]

3. Flattening Nested Collections with forEach() in Java (Merging)


package com.javaprogramto.java8.streams.nested;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FlattenCollectionExample {
    public static void main(String[] args) {

        List<List<String>> nestedCollection = new ArrayList<>();

        List<String> strList1 = Arrays.asList("List 1 Str 1", "List 1 Str 2", "List 1 Str 3");

        List<String> strList2 = Arrays.asList("List 2 Str 4", "List 2 Str 5");

        List<String> strList3 = Arrays.asList("List 3 Str 6", "List 3 Str 7", "List 3 Str 8", "List 3 Str 9");
        nestedCollection.add(strList1);
        nestedCollection.add(strList2);
        nestedCollection.add(strList3);

        List<String> flattenList = new ArrayList<>();

        nestedCollection.forEach(flattenList::addAll);

        System.out.println("Flatten List with forEach() : "+flattenList);

    }
}

Output:

Flatten List with forEach: [List 1 Str 1, List 1 Str 2, List 1 Str 3, List 2 Str 4, List 2 Str 5, List 3 Str 6, List 3 Str 7, List 3 Str 8, List 3 Str 9]

4. Flattening Nested Collections with flatMap() in Java (Merging)


The same above code can be rewritten with flatMap() method which flatten List into Strings.

List<String> flattenList = nestedCollection.stream().flatMap(list -> list.stream()).collect(Collectors.toList());

System.out.println("Flatten List with flatMap() : " + flattenList);

The output will be the same as the above section program.

5. Conclusion


In this article, You've seen how to flatten the Collection or List with Stream API.

Examples programs on forEach() and flatMap() method in java 8.

As usual, all the examples are over GitHub.

Ref


No comments:

Post a Comment

Please do not add any spam links in the comments section.