Comparing two ArrayList In Java Last Updated : 07 Oct, 2022 Comments Improve Suggest changes Like Article Like Report Java provides a method for comparing two Array List. The ArrayList.equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal. Example: Input : ArrayList1 = [1, 2, 3, 4], ArrayList2 = [1, 2, 3, 4] Output: Array List are equal Input : ArrayList1 = [1, 2, 3, 4], ArrayList2 = [4, 2, 3, 1] Output: Array List are not equal Syntax: boolean equals(Object o) Parameters: This function has a single parameter which is an object to be compared for equality. Returns: This method returns True if Array lists are equal. Implementation: Java // Comparing two ArrayList In Java import java.util.ArrayList; public class GFG { public static void main(String[] args) { // create two Array List ArrayList<String> ArrayList1 = new ArrayList<String>(); ArrayList<String> ArrayList2 = new ArrayList<String>(); // insert items in ArrayList 1 ArrayList1.add("item 1"); ArrayList1.add("item 2"); ArrayList1.add("item 3"); ArrayList1.add("item 4"); // insert items in ArrayList 2 ArrayList2.add("item 1"); ArrayList2.add("item 2"); ArrayList2.add("item 3"); ArrayList2.add("item 4"); // Display both ArrayList System.out.println(" ArrayList1 = " + ArrayList2); System.out.println(" ArrayList1 = " + ArrayList1); // compare ArrayList1 with ArrayList2 if (ArrayList1.equals(ArrayList2) == true) { System.out.println(" Array List are equal"); } else // else block execute when // ArrayList are not equal { System.out.println(" Array List are not equal"); } // insert one more item in ArrayList 1 System.out.println( "\n Lets insert one more item in Array List 1"); ArrayList1.add("item 5"); // display both ArrayList System.out.println(" ArrayList1 = " + ArrayList1); System.out.println(" ArrayList = " + ArrayList2); // again compare ArrayList 1 with ArrayList 2 if (ArrayList1.equals(ArrayList2) == true) { System.out.println(" Array List are equal"); } else { System.out.println(" Array List are not equal"); } } } Output ArrayList1 = [item 1, item 2, item 3, item 4] ArrayList1 = [item 1, item 2, item 3, item 4] Array List are equal Lets insert one more item in Array List 1 ArrayList1 = [item 1, item 2, item 3, item 4, item 5] ArrayList = [item 1, item 2, item 3, item 4] Array List are not equalTime Complexity: O(N), where N is the length of the Array list. Comment More infoAdvertise with us Next Article Comparing two ArrayList In Java mukulsomukesh Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-ArrayList +1 More Practice Tags : Java Similar Reads Get Enumeration Over Java ArrayList ArrayList class is a re-sizeable array that is present in java.util package. Unlike the built-in arrays, ArrayList can change their size dynamically where elements can be added and removed from an ArrayList. In java version 1, enum was not present. With advancement to version, 1.5 enum was introduce 3 min read How to Compare two Collections in Java? Java Collection provides an architecture to store and manipulate the group of objects. Here we will see how to compare Elements in a Collection in Java. Steps: Take both inputs with help of asList() function.Sort them using Collections.sort() method.Compare them using equals() function.Print output. 2 min read Sort ArrayList in Descending Order Using Comparator in Java A comparator is an interface that is used to rearrange the ArrayList in a sorted manner. A comparator is used to sort an ArrayList of User-defined objects. In java, a Comparator is provided in java.util package. Using Comparator sort ArrayList on the basis of multiple variables, or simply implement 3 min read How Objects Can an ArrayList Hold in Java? ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. In order to understan 3 min read Traverse Through ArrayList in Forward Direction in Java ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java. The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in a proper organized sequence). ArrayList can be 3 min read Finding Maximum Element of Java ArrayList For finding the maximum element in the ArrayList, complete traversal of the ArrayList is required. There is an inbuilt function in the ArrayList class to find the maximum element in the ArrayList, i.e. Time Complexity is O(N), where N is the size of ArrayList, Let's discuss both the methods. Example 2 min read How to Sort ArrayList using Comparator? Comparator is an interface that is used for rearranging the Arraylist in a sorted manner. Comparator is used to sort an ArrayList of User-defined objects. In java, Comparator is provided in java.util package. Using Comparator we can sort ArrayList on the basis of multiple variables. We can simply im 3 min read Java Program to Compare two Boolean Arrays Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways: By using Java built-in method that is .equals() method.By using the Naive approach. Examples: Input : A = [true , true , false] A1 = [true, true, false] Output: Both the ar 3 min read equals() and deepEquals() Method to Compare two Arrays in Java Arrays. equals() method does not compare recursively if an array contains another array on other hand Arrays. deepEquals() method compare recursively if an array contains another array. Arrays.equals(Object[], Object[]) Syntax : public static boolean equals(int[] a, int[] a2) Parameters : a - one ar 3 min read Java Program to Sort Objects in ArrayList by Date The foremost tool that strikes is the sort() method to be used for the comparator mechanism of the Collections class which sorts in the decreasing order. Yes if in generic we want to achieve the goal considering the boundary condition where objects to sorted are user-defined then blindly do with Com 6 min read Like