Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to access Null key value of an HashMap

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

As HashMap allows to add one NULL key .
Can anybody please let me know .

How can we access the value of this null key ??


How can i access the value called as 'test' ??

Thanks in advnace .
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By curiosity, did you try get(null) ?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
get(null);???
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could have tried this yourself....

FYI, as far as I know, you can keep on adding null keys to the same HashMap except that when you try to retrieve the value it will return the latest entry into the HashMap against a null key. Correct me if I am wrong...


-Ninad
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you're right. If null is allowed (it isn't in Hashtable for instance) then it behaves like any other key: calling map.put(null, value) will overwrite any previous value for the null key.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"Rob Prime" wrote: No, you're right. If null is allowed (it isn't in Hashtable for instance) then it behaves like any other key: calling map.put(null, value) will overwrite any previous value for the null key.



Thanks Rob!


 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried , its working .

but why did Java allowed a null key to be inserted inside a HashMap , is this behaviour would be useful anywhere ??

Thanks .
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ravi Kiran V wrote:

but why did Java allowed a null key to be inserted inside a HashMap , is this behaviour would be useful anywhere ??



https://coderanch.com/t/507881/java/java/HashMap
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"Ravi Kiran V" wrote: ... but why did Java allowed a null key to be inserted inside a HashMap , is this behaviour would be useful anywhere ??



There has already been some debate over this matter in some other thread, have a look at this for your reference.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:
https://coderanch.com/t/507881/java/java/HashMap



Coincidentally, the above post has also been created by someone by the name Ravi Kiran
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic