wood burning stoves 2.0*
The moose likes Java in General and the fly likes General question about HashMaps in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "General question about HashMaps in java" Watch "General question about HashMaps in java" New topic
Author

General question about HashMaps in java

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello,
I would like to know what happens when two objects equal according to equals() are used as keys in order to store values into a HashMap.
1. Does it make sense to do that?
2. What happens when one tries to put the second key/value into the HashMap?
3. What happens when one tries to get the second key from the HashMap?
Any comment welcome,
Julien.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
What happened when you tried it ?
You'll always learn more by experimenting than just being told the answer.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Have you read the Javadoc of java.util.Map?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Thanks for your replies and sorry: I missed an important bit of information in my question.

I meant to say: What happens when two objects whose hashCode method has not been overriden and therefore inherit the hashcode method from Object and are equal according to equals() are used as keys in order to store values into a HashMap.

1. What happens when one tries to put the second key/value into the HashMap?
My answer: it works fine insofar as the hashcode method from Object returns a unique id.
2. What happens when one tries to get the second key from the HashMap?
This is where your comments are welcome.

You'll always learn more by experimenting than just being told the answer.

Joanne, I do know experimenting is a good way to learn. However sometimes exchanging with others can be stimulating and interesting as well...

Julien.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

The two keys will probably not be seen as equals. HashMap and Hashtable use buckets, and the hash code is used to determine in which bucket an object is placed. So if the hash codes are different the keys will be put / looked for in different bucket. This is the very reason why the equals and hashCode method are so tightly coupled.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
It depends whether you have a hash code collision. If you get both keys into the same bucket, because the lower-significance bits in their hash codes (in binary) happen to be the same, then they will behave as if they were the same object. Otherwise they will behave (I would have thought) as different objects.

I would remind any greenhorns reading this thread:
  • This practice is incorrect because the hashCode method ought to be overridden along with equals(), but this is obviously permissible in this case because it is simply a "What-if?" application which will never be released into the "wild".
  •  
     
    subject: General question about HashMaps in java
     
    Similar Threads
    how hashmap identify the duplicate values? (i need internal logic)
    Passed SCJD
    What is an overriding bean declaration?
    HashMap and HashTable in java
    Record Locking Test