*
The moose likes Beginning Java and the fly likes TreeSet not allowing the addition of a unique object, yet the equals() method is not being called Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TreeSet not allowing the addition of a unique object, yet the equals() method is not being called" Watch "TreeSet not allowing the addition of a unique object, yet the equals() method is not being called" New topic
Author

TreeSet not allowing the addition of a unique object, yet the equals() method is not being called

Faz Ali
Greenhorn

Joined: Dec 02, 2010
Posts: 21
The SCJP6 study guide I am using states that "Your good friend the equals() method determines wheter two objects are identical (in which case only one can be in the set)"
The code I've written below is not following the above statement as far as I can see. There must be something obviously wrong with my code but I can't see it. I cannot figure out why the code is not allowing car3 to be added to the set since equality between cars is based solely on the vehicleID which is unique between my 3 instances. I'm pretty sure I've overidden the equals() method correctly yet whenever I run this code, the System.out.println line in the equals() method does not run when the cars are added to the set and only 2 of the 3 are being added.



The output from the above code is:

2
Aston Martin DBS 29086335
Lamborghini LP640 343465734


It should be:

3
Aston Martin DBS 29086335
Lamborghini LP640 343465734
Lamborghini LP640 297853624


Can anyone point me in the right direction?

Thanks
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3010
    
  10
Unlike other Set implementations, TreeSet doesn't actually use equals() at all. It uses compareTo() instead - if compareTo() returns zero, that means the two objects are "equal" as far as the TreeSet is concerned. Right now, both your Lamborghinis have the same age, and compareTo says they are therefore equal to each other, and the TreeSet allows only one of the Lamborghinis to remain in the set as a result.

If you want to test your hashCode() and equals() methods, use a HashSet, not a TreeSet.

If you want to use a TreeSet and still see all three cars, you need to modify compareTo() so that two different Lamborghinis are not equal. Perhaps after you compare the ages, if the ages are equal, don't return 0 (yet) but instead compare something else, like vehicleID.
Faz Ali
Greenhorn

Joined: Dec 02, 2010
Posts: 21
Mike Simmons wrote:Unlike other Set implementations, TreeSet doesn't actually use equals() at all. It uses compareTo() instead - if compareTo() returns zero, that means the two objects are "equal" as far as the TreeSet is concerned. Right now, both your Lamborghinis have the same age, and compareTo says they are therefore equal to each other, and the TreeSet allows only one of the Lamborghinis to remain in the set as a result.

If you want to test your hashCode() and equals() methods, use a HashSet, not a TreeSet.

If you want to use a TreeSet and still see all three cars, you need to modify compareTo() so that two different Lamborghinis are not equal. Perhaps after you compare the ages, if the ages are equal, don't return 0 (yet) but instead compare something else, like vehicleID.


Very quick reply! tyvm
I didn't write the code to test the hashCode and equals() methods, I just wrote the Car class primarily to test the TreeSet which is the reason for it being so very basic and implementing Comparable. I wish this book (Kathy & Bert SCJP6 study guide) made clear that TreeSet uses compareTo() rather than equals(); it doesn't mention this fact at all
Again, tyvm
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Faz Ali wrote:tyvm

Please UseRealWords: "thank you very much".

I wish this book (Kathy & Bert SCJP6 study guide) made clear that TreeSet uses compareTo() rather than equals(); it doesn't mention this fact at all

But the Javadoc page of TreeSet does.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeSet not allowing the addition of a unique object, yet the equals() method is not being called