File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why HashMap allows to add one NULL key ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why HashMap allows to add one NULL key ?" Watch "Why HashMap allows to add one NULL key ?" New topic
Author

Why HashMap allows to add one NULL key ?

Cm Ananth
Ranch Hand

Joined: Oct 20, 2005
Posts: 44
Could someone explain me why HashMap allows to add one NULL key ? Because i don't see any practical use of adding NULL as the key and storing a value in it..

In which case we need to use NULL as a key? Please explain with some examples.

Thanks in advance..


By Ananth, Bangalore
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Why disallow it?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Cm Ananth
Ranch Hand

Joined: Oct 20, 2005
Posts: 44
I feel there won't be any use case for adding NULL as a key and store a value for it. I wonder why such a feature is allowed by java and some real world examples will help me to understand the use of this feature.

Thanks for your reply.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Could be used to supply a default value I suppose. I'm imagining a situation where an application gives you a choice of options, and the result of these options are mapped with a HashMap. Now suppose this option choice is itself optional. The user hasn't selected anything therefore the choice is null, so return whatever value is in the Map with a null key.

Might not be how this is used, but I can't think of any benefit of preventing people using a null key?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8566

One real life scenario where even a "null" can convey information.

I am working on a project where I have "jobs" (e.g. copy some data from some place to another). I also have a "schedule" which can be associated to the "jobs". The "schedule" contains information on at what time the job should be executed and when should it be repeated.
There is a simple check which we have put in. If the job schedule is null, we execute the job only once. Else we execute it as per schedule.


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Cm Ananth
Ranch Hand

Joined: Oct 20, 2005
Posts: 44
Thanks Paul & Maneesh..

Paul's example has given me some idea of where HashMap NULL keys can be used.

Any other examples also welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
Several people have given useful examples of when a null key is useful, but the real answer to the original question is that whoever designed the class thought it would be most useful like that.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by CM Ananth:
I feel there won't be any use case for adding NULL as a key and store a value for it. I wonder why such a feature is allowed by java and some real world examples will help me to understand the use of this feature.


I think the question should be reverted: how would disallowing null as a key be useful?

Why actively disallow something just because we can't yet think of a case where it would be useful?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Implementing your own version of HashMap that doesn't allow a null is easy by just extending or wrapping the HashMap class and putting some extra checking in. Whereas creating your own class that allowed nulls if HashMap didn't allow them would be a little more difficult.


Joanne
 
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: Why HashMap allows to add one NULL key ?
 
Similar Threads
HashMap
How to access Null key value of an HashMap
Hashtable and Hashmap Doubt
HashMap
Hashtable doesn't allow null value and HashMap allows only one null as key