public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
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;
}
我试图理解 HashMap 的实现。除了这一行之外,我理解了所有内容 - Object k;
请解释一下这个Object k是如何出现的?
最佳答案
在 HashMap
的实现中,数据结构由条目链接列表数组支持。这些条目有一个键和一个值。
该变量k
用于在迭代链表存储桶时存储每个条目的 key 。如果发现它与您尝试插入值的键相等(引用,然后值相等),则该值将替换旧值。
关于java - 什么是对象 k?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26913648/