Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null key in a HashMap

 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Key in a HashMap is stored as hashcode.So if there is a null key,how is it stored internally?
 
Paul Clapham
Sheriff
Posts: 21296
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21296
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob!
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21296
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic