Skip to main content

How to Compare Two ArrayList in Java

How to compare two ArrayList in Java

In this article, we are going to learn how we can compare two ArrayList in Java whether they are the same or not in terms of elements store in it. Also, we are going to find distinct and shared elements of the two ArrayList.

Content of the Article


How to compare two ArrayList in Java to check they are equals?

The ArrayList class override the equals() method of the object class and can be used to compare two ArrayList in java in terms of whether the two lists contains the exact same elements (in the same order) or not. It might be possible that two ArrayList contains the same elements but different in order. To be sure, we can first sort the ArrayList (sorting can be ascending or descending) and then apply the equals() method for comparison.

Example 1: Compare two ArrayList in Java to check they are equals

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

public class ArrayListCompareExample {
        public static void main(String[] args) {
                ArrayList<String> languageList1 = new ArrayList<>(
                        Arrays.asList("Java", "C++", "Python", "PHP", "NodeJS"));
                System.out.println("ArrayList 1: " + languageList1);

                ArrayList<String> languageList2 = new ArrayList<>(
                        Arrays.asList("C++", "Python", "PHP", "NodeJS", "Java"));
                System.out.println("ArrayList 2: " + languageList2);

                if (compareTwoArrayList(languageList1, languageList2)) {
                        System.out.println("Yes! Both the ArrayLists are same");
                } else {
                        System.out.println("No! ArrayLists are different");
                }
        }

        private static boolean compareTwoArrayList(ArrayList<String> arrayList1,
             ArrayList<String> arrayList2) {
                // Sort ArrayList in ascending order
                Collections.sort(arrayList1);
                Collections.sort(arrayList2);

                // Compare two ArrayList in Java: equality check
                return arrayList1.equals(arrayList2);
        }
}
Output
ArrayList 1: [Java, C++, Python, PHP, NodeJS]
ArrayList 2: [C++, Python, PHP, NodeJS, Java]
Yes! Both the ArrayLists are same

Switch to Generic method to Compare two ArrayList in Java for equality check

private static <T extends Comparable<T>> boolean compareTwoArrayList(ArrayList<T> arrayList1,
        ArrayList<T> arrayList2) {
                // Sort ArrayList in ascending order
                Collections.sort(arrayList1);
                Collections.sort(arrayList2);

                // Compare two ArrayList in Java: equality check
                return arrayList1.equals(arrayList2);
}
Now we can use this method to compare two ArrayList in java which can be of any type but the type must implement the Comparable interface. If the elements stored in the arraylist are not the implementing the Comparable interface, we can sort the Arraylist with the help of the Comparators.

How to compare two ArrayList in Java to find shared elements?

Example 2: Compare two ArrayList in Java to find shared elements

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

public class ArrayListCompareExample {
        public static void main(String[] args) {
                ArrayList<String> languageList1 = new ArrayList<>(
                                Arrays.asList("Java", "C++", "PHP", "NodeJS"));
                System.out.println("ArrayList 1: " + languageList1);

                ArrayList<String> languageList2 = new ArrayList<>(
                                Arrays.asList("C++", "Python", "PHP", "JavaScript"));
                System.out.println("ArrayList 2: " + languageList2);

                ArrayList<String> sharedLanguageList = 
                                findSharedElements(languageList1, languageList2);
                System.out.println("After Compare two ArrayList:");
                System.out.println("Shared Elements: " + sharedLanguageList);
        }

        private static <T> ArrayList<T> findSharedElements(ArrayList<T> arrayList1,
             ArrayList<T> arrayList2) {
                ArrayList<T> arrayList3 = new ArrayList<>(arrayList1);
                arrayList3.retainAll(arrayList2);
                return arrayList3;
        }
}
Output
ArrayList 1: [Java, C++, PHP, NodeJS]
ArrayList 2: [C++, Python, PHP, JavaScript]
After Compare two ArrayList:
Shared Elements: [C++, PHP]

How to compare two ArrayList in Java to find distinct elements?

Example 3: Compare two ArrayList in Java to find distinct elements

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

public class ArrayListCompareExample {
        public static void main(String[] args) {
                ArrayList<String> languageList1 = new ArrayList<>(
                                Arrays.asList("Java", "C++", "PHP", "NodeJS"));
                System.out.println("ArrayList 1: " + languageList1);

                ArrayList<String> languageList2 = new ArrayList<>(
                                Arrays.asList("C++", "Python", "PHP", "JavaScript"));
                System.out.println("ArrayList 2: " + languageList2);

                ArrayList<String> languageList3 = 
                                findDistinctElements(languageList1, languageList2);
                System.out.println("Distinct Elements in List1 w.r.t. List2: " + languageList3);
  
                ArrayList<String> languageList4 = 
                                findDistinctElements(languageList2, languageList1);
                System.out.println("Distinct Elements in List2 w.r.t. List1: " + languageList4);
        }

        private static <T> ArrayList<T> findDistinctElements(ArrayList<T> arrayList1,
             ArrayList<T> arrayList2) {
                ArrayList<T> arrayList3 = new ArrayList<>(arrayList1);
                arrayList3.removeAll(arrayList2);
                return arrayList3;
        }
}
Output
ArrayList 1: [Java, C++, PHP, NodeJS]
ArrayList 2: [C++, Python, PHP, JavaScript]
Distinct Elements in List1 w.r.t. List2: [Java, NodeJS]
Distinct Elements in List2 w.r.t. List1: [Python, JavaScript]

Conclusion

In this article, we have learned how to compare two ArrayList in Java. Also, we learned how to find shared and distinct elements while comparing two ArrayList in Java.


Related Posts:

Comments