How to merge two ArrayLists in Java

Learn how to merge two arraylists into a combined single arraylist in Java. Also learn to join arraylists without duplicates in the combined list.

1. Merge arraylists – List.addAll() method

addAll() method simplest way to append all of the elements in the given collection 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 ArrayListExample 
{
    public static void main(String[] args) throws Exception 
    {
        ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f"));
        
        ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
        
        listOne.addAll(listTwo);	//Merge both lists
        
        System.out.println(listOne);
    }
}

Program output.

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

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.

2. Merge arraylists – Java 8 Stream.flatMap()

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

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

public class ArrayListExample 
{
    public static void main(String[] args) throws Exception 
    {
        ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
        
        ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "f", "g"));
        
        List<String> combinedList = Stream.of(listOne, listTwo)
                                        .flatMap(x -> x.stream())
                                        .collect(Collectors.toList());
        System.out.println(combinedList);
    }
}

Program output.

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

3. Merge two arraylists without duplicates

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

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

  1. Use LinkedHashSet. A set allow only unique elements. Push both lists in a set and set will represent a list of all unique elements combined.

    We are using LinkedHashSet because it will preserve the elements order as well.

  2. This is two step process. Remove all elements of first list from second list, and then add first list to second list. It will give use the combined list without duplicate elements.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ArrayListExample 
{
    public static void main(String[] args) throws Exception 
    {
        ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
        
        ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "f", "g"));
        
        //1
        
        Set<String> set = new LinkedHashSet<>(listOne);
        set.addAll(listTwo);
        List<String> combinedList = new ArrayList<>(set);
        
        System.out.println(combinedList);
        
        //2
        
        List<String> listTwoCopy = new ArrayList<>(listTwo);
        listTwoCopy.removeAll(listOne);
        listOne.addAll(listTwoCopy);
        
        System.out.println(listOne);
    }
}

Program output.

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

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.