我在某个地方读到过,在HashMap的背景下:
hashCode() allows sorting objects by their hash values, and then the Object#equals method only needs to be invoked when objects share the same hash value.
个
这是否意味着当最终调用equals()方法时,就已经比较了hashCode并发现它们一定相等?这种情况有什么例外吗?
问题的第二部分:在equals()方法中判断两个对象的hashCode()有意义吗?
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof InternalObjectSerialized)) {
return false;
}
InternalObjectSerialized<?> inputObj = (InternalObjectSerialized<?>) obj;
if (!(Arrays.equals(this.bytes, inputObj.bytes))) {
return false;
}
// Does the following line make any sense ??
return this.hashCode() == inputObj.hashCode();
}