Why does Hashtable not take null key?

后端 未结 6 1019
傲寒
傲寒 2020-12-08 05:35

Why does Hashtable not take a null key?

Also why does HashMap allow null keys?

What is the purpose of making these two classes Key

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 06:09

    Hashtable predates the collections framework, and was part of JDK 1.0. At that time null keys were probably considered not useful or not essential, and were thus forbidden. You might see it as a design error, just as the choice of the name Hashtable rather than HashTable.

    Then, several years later, came the collections framework, and Hashtable was slightly modified to fit into the framework. But the behavior on null keys was not changed to keep backward compatibility.

    Hashtable should be deprecated, IMHO.

提交回复
热议问题