Java compare ArrayList example shows how to compare ArrayList objects in Java. The example also shows how to compare two ArrayList objects to check if they are equal as well as getting missing or common elements from two ArrayList objects.
Java ArrayList is a part of the Java Collection framework.
How to compare ArrayList in Java?
There are many ways in which we can compare two ArrayList objects. For example, we may want to check if they are equal, or we may want to know what are the common elements or elements contained in one ArrayList but missing from another.
1. Compare two ArrayList objects to check if they are equal
Below given code compares two ArrayList and checks if they are equal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class JavaCompareArrayListsExample { public static void main(String[] args) { ArrayList<Integer> aList1 = new ArrayList<>(); aList1.add(1); aList1.add(2); aList1.add(3); ArrayList<Integer> aList2 = new ArrayList<>(); aList2.add(1); aList2.add(2); aList2.add(3); System.out.println("Equal? " + compareArrayList(aList1, aList2)); /* * Change first element */ aList1.set(0, 5); System.out.println("Equal? " + compareArrayList(aList1, aList2)); } private static boolean compareArrayList(List<Integer> a, List<Integer> b){ /* * If both reference points to same object */ if(a == b) return true; /* * If one of them is null * they are not equal */ if(a == null || b == null) return false; /* * If they differ in size, * they are not equal */ if(a.size() != b.size()) return false; /* * Compare them using equals method * which compares individual elements * of both ArrayList objects */ return a.equals(b); } } |
Output
1 2 |
Equal? true Equal? false |
If both ArrayList objects have the same elements but in a different order, the above code will return false. If you want to compare ArrayList elements regardless of the order, sort them before comparing them like given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class JavaCompareArrayListsExample { public static void main(String[] args) { ArrayList<Integer> aList1 = new ArrayList<>(); aList1.add(1); aList1.add(2); aList1.add(3); //same elements but in different order ArrayList<Integer> aList2 = new ArrayList<>(); aList2.add(1); aList2.add(3); aList2.add(2); System.out.println("Equal? " + compareArrayList(aList1, aList2)); } private static boolean compareArrayList(List<Integer> a, List<Integer> b){ if(a == b) return true; if(a == null || b == null) return false; if(a.size() != b.size()) return false; Collections.sort(a); Collections.sort(b); return a.equals(b); } } |
Output
1 |
Equal? true |
Note: if the ArrayList objects you want to compare contains objects of a custom class, then the class must override equals
and hashCode
methods as given below. Additionally, if you sort ArrayList using sort method, the custom class also needs to implement Comparable interface and define the compareTo
method.
The below given example has Emp class having empId as primary key, so if two objects have the same empId, they will be considered as equal. Change the equals and hashCode methods according to your class structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class JavaCompareArrayListsExample { public static void main(String[] args) { ArrayList<Emp> alist1 = new ArrayList<>(); alist1.add(new Emp(1, "a")); alist1.add(new Emp(2, "b")); alist1.add(new Emp(3, "c")); ArrayList<Emp> alist2 = new ArrayList<>(); alist2.add(new Emp(1, "a")); alist2.add(new Emp(2, "b")); alist2.add(new Emp(3, "c")); System.out.println("Equal? " + compareArrayList(alist1, alist2)); //replace element at index 2 alist1.set(2, new Emp(4, "d")); System.out.println("Equal? " + compareArrayList(alist1, alist2)); } private static boolean compareArrayList(List<Emp> a, List<Emp> b){ if(a == b) return true; if(a == null || b == null) return false; if(a.size() != b.size()) return false; Collections.sort(a); Collections.sort(b); return a.equals(b); } } class Emp implements Comparable<Emp>{ int empId; String empName; public Emp(int id, String name){ this.empId = id; this.empName = name; } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public int hashCode(){ return 31 * empId; } public boolean equals(Object obj){ if (this == obj) return true; if(obj == null) return false; if ( !(obj instanceof Emp) ) return false; if(this.empId != ((Emp)obj).getEmpId()) return false; return true; } public int compareTo(Emp e) { if(this.empId == e.empId) return 0; else if(this.empId > e.empId) return 1; else return -1; } } |
Output
1 2 |
Equal? true Equal? false |
2. Compare and get common elements of two ArrayList objects
If you want to compare two ArrayList objects and get all the common elements between the two, you can use retainAll
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class JavaCompareArrayListsExample { public static void main(String[] args) { ArrayList<Integer> aList1 = new ArrayList<>(); aList1.add(1); aList1.add(2); aList1.add(3); ArrayList<Integer> aList2 = new ArrayList<>(); aList2.add(1); aList2.add(2); aList2.add(4); compareArrayList(aList1, aList2); } private static void compareArrayList(List<Integer> a, List<Integer> b){ /* * Copy ArrayList so that original list * remains unchanged */ List<Integer> copy = new ArrayList<>(a); /* * This will retain all elements in list a which are * also present in b (i.e. common elements) and * remove rest of the elements. */ copy.retainAll(b); System.out.println( "ArrayList common elements: " + copy); } } |
Output
1 |
ArrayList common elements: [1, 2] |
Note: Make sure to copy ArrayList first so that the original list remains unchanged. Again, for the ArrayList having custom class objects, implement equals and hashCode methods in your custom class as given in the above Emp class example.
3. Compare and get missing elements from another ArrayList
Instead of retaining all the elements contained in other ArrayList from the first ArrayList object, we are going to remove all the elements contained in other ArrayList. That will give us all the elements which are not present in other ArrayList as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class JavaCompareArrayListsExample { public static void main(String[] args) { ArrayList<Integer> aList1 = new ArrayList<>(); aList1.add(1); aList1.add(2); aList1.add(3); ArrayList<Integer> aList2 = new ArrayList<>(); aList2.add(1); aList2.add(2); aList2.add(4); compareArrayList(aList1, aList2); } private static void compareArrayList(List<Integer> a, List<Integer> b){ /* * Copy ArrayList so that original list * remains unchanged */ List<Integer> copy = new ArrayList<>(a); /* * This will remove all elements in list a which are * not present in b (i.e. missing elements) */ copy.removeAll(b); System.out.println("Present in a but missing from b: " + copy); /* * Similarly, get elements present in b but * missing from from a */ copy = new ArrayList<>(b); copy.removeAll(a); System.out.println("Present in b but missing from a: " + copy); } } |
Output
1 2 |
Present in a but missing from b: [3] Present in b but missing from a: [4] |
Note: Make sure to copy ArrayList first so that the original list remains unchanged. Again, for the ArrayList having custom class objects, implement equals
and hashCode
methods in your custom class as given in the above Emp class example.
This example is a part of the Java ArrayList tutorial with examples.
Reference: ArrayList JavaDoc (Java 8)