• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

TreeSet and HashSet

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why below code print size 1? the class doesn't override equals and hashCode methods.

If I run the code with HashSet, it will print 2 as I expected. why is not the same for TreeSet?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From K & B, page 542 (Exam Watch):

When using HashSet or LinkedHashSet, the objects you add to them
must override hashCode(). If they don�t override hashCode(), the default Object.hashCode() method will allow multiple objects that you might consider "meaningfully equal" to be added to your "no duplicates allowed" set.

 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anthony Karta:
If I run the code with HashSet, it will print 2 as I expected. why is not the same for TreeSet?



Do you agree that new TestComparable().compareTo(new TestComparable()) is 0, but new TestComparable().equals(new TestComparable()) is false?

That sort of tells the story.
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Cole:


Do you agree that new TestComparable().compareTo(new TestComparable()) is 0 "Agreed" - it's for sorting in TreeSet

, but new TestComparable().equals(new TestComparable()) is false?
"Agreed"
That sort of tells the story.



that's why I said there are 2 object of TestComparable. It works for HashSet but not TreeSet (in the code above)
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the specification of TreeSet it is given as

"Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. "

From the above paragraph, it is clear that TreeSet is using compareTo() function for checking whether two instances are the same. Here we are providing function for compareTo() , and that function returns zero, when we try to add the second instance of TestComparable. So the second object is not added to the TreeSet.

But in the case of HashSet, hashCode() and equals() will be invoked. Since we are not overriding those functions, equals returns false and the second object is added to the HashSet.
 
please buy my thing and then I'll have more money:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic