• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

why hashmap allows only 1 null value..?

 
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..?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE 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
Hibernate Eclipse IDE 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
Hibernate Eclipse IDE 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...?
 
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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...?
 
author
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic