• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sets and hash code

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I know that, sets use hash code and equals to ensure no duplication in the set, then if I do something like override the hashCode method in an object and print hello world, then insert this object to a set, can I suppose that hello world must be shown on the console??
Something like
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Certain implementations of interface Set, such as HashSet and TreeSet, use the hash codes of objects that you put in the Set. So, such Set implementations will indeed call the hashCode() method of your class.

However, when exactly they call it, depends on how exactly HashSet and TreeSet are implemented. You would expect that "Hello World!!!" is printed in your example code, and indeed it is if you run it (did you try?). But HashSet and TreeSet don't specify anywhere when they are going to call hashCode(), so there is no guarantee that it will always be called when you put an object in the set.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, TreeSet never uses hashCode(). TreeSet is based on Comparator/Comparable.
 
Arthur Sc Chan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You would expect that "Hello World!!!" is printed in your example code, and indeed it is if you run it (did you try?). But HashSet and TreeSet don't specify anywhere when they are going to call hashCode(), so there is no guarantee that it will always be called when you put an object in the set.


Yes. I have tried that code and seeing the Hello World in the console, but like threading, sometimes the thing I see in console may not be the only way it can be displayed, and so I just want to confirm that sentence must print out when add is called. And after my understanding on your word, it is no guarantee that hashCode method must be called when calling add method in Set implementation (say HashSet<Test>), am I correct?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arthur Sc Chan wrote:sometimes the thing I see in console may not be the only way it can be displayed, and so I just want to confirm that sentence must print out when add is called.


You could print a stack trace in your hashCode method and that will show you where it is called from. Replace your print statement with

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic