wood burning stoves 2.0*
The moose likes Java in General and the fly likes Any predefined method to disallow null keys in hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Any predefined method to disallow null keys in hashmap" Watch "Any predefined method to disallow null keys in hashmap" New topic
Author

Any predefined method to disallow null keys in hashmap

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
hi everyone,

in one of interviews ,i faced the question like
Any predefined method is there in collections disallow null keys in hashmap.

please help me out.



Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

Hi,
I think you have to subclass hashmap and override its put method inside that check for null key if yes throw IllegalArgumentException

thanks & regards
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
thanks for reply,
but, he(interviewer) told me that there is a predefined method for to filter null keys.
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

shyam sunder prasad wrote:thanks for reply,
but, he(interviewer) told me that there is a predefined method for to filter null keys.


I don't think so, please look in api

thanks & regards
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
You can either (A) use a Hashtable, which never allows null keys or values, or (B) use Collections.checkedMap(), maybe using Object.class for the second and third parameter, to create a wrapper around any Map (including a HashMap) which will not allow null keys or values.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Mike Simmons wrote:You can either (A) use a Hashtable, which never allows null keys or values, or (B) use Collections.checkedMap(), maybe using Object.class for the second and third parameter, to create a wrapper around any Map (including a HashMap) which will not allow null keys or values.



I am willing to bet that it is option B that the interviewer was going for. Regardless, if so, it is a pretty useless interview question -- either the candidates knows it or the candidate doesn't know it, and you can't infer much from either case.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

It isn't explicitly mentioned on the Javadoc page for Collections.checkedMap, but the put and putAll methods of the returned instance call the following method:
Since null is not an instance of any type a null key or value will result in a ClassCastException NullPointerException (because of the call to getClass()). However, as I said, it's not explicitly mentioned. I don't regard null as being of the wrong type.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Rob Spoor wrote:However, as I said, it's not explicitly mentioned. I don't regard null as being of the wrong type.

I agree; I had to test it myself before I posted that one. Seems like an oversight in the documentation. And a further reason why this is a poor interview question.
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
please help me out how to avoid null keys with predefined method?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Well, have you read this thread? I recommend using Collections.checkedMap(). Do you know how to look up a class and method in the Java API? Can you compile and run a Java program? Try to figure out how to create a Map using checkedMap(), and then try inserting a null into that map. Try it and find out what happens. If you can't, or if you don't understand some part of this, then post again and tell us what part you're having trouble with. We're happy to help, but if you simply ignore the previous answers, we can't tell what you understand and what you don't.
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62


if i try to add null key then it's raising an exception,

Thank you





 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Any predefined method to disallow null keys in hashmap
 
Similar Threads
Code to store null keys and null values in hashmap
why we HashMap allow null key
scenerio from examLab
HashMap.keySet()
Why Hashtable does not accept null as keys and values ?