Comparing two ArrayList In Java
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.
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
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.
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.