File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMap question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMap question" Watch "HashMap question" New topic
Author

HashMap question

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
I have the following sections of code:






How is the hash code coming into play here? I have been used to a Map as simply a key/value pair type of table, but I am not sure how the hash code is being used.


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

Rachel Glenn wrote:I have the following sections of code:





How is the hash code coming into play here? I have been used to a Map as simply a key/value pair type of table, but I am not sure how the hash code is being used.


HashMaps only hash on the key, so in your example, the hashCode() method of the Dog class is not being used.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Thanks. After doing some further reading I figured it out!!!
Prajwal Shetty
Ranch Hand

Joined: May 26, 2011
Posts: 33

We generally Override the hashCode() inorder to optimize the searching.
Consider the value returned by the hashCode() to be a bucket label.
So, In the example above you created only one Object of Dog "aiko" that was stored in bucket labelled '4'.

Please correct me Ranchers if I am wrong.


OCPJP 6, OCEJWCD 6, OCPJBCD 5
sakthi FrontTech
Greenhorn

Joined: May 18, 2010
Posts: 2
We generally Override the hashCode() inorder to optimize the searching.

If you see the HashMap implementation, hashCode of the object(say Dog Object in this case) will be manipulated again to avoid the weak hashCode implementation, so hashCode alone need not be the reason for optimize the searching, of Course its one of the reason. Refer below code




In the example above you created only one Object of Dog "aiko" that was stored in bucket labelled '4'.

In Java's HashMap implementation hashCode of the object doesn't specify the location or bucket the hash method pasted above will specify the location or bucket. The location or bucket will be changed when the threshHold percentage is reached.
If you want to know more about the HashMap please refer source of the HashMap.
 
Don't get me started about those stupid light bulbs.
 
subject: HashMap question
 
Similar Threads
Collection - Chap 7 (Maps) (Problem in Understanding an example in K&B SCJP 1.6)
Regarding Values in HashMap
override hashCode() and equals() to store an object inside hashMap not working properly
confusion in HashMaps
Chapter 7: Generics and Collections K and B pg no 584