Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Why HashMap allows to add one NULL key ?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why disallow it?
 
Cm Ananth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Cm Ananth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul & Maneesh..

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

Any other examples also welcome
 
Marshal
Posts: 74359
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello - i was running some basic code tests to try and do this and keep getting a runtime exception whenever i add another element to the Map/Set after putting a null key initially. Seems that it always fails on the equals method when comparing the current object being inserted to the previous null entry. This makes sense so i am curious how this is implemented in the real world. A bit confused.

Any thoughts? Thanks.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Carlisle wrote:Hello - i was running some basic code tests to try and do this and keep getting a runtime exception whenever i add another element to the Map/Set after putting a null key initially. Seems that it always fails on the equals method when comparing the current object being inserted to the previous null entry. This makes sense so i am curious how this is implemented in the real world. A bit confused..


Can you show us the code you are using and what exception you are getting. It works fine for me.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Specifically, look at the equals() method of the class you are putting in to the map. I would suspect that is where the exception is coming from.
 
Brian Carlisle
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure - taken from the SCJP book. I added the null entry to see what would happen. Also tried changing the int to String for size and using equals() but no luck. The same happens when i convert the code to use a LinkedHashMap.

It makes sense to me logically why the exception is thrown.

 
Brian Carlisle
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it is most certainly in the equals method. Can you show me a simple example of this working? Much appreciated.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Carlisle wrote:


This equals() method isn't very good. The error is on line 3 (in the if condition). You assume the Object being compared to (o) is not null, you dereference it without first checking if it was nulll. So this would fail. You need to do a first check. A typical equals() method would be like this:


<edit>Forgot the case where other might be of a different type. This is one way of checking that. Another is to use instanceof, but there is a difference in semantics between the two options... I chose to check classes for equality</edit>
 
Brian Carlisle
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve!
 
Sheriff
Posts: 22508
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I usually follow that flow but shorten it a bit:
I also wonder why in hashCode you divide the size by 5. Why not simply return size itself? It's an int, so it's a valid value.

Edit: I just saw that Steve's example uses Turtle.class to compare to. I prefer not to use one fixed class except if the class is final (in which case I use instanceof). Using getClass() instead allows sub classes to still have a valid equals method. Let's say you have class SnappingTurtle extends Turtle, then in Steve's example a new SnappingTurtle(5) will be equal to a new Turtle(5), which I disagree with. In my example, the two will be unequal even though they have the same size.
 
Brian Carlisle
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the hashcode method is definately bizarre. But its straight from the SCJP test book. Prepping for the exam and want to be prepared for anything.

Thanks.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because HashMap allows only unique keys. Repetition of the same value for different keys are allowed.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic