Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order.
Examples
package com.logicbig.example.arraylist;
import java.util.*;
public class EqualsExample {
public static void main(String... args) { // Compare two ArrayLists ArrayList<String> list1 = new ArrayList<>(Arrays.asList("A", "B", "C")); ArrayList<String> list2 = new ArrayList<>(Arrays.asList("A", "B", "C")); System.out.println("Are lists equal? " + list1.equals(list2)); } }
Output
Are lists equal? true
JDK 25
package com.logicbig.example.arraylist;
import java.util.*;
public class EqualsExample2 {
public static void main(String... args) { // Order matters in equality ArrayList<String> list3 = new ArrayList<>(Arrays.asList("A", "B", "C")); ArrayList<String> list4 = new ArrayList<>(Arrays.asList("C", "B", "A")); System.out.println("Are reversed lists equal? " + list3.equals(list4)); } }