Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Values in HashMap

 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are the values put into a HashMap stuck to the bucket in which they are put in initially based on a key.....

won't they get transferred to another bucket even if the corresponding key's state and hence it's hash code changes............
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
won't they get transferred to another bucket even if the corresponding key's state and hence it's hash code changes............


A hash code implementation that returns different values with time is incorrect.

The objects need not reside in the same bucket all the time. If the hash map size changes for example, the objects will have to be rearranged to reflect the new size of the underlying store. The way they are rearranged depends on the hashing algorithm used
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


is my code and the output is....




this is what i am referring to.....
now does my question make any sense.....

i suppose that i wasn't clear before.........

thanks in advance my friend.....
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this revised program




Also see this statement from the documentation.

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply friend....


does the value's hash code have anything to do with the bucket in which it is put or it is purely dependent on key's hash code only.....
equals() is not a problem for me.....
hascode() is only confusing me.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic