• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any predefined method to disallow null keys in hashmap

 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for reply,
but, he(interviewer) told me that there is a predefined method for to filter null keys.
 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me out how to avoid null keys with predefined method?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Thank you





 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic