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.
"I'm not back." - Bill Harding, Twister
Joined: Jan 18, 2008
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
Joined: Jan 30, 2000
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.
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 ]