当 hashcode() returns 为零时对 Collection 实现有什么影响

what is the impact over Collection implementations when hashcode() returns zero

好的,仅供参考,如果 object's hashcode 方法始终对 类 等 hashmaphashset 等集合实现有何意义returns 0demoClass 中。我知道它与 hashmap 的 putForNullKey 或 Collection 实现的其他 类 有关,但不太了解细节。我知道空对象的 hascode 是 0 所以它有 0 hashcode 的特定方法。

@Override
public int hashCode() {
return 0;
} 

这将使 HashMapHashSet 和其他依赖 hashCode 的集合效率非常低,因为所有 elements/entries 都将添加到同一个容器中。

get()containsKey()contains() 等方法将采用 O(n) 而不是 O(1)

顺便说一句,答案并不特定于 0 hashCode。任何常量 hashCode 的行为都完全相同。