• Post Reply Bookmark Topic Watch Topic
  • New Topic

why hashmap allows only 1 null value..?  RSS feed

 
Damodara Reddy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any significance behind hashmap allowing only one null value..?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put as many null values as you like in a HashMap. What makes you think differently?
 
Damodara Reddy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam sorry Paul Sturrock,
Does hashmap allow null keys, I heard that it allows one null key, is it? If so what might be the reason giving us that option?
 
Damodara Reddy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you make me bit clear about the null keys and values of Hashtable and Hashmap ?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Damodara Reddy:
Iam sorry Paul Sturrock,
Does hashmap allow null keys, I heard that it allows one null key, is it? If so what might be the reason giving us that option?


Yes, though obviously you can only have one null key. If you try to put another value in the Map with a null key it replaces the first value, since Maps are made up of key/value pairs. Hashtables don't allow null keys or values (well, the compiler will allow them but you'll get a NullPointerException when you run your program).
 
Damodara Reddy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul Sturrock for your reply.
 
Damodara Reddy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock,
can you please tell me, in which case we prefer to insert a value with null key into hashmap?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I follow your question. Are you just looking for a reason to use a null key in a map?
 
Damodara Reddy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, reason for using a null key.
 
Damodara Reddy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Paul Sturrock, can you clear me about this issue..?
In which situation/scenario we would go for inserting a value with null key into HashMap...?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Damodara Reddy:
Hai Paul Sturrock, can you clear me about this issue..?
In which situation/scenario we would go for inserting a value with null key into HashMap...?


Personally, I always thought it was kindof silly to allow a null key -- I don't think that I ever needed it. But hey... It's a general use collection. There is no reason to justify everything. The designers of the collection may have simply decided to allow any key, including null.

Henry
 
Damodara Reddy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Henry Wong.
I have one more issue regarding HashMap and Hashtable.
When the synchronization comes into picture, which one is efficient among the bellow,
using HashMap in a synchronized block, or using a Hashtable...?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Damodara Reddy:
Thanks for your reply Henry Wong.
I have one more issue regarding HashMap and Hashtable.
When the synchronization comes into picture, which one is efficient among the bellow,
using HashMap in a synchronized block, or using a Hashtable...?


This question gets asked quite frequently - a search in our Thread forum should give a lot to read. If you still have questions, I'd suggest to start a new topic there...
 
Damodara Reddy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply Ilja Preuss.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!