Meaningless Drivel is fun!*
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
Author

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:

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 ]
 
 
subject: equals and Lists