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 behaviour so different?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From the
HashtableJavaDoc:In a nutshell, since
nullisn’t an object, you can’t call.equals()or.hashCode()on it, so theHashtablecan’t compute a hash to use it as a key.HashMapis newer, and has more advanced capabilities, which are basically just an improvement on theHashtablefunctionality. As such, whenHashMapwas created, it was specifically designed to handlenullvalues as keys and handles them as a special case.Specifically, the use of
nullas a key is handled like this when issuing a.get(key):