In the program above, list1 and list2 are compared using equals() in line 1 and line 2.
unless we override the equals() for comparing object references, equals() always returns false. As per the explanation equals() returns "false" in line 1 and line 3. But it returns "true" in line 2?
so does LinkedList or any Collection, overrides the equals() method of object?
Is the equals() method overridden by List, Set, Map ?
As line 4 returns false, equals() is not overridden by PriorityQueue?
unless we override the equals() for comparing object references, equals() always returns false
This is not true. If you don't override equals, it will return true if and only if the two objects references point to the same object (the same address in memory).
It states that "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. In other words, two Lists are defined to be equal if they contain the same elements in the same order."
Should this not return true?
Rajesh - No, will not return true because:
I am sorry if I mislead you. I thought all implementing class of List interface supports above statement. is it not true?
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. This definition ensures that the equals method works properly across different implementations of the List interface.
So if you try this code, you'll get true as the answer