equals() 方法实际判断引用地址
public boolean equals(Object obj) {
return (this == obj);
}
hashcode() 方法和equals相同,实际判断引用地址:
这就是用hashCode()方法判断的。hashCode()是Object类的方法(equals()方法也是),所有类都间接或直接继承了Object类,因此都继承了hashCode()方法。Object类的hashCode()方法的返回值以整数形式表示一个对象的地址(未必是真正的地址,通过某种转换表示这个地址而已),即当调用HashSet的add(Object a)时,将a的hashCode值(直接说成hashCode值)与已有元素的hashCode值进行比较,如果不同则直接加进去,如果hashCode值相同则接着调用equals方法比较,如果equals方法返回真则不加,返回true则加进去。
如果不重写这两个方法,子类都将直接继承Object的两个方法,即会直接比较两个对象的地址。这在实际项目开发中意义较少的,一般情况下都会希望以内容来判断时候能加进去(即add)
HashSet->HashMap->TreeNode(红黑树)
final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
int h, K k, V v) {
Class<?> kc = null;
boolean searched = false;
TreeNode<K,V> root = (parent != null) ? root() : this;
for (TreeNode<K,V> p = root;;) {
int dir, ph; K pk;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
//equals
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);
}
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
Node<K,V> xpn = xp.next;
TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
if (dir <= 0)
xp.left = x;
else
xp.right = x;
xp.next = x;
x.parent = x.prev = xp;
if (xpn != null)
((TreeNode<K,V>)xpn).prev = x;
moveRootToFront(tab, balanceInsertion(root, x));
return null;
}
}
}