• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

null key in a HashMap

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob!
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic