Comparing two ArrayLists could be a tricky task for beginner programmers. On this page, we are going to share some of the best ways to compare two ArrayLists.

Compare ArrayLists using Equal():

You can easily compare the two ArrayLists by simply using the equals() method. This method returns a boolean value(True or False). So, if the result is true, the ArrayLists are equal; otherwise, they are not equal.

Source Code:

import java.util.ArrayList;

public class ComparingArrayLists { //using equals()

    public static void main(String[] args) {
        //initializing two array lists
        ArrayList < String > list1 = new ArrayList < > ();
        ArrayList < String > list2 = new ArrayList < > ();

        //storing data in list 1
        list1.add("Java");
        list1.add("Python");
        list1.add("C++");

        //storing data in list 2
        list2.add("Java");
        list2.add("Python");
        list2.add("C++");

        //printing both lists
        System.out.println("List 1:" + list1);
        System.out.println("List 2:" + list2);

        //comparing both lists
        if (list1.equals(list2)) {
            System.out.println("Lists are equal");
        } else {
            System.out.println("Lists are not equal");
        }

        //adding another element to list 1
        list1.add("HTML");

        //comparing both lists again
        if (list1.equals(list2)) {
            System.out.println("Lists are equal");
        } else {
            System.out.println("Lists are not equal");
        }
    }
}

Output:

run:
List 1:[Java, Python, C++]
List 2:[Java, Python, C++]
Lists are equal
Lists are not equal
BUILD SUCCESSFUL (total time: 5 seconds)

Compare Arraylists using retainAll():

In case you want to compare two ArrayLists with respect to common elements, then you should be trying retainAll() method. It will return all common elements between two ArrayLists.

Source code:

import java.util.ArrayList;

public class ComparingArrayLists {//using retainAll()

    public static void main(String[] args) {
        //initializing two array lists
        ArrayList list1 = new ArrayList<>();
        ArrayList list2 = new ArrayList<>();

        //storing data in list 1
        list1.add("Mike");
        list1.add("Sara");
        list1.add("John");

        //storing data in list 2
        list2.add("Mike");
        list2.add("Sara");
        list2.add("Diaz");
        list2.add("Sam");

        //printing both lists
        System.out.println("List 1:" + list1);
        System.out.println("List 2:" + list2);

        //it will return common elements
        list2.retainAll(list1);
        //printing common elements
        System.out.println("Common elements are: " + list2);

    }
}

Output:

run:
List 1:[Mike, Sara, John]
List 2:[Mike, Sara, Diaz, Sam]
Common elements are: [Mike, Sara]
BUILD SUCCESSFUL (total time: 14 seconds)

Compare ArrayLists using contains():

An alternate way of retainAll() is to compare the elements of the ArrayLists using contains() method. The contains() method checks whether List 1 has the same element as List 2 at the same index.

Source code:

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

public class ComparingArrayLists {//using contains()
    
    public static void main(String[] args) {
        //initializing two array lists
        ArrayList list1 = new ArrayList<>(Arrays.asList("Apple", "Banana", "Peach", "Apricot"));
        ArrayList list2 = new ArrayList<>(Arrays.asList("Cabbage", "Carrots", "Cucumber", "Banana"));
        
        //printing both lists
        System.out.println("List 1:" + list1);
        System.out.println("List 2:" + list2);
        
        //Finding similar items in both lists
        int i=0;
        while(i<list1.size() && i<list2.size()){
            if(list2.contains(list1.get(i))){
                System.out.println(list1.get(i) + " is in both lists");
            }
            i++;
        }
        
    }
}

Output:

run:
List 1:[Apple, Banana, Peach, Apricot]
List 2:[Cabbage, Carrots, Cucumber, Banana]
Banana is in both lists
BUILD SUCCESSFUL (total time: 7 seconds)

References:

Happy Learning 🙂