aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Values in HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Values in HashMap" Watch "Regarding Values in HashMap" New topic
Author

Regarding Values in HashMap

karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

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............


KARTHICK.C , SCJP6-93%
(Born to Win)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196



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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

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.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Values in HashMap