How to compare two ArrayLists in Java

Learn to compare two arraylists in Java with simple examples. We will first test if two arraylists are equal or not. If both lists are not equal, we will find the difference between lists.

The difference in list is equals to another third list which contains either additional elements or missing elements.

Also learn to find common elements between two arraylists.

1. Compare two arraylists for equality

Java program to test if two given lists are equal. To test equality –

  • Sort both lists.
  • Compare both lists using equals() method.

List.equals() method return true if both elements are of same size and both contains same set of elements in exactly same order.

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f"));
        
        ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
        
        Collections.sort(listOne);
        Collections.sort(listTwo);
        
        //Compare unequal lists example
        
        boolean isEqual = listOne.equals(listTwo);      //false
        System.out.println(isEqual);
        
        //Compare equals lists example
        
        ArrayList<String> listThree = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f"));
        
        isEqual = listOne.equals(listThree);      //true
        System.out.println(isEqual);
    }
}

Program output.

false
true

2. Compare two arraylists – find additional elements

If two arraylists are not equal and we want to find what are additional elements in first list in comparison to second list then we can use this method.

It uses removeAll() method which removes all elements of second list from first list. It leaves only additonal elements in first list.

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f"));
        
        ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
        
        //remove all elements of second list
        listOne.removeAll(listTwo);
        
        System.out.println(listOne);
    }
}

Program output.

[f]

3. Compare two arraylists – find missing elements

To get the missing elements in first list, which are present in second list, we can reverse the above example. Here we can remove all elements of first list from second list using removeAll() method.

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f"));
        
        ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
        
        //remove all elements from second list
        listTwo.removeAll(listOne);
        
        System.out.println(listTwo);
    }
}

Program output.

[e]

4. Compare two arraylists – find common elements

To find common elements in two arraylists, use List.retainAll() method. This method retains only the elements in this list that are contained in the specified arraylist passed as method argument.

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        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.retainAll(listTwo);
        
        System.out.println(listOne);
    }
}

Program output.

[a, b, c, d]

Above example will work good in Java 8 as well.

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Leave a Reply

7 Comments
Most Voted
Newest Oldest
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.