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

This approach retains all the elements from both lists, including duplicate elements. The size of the merged list will be arithmetic sum of the sizes of both lists.

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.

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]
[c, 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 cleaner. 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.

Using Stream is recommended as we do not need to modify the original List instances, and we create a third List with elements from both Lists.

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]

In these examples, we combined the lists, but in the final list, we had duplicate elements. This may not be the desired output in many cases. Next, we will learn to merge the lists, excluding duplicates.

2. Merging Two ArrayLists excluding Duplicate Elements

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

2.1. Using LinkedHashSet

The Java Sets allow only unique elements. When we push both lists in a Set and the Set will represent a list of all unique elements combined. In our example, 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 removeAll/addAll

This is a two-step process, and we can

  • 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.

Our Blogs

REST API Tutorial