java.util.HashMap
实现了 put 方法,该方法内部包含 以下代码:
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
在上面的代码中,为什么没有首先进行引用检查(因为具有相同引用的两个对象将具有相同的哈希和相等性)?例如这样:
if ((k = e.key) == key) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
} else if ( compare hash and equals) {
// do something again with the value
}
这样不是可以省去一次比较吗?