aspose file tools*
The moose likes Java in General and the fly likes null key in a HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "null key in a HashMap" Watch "null key in a HashMap" New topic
Author

null key in a HashMap

Max White
Ranch Hand

Joined: Jun 28, 2008
Posts: 85
Key in a HashMap is stored as hashcode.So if there is a null key,how is it stored internally?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

Max White wrote:Key in a HashMap is stored as hashcode.


I don't see any evidence of that. What makes you think so?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

With some fixed value for the hash code. An excerpt of the source code of HashMap (which you can find in the src.zip file in your JDK folder):
Here hash() is a method that
Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Max White
Ranch Hand

Joined: Jun 28, 2008
Posts: 85
Collections such as HashMap and HashSet use the hashcode value of an object to determine how the object should be stored in the collection, and the hashcode is used again to help locate the object in the collection



I read something like above in Kathy & Bert(Collections chap) & so interpreted it that way.What does the book means by "hashcode value of an object"?
Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

There's a big gap between that statement and "Key in a HashMap is stored as hashcode". All it says is that the HashMap class uses the value of an object's hashcode.

And what the book means by "the hashcode value of an object" is "the value returned by the hashCode() method of the object".
Max White
Ranch Hand

Joined: Jun 28, 2008
Posts: 85
Thanks Rob!
Max White
Ranch Hand

Joined: Jun 28, 2008
Posts: 85
All it says is that the HashMap class uses the value of an object's hashcode


So what does HashMap class do with the hashcode value?
Is it not used as a key?for faster access..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

Max White wrote:
All it says is that the HashMap class uses the value of an object's hashcode


So what does HashMap class do with the hashcode value?
Is it not used as a key?for faster access..


It uses the hashcode value to figure out which bucket the key/value pair should be in. This is certainly for faster access but saying it's "used as a key" is a very confusing way of saying that, given that there's already something else which is called the "key". That's why I am opposing your attempts to say it's some kind of key.

What the HashMap stores for each of its entries is the key object, the value object, and the hashcode of the key object (adjusted as mentioned earlier by Rob Spoor). But you're trying to refer to the hashcode of the key object as "a key". That's too confusing for me and it's not necessary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null key in a HashMap