How to Merge Two ArrayLists in Java

Learn to merge two ArrayList into a combined single ArrayList. Also, learn to join ArrayList without duplicates in a combined list instance.

1. Merging Two ArrayLists Retaining All Elements

In this approach, we are remaining all the elements from both lists, including duplicate elements.

1.1. Using List.addAll()

The addAll() method is the simplest way to append all of the elements from the given list to the end of another list. Using this method, we can combine multiple lists into a single list.

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

public class MergeArrayLists {
    public static void main(String[] args) {
        ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c"));

        ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("c", "d", "e"));

        listOne.addAll(listTwo);  //Merge both lists

        System.out.println(listOne);
        System.out.println(listTwo);
    }
}

Program output.

[a, b, c, c, d, e]
[d, e]

TIP : There are more ways to merge lists using libraries like guava or Apache commons lang, but they all use addAll() method only. So it’s better to use this method directly.

1.2. Using Stream.flatMap()

Java 8 streams provide us with one-line solutions to most of the problems and at the same time, the code looks clean. Stream’s flatMap() method can be used to get the elements of two or more lists in a single stream, and then collect stream elements to an ArrayList.

package com.howtodoinjava.core.collections.list;

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

public class MergeArrayLists {
    public static void main(String[] args) {
        ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c"));

        ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("c", "d", "e"));

        List<String> combinedList = Stream.of(listOne, listTwo)
                .flatMap(x -> x.stream())
                .collect(Collectors.toList());

        System.out.println(combinedList);
    }
}

Program output.

[a, b, c, c, d, e]

2. Merging Two ArrayLists excluding Duplicate Elements

In the first two examples, we combined the lists but in the final list, we had duplicate elements. This may not be the desired output in many cases.

To get a combined list minus duplicate elements, we have two approaches:

2.1. Using LinkedHashSet

Use LinkedHashSet. A set allows only unique elements. Push both lists in a set and the set will represent a list of all unique elements combined. We are using LinkedHashSet because it will preserve the element’s order as well.

ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c"));
ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("c", "d", "e"));

//Add items from Lists into Set
Set<String> set = new LinkedHashSet<>(listOne);
set.addAll(listTwo);

//Convert Set to ArrayList
ArrayList<String> combinedList = new ArrayList<>(set);

System.out.println(combinedList);
[a, b, c, d, e]

2.2. Using Iteration

This is a two-step process.

  • Remove all elements of the first list from the second list,
  • and then add the first list to the second list.

It will give users the combined list without duplicate elements.

ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c"));
ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("c", "d", "e"));

List<String> listTwoCopy = new ArrayList<>(listTwo);
listTwoCopy.removeAll(listOne);
listOne.addAll(listTwoCopy);

System.out.println(listOne);

Program output.

[a, b, c, d, e]

Happy Learning !!

Source Code on Github

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.