aspose file tools
The moose likes Java in General and the fly likes How to access Null key value of an HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to access Null key value of an HashMap" Watch "How to access Null key value of an HashMap" New topic
Author

How to access Null key value of an HashMap

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .


Save India From Corruption - Anna Hazare.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

By curiosity, did you try get(null) ?


[My Blog]
All roads lead to JavaRanch
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

get(null);???


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
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


Don't walk as if you rule the world, walk as if you don't care who rules it...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
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

Joined: Apr 18, 2009
Posts: 2234

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 .
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 8145
    
  52

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 ??



http://www.coderanch.com/t/507881/java/java/HashMap

[My Blog] [JavaRanch Journal]
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
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

Joined: Jan 05, 2010
Posts: 64


Coincidentally, the above post has also been created by someone by the name Ravi Kiran
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to access Null key value of an HashMap
 
Similar Threads
HashMap, contains a key ?
Confirmation on HashMap
query regarding null key in hashmap
hash map
How HaspMap use null key , to calculate it's hashcode for storing in particular location?