• Post Reply Bookmark Topic Watch Topic
  • New Topic

Immutable & HashMap Keys  RSS feed

 
Ranch Hand
Posts: 116
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read that for HashMap Keys, the objects should be immutable. But I didn't understand this concept... how the immutable objects will help in case of hashmap keys?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is simply a requirement of the HashMap class. The hash code for the key isn't allowed to change, once it is added to the map. If you use a mutable object for the key, and hence, likely have a changing hash code, changing the value of the key object will corrupt the hashmap.

Henry
 
Vaibhav Gargs
Ranch Hand
Posts: 116
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry!

But if we override equals() and hashcode() methods of custom object (being used as key in the map) and we don't change the values of custom object, then won't it be sufficient?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav Gargs wrote:
if we override equals() and hashcode() methods of custom object (being used as key in the map) and we don't change the values of custom object, then won't it be sufficient?


As long as the hash code of the key (and whether it is equals()) does not change during the lifetime of its usage, the hash map should not be corrupted.

Henry
 
Vaibhav Gargs
Ranch Hand
Posts: 116
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry! So, I am confused why do we need immutable objects for Hashmap keys? We know it won't be changed during program.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a requirement, but it's strongly recommended.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav Gargs wrote:We know it won't be changed during program.

Famous last words. 

You and your current team may know now not to change any objects that have been used as keys in a Map. However, if these objects or even the Map that uses them are passed around to various classes in your program, it's conceivable that sometime in the future an unwary or unaware maintenance programmer, someone new to the team or maybe even someone on your current team, could inadvertently make changes that will end up corrupting the Map. Using immutable objects as keys in a Map is one line of defense against this potential source of bugs.
 
Vaibhav Gargs
Ranch Hand
Posts: 116
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your valuable inputs
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!