【发布时间】:2016-10-12 17:02:58
【问题描述】:
目前我钻研 JSE 源代码是为了好玩。从一些教程中我发现了一个原则two equals objects (i.e. as to object a and b a.equals(b) returns true) must have the same hashcode, on the other hand, two objects with the same hashcode are not necessarily equals。根据Java API的HashTable源代码(http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Hashtable.java),例如参考第254行,我们几乎可以推导出equals => same hashcode,但我仍然不能完全理解为什么two objects with the same hashcode are not necessarily equals。任何人都可以对此给出更多推理吗?在此先感谢
注意!这个问题更感兴趣的是为什么具有相同哈希码的两个对象不一定相等
【问题讨论】:
-
可能的
Strings(例如)多于可能的ints。最终你会用完唯一的哈希码,即使使用了完美的哈希算法。 -
你和你的双胞胎兄弟同一天生日,却是不同的人......
标签: java hashtable equals hashcode