什么是对象k?

时间:2014-11-13 16:27:18

标签: java hashmap

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实现。除了这一行,我理解了一切 - 对象k;
请解释这个对象k是如何出现的?

1 个答案:

答案 0 :(得分:1)

HashMap的实现中,数据结构由一组链接的条目列表支持。这些条目有一个键和一个值。

该变量k用于在迭代链表列表时存储每个条目的键。如果发现它与您尝试插入值的键相等(引用然后值相等),那么该值将替换旧值。

相关问题