在map集合中,hashcode的相同的话,怎么区分,职场小白 求各位大神帮帮忙
1.如果hashcode相同 key值不相同 则该数组下面的链表下面加一个entry
2.如果hashcode相同 key值相同 则进行更新
hashmap是数组和链表结构 数组通过hashcode进行定位
hashMap 的put方法源码,如果hashcode相同则覆盖
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
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;
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;
}
Map中解决冲突的方法是bucket的方式,所有相同hashCode的记录存在一个链表中的。