AbstractList equals() method in Java with Examples
The equals() method of java.util.AbstractList class is used to compare 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.
public boolean equals(Object o)
Parameters: This method takes the object o as a parameter to be compared for equality with this list.
Returns Value: This method returns true if the specified object is equal to this list.
Below are the examples to illustrate the equals() method.
First ArrayListlist : [A, B, C, D, E] Second ArrayList : [A, B, C, D, E] Are both list equal : true
First ArrayListlist : [10, 20, 30, 40, 50] Second ArrayList : [10, 20, 30] Are both list equal : false
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.