File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals and Lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals and Lists" Watch "equals and Lists" New topic

equals and Lists

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Well, it never occurred to me that an ArrayList could equal a LinkedList.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Neither did I. I checked API and both inherit AbstractList.equals:

public boolean equals(Object o)
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.

It does make sense after all.

SCJP2. Please Indent your code using UBB Code
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Jose. I am glad to know I am not the only one who is surprised.
AbstractList equals iterates over two Lists
ListIterator e1 = listIterator();
ListIterator e2 = ((List) o).listIterator();
while(e1.hasNext() && e2.hasNext()) {
[ September 03, 2003: Message edited by: Marlene Miller ]
I agree. Here's the link:
subject: equals and Lists
It's not a secret anymore!