Java Object hashCode()的小点

Tip 1:

官方文档中的一句话:“Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.”
目前已知protobuf的相关对象会出现不同JVM实例的hashCode不一致。

Tip 2:

"We want a different system".hashCode();
"\0We want a different system".hashCode();

上面两个字符串hashCode一样,但是不equals。如果出现恶意攻击会导致HashMap哈希冲突,使得某个Entry后的链表会变长,本来是O(1)的HashMap会变成O(n)的链表。

你可能感兴趣的:(Java)