• 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

General question about HashMaps in java

 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried it ?
You'll always learn more by experimenting than just being told the answer.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the Javadoc of java.util.Map?
 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
  •  
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic