Comparing two ArrayList In Java - GeeksforGeeks

04 Dec, 2020
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 Javaimport 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 AyyarList 1        ArrayList1.add("item 1");        ArrayList1.add("item 2");        ArrayList1.add("item 3");        ArrayList1.add("item 4");          // insert items in AyyarList 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");        }    }}
 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 equal

Time Complexity: O(N), where N is the length of the Array list.
Java-ArrayList
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Iterate through List in Java