TrainBeaser for iPhone
[Logo] JavaRanch » Big Moose Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

how does equals() work with lists as objects?

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Messages: 88




output:
false
true
false
false

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?

SCJP 6.0 90%
Waclaw Borowiec
Greenhorn

Joined: Dec 14, 2009
Messages: 18

Only Set, List and Map interfaces have a requirement to implement equals method in an elements-based way. In case of Collection it's not necessary and in a Queue it's even discouraged. That's what API for these types says.
Mazer Lao Tzu
Ranch Hand

Joined: Jan 20, 2010
Messages: 35

Sets, Lists, and Maps do not necessarily override equals. LinkedList does because it is a subclass of AbstractList which overrides equals. However, we can not say anything in general about whether a implementation of a Collection does or does not override equals. You will have to check the javadoc for the particular implementation you are working with.

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).


-- Mazer
Sreenivasa Rao Venepalli
Greenhorn

Joined: Oct 08, 2009
Messages: 4

Please refer API http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#equals(java.lang.Object)
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."

Regards,
Sreenivas
Rajesh Shinde
Greenhorn

Joined: Jan 29, 2010
Messages: 11

System.out.println(p1.equals(p2));

Should this not return true?

scjp6 processing.....
Mazer Lao Tzu
Ranch Hand

Joined: Jan 20, 2010
Messages: 35

Sreenivasa - Be careful. That statement applies to the Vector equals method, not all list-based equals methods.

Rajesh - No, will not return true because:
  • p1 is a PriorityQueue
  • p1 and p2 are different objects in memory (they have different memory addresses)
  • PriorityQueue does not override the equals method from Object.


  • -- Mazer
    Sreenivasa Rao Venepalli
    Greenhorn

    Joined: Oct 08, 2009
    Messages: 4

    Hi Mazer,
    I am sorry if I mislead you. I thought all implementing class of List interface supports above statement. is it not true?

    Regards,
    Sreenivas
    Ankit Garg
    Bartender

    Joined: Aug 03, 2008
    Messages: 6394

    Sreenivasa, you are right, the new documentation of List interface defines the behavior of equals method implemented by all List implementation more clearly
    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

    SCJP 6.0 98%, SCWCD 5 98%, Javaranch SCJP FAQ, SCWCD Links
    Mazer Lao Tzu
    Ranch Hand

    Joined: Jan 20, 2010
    Messages: 35

    Apparently I was wrong. Sorry about that. Thank you Ankit for correcting me.


    -- Mazer
     
     
     
    Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
     
    RSS feed
     
    New topic
    MyEclipse Enterprise Workbench