SoFunction
Updated on 2025-03-01

Java methods to merge multiple list objects

In Java, combining the list data of multiple objects into a new list can be achieved in many ways. Suppose there is aListList of each list contains objects of the same type and can be usedStreamAPIs or traditional loops to achieve this.

Sample Code User Class

Suppose we have oneUserkind:

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
         = name;
         = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
         = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
         = age;
    }

    @Override
    public String toString() {
        return "User{name='" + name + "', age=" + age + "}";
    }
}

Using the Stream API

import ;
import ;
import ;
import ;

public class MergeListsExample {

    public static void main(String[] args) {
        List<User> list1 = (
            new User("Alice", 30),
            new User("Bob", 25)
        );

        List<User> list2 = (
            new User("Charlie", 35),
            new User("Diana", 28)
        );

        List<User> list3 = (
            new User("Eve", 22),
            new User("Frank", 31)
        );

        List<List<User>> listOfLists = (list1, list2, list3);

        // Merge lists using Stream API        List<User> mergedList = ()
            .flatMap(List::stream)
            .collect(());

        // Output the merged list        (::println);
    }
}

Using traditional loops

import ;
import ;
import ;

public class MergeListsExample {

    public static void main(String[] args) {
        List<User> list1 = (
            new User("Alice", 30),
            new User("Bob", 25)
        );

        List<User> list2 = (
            new User("Charlie", 35),
            new User("Diana", 28)
        );

        List<User> list3 = (
            new User("Eve", 22),
            new User("Frank", 31)
        );

        List<List<User>> listOfLists = (list1, list2, list3);

        // Merge list using traditional loop        List<User> mergedList = new ArrayList<>();
        for (List<User> list : listOfLists) {
            (list);
        }

        // Output the merged list        (::println);
    }
}

explain

definitionUserkind:

  • UserClass containsnameandageand provide corresponding getter and setter methods.
  • RewrittentoStringMethods for easy output.

Create multipleList

  • Created threeList<User>Objects, each list contains severalUserObject.

useStreamAPI merge list:

  • ():WillList<List<User>>Convert toStream<List<User>>
  • flatMap(List::stream): Put eachList<User>Convert toStream<User>and flatten it into a singleStream<User>
  • collect(()):WillStream<User>Collect as oneList<User>

Merge the list using traditional loops:

  • Create an emptyArrayList<User>Used to store merged data.
  • TraversallistOfLists, add elements from each sublist tomergedListmiddle.

Output

Regardless of useStreamThe final output is the same for API or traditional loops:

User{name='Alice', age=30}
User{name='Bob', age=25}
User{name='Charlie', age=35}
User{name='Diana', age=28}
User{name='Eve', age=22}
User{name='Frank', age=31}

Summarize

In Java programming, merging multiple list objects can be implemented through Stream API or traditional loop method. When merging using Stream API, the flatMap method can be used to flatten the nested List, and then collected into a new list through the collect method. The traditional loop creates an empty ArrayList and adds elements by traversing each list. Both methods can effectively merge the list and keep the data types consistent.

This is the article about merging multiple object lists in Java. For more information about merging multiple object lists in Java, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!