3.jpg

微信公众号:爱问CTO
专业编程问答社区
www.askcto.com

从 JDK 文档中,我们可以看到当时作者的想法。

This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method

可以看出,当时不允许是因为希望每个 key 都会实现 hashCode 和 equals 方法。而 null 显然没有。由于null不是对象,因此不能在其上调用.equals()或.hashCode(),因此Hashtable无法将其计算哈希值以用作键。


爱问CTO
18 声望2 粉丝

爱问CTO 专业编程问答网站