hashMap 根据已有知识知道的

时间:2023-12-04 22:36:38
public V put(K key, V value) {
//假如table为空
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
//假如键为空
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
//遍历table[i]
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;
}