There is some code in java.util.HashMap.TreeNode#putTreeVal()
, just like below:
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0) {
if (!searched) {
TreeNode<K,V> q, ch;
searched = true;
if (((ch = p.left) != null &&
(q = ch.find(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.find(h, k, kc)) != null))
return q;
}
dir = tieBreakOrder(k, pk);
}
There have two situation: h
less than ph
, h
greater than ph
.
Usually, the code (pk = p.key) == k || (k != null && k.equals(pk))
means h
equals to ph
, but i don't know why there still else if after that.
What is the situation when two objects's hashCode
is equals to each other, but ==
and euqlas()
will get false?
When Object's class override equals()
method will cause this situation? But i used heard that override equals()
must override hashCode()
too, so this question wouldn't happen to.
I hope some people could tell me which situation will cause the third else if
.