hashcode主要是给HashSet、HashMap等使用的。
如HashMap存储时,会根据key的hashcode计算得到一个index,存放在Entry[index]中。
如果hashcode相同,但equals方法不相同时,在Entry[index]中实际存放的是一个链表。
由此可以看出,如果hashcode重复较多,equals方法不相同时,效率较低
如果equals相同,但hashcode不相同,则会存入重复的对象。
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
//==或者equals相同的情况
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
http://www.diybl.com/course/3_program/java/javajs/20081123/152245.html
1032

被折叠的 条评论
为什么被折叠?



