This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Sets and hash code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sets and hash code" Watch "Sets and hash code" New topic
Author

Sets and hash code

Arthur Sc Chan
Greenhorn

Joined: Sep 07, 2010
Posts: 11
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3394
    
    9
As far as I know, TreeSet never uses hashCode(). TreeSet is based on Comparator/Comparable.
Arthur Sc Chan
Greenhorn

Joined: Sep 07, 2010
Posts: 11
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?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
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


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sets and hash code
 
Similar Threads
equals hashcode doubt sjcp 1.4
HashMap and HashSet
What's wrong with this code? Why I am able to add duplicates with this code.
Problem overriding hashcode and equals
hashcode and equals