• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why does HashMap allow 'null' values

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been wondering about this a little and wanted to know what could be the reason for allowing 'null' values into HashMap but not into HashTables.

Thanks
Vishnu
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you really think this is a topic for the Advanced forum?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that Hashtable came first. I think that there is rarely any reason to want null values or a null key in a hash table, and so the people who made Hashtable did not allow them. But later when the collections framework was expanded, the Map interface was written to be a bit more general. Even though the need for null values and the null key is rare, some people do use them occasionally. So Map was written to allow them (optionally), and the new HashMap implmentation definitely does allow them. However they didn't want to change the existing Hashtable implementation, so they left it alone.
 
Anasapurapu Vishnu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the reply, could you please let me know a situation where we would need to store 'null' values into a HashMap
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't really think of a good reason for this - personally I never use nulls in maps. Some other people do, and it doesn't really bother me, but I've never seen seen a need. Perhaps someone else can suggest a good example.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the example of using a Map to represent a row of data from a database. Databases often have nullable columns. Because Map allows null values, our map can have a key/value pair:
SOME_COLUMN_NAME -> null
This type of entry indicates that the table from which the data originated really does have a column named "SOME_COLUMN_NAME." If Map didn't allow null values, we couldn't (easily) use the map described above to distinguish between the following two scenarios:
- The backing table does not have a column named "SOME_COLUMN_NAME"
- The backing table does have a column named "SOME_COLUMN_NAME" but in this particular case the related value is null.
[ January 23, 2008: Message edited by: Dave Wingate ]
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic