Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Faz Ali
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic