HashMap put(K key, V value)解析

1、HashMap 类结构

/**
     * The table, resized as necessary. Length MUST Always be a power of two.
     */
    transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;		//用来存储Entry

    final float loadFactor;              //负载因子 当数组容量的占用比例超过负载因子时,数组就会扩容。

    static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;						//用来存储Key
        V value;							//用来存储Value
        Entry<K,V> next;					//用来指向:相同hashCode的Key,但是不同Key对象
        int hash;							//Key对象的hashCode值

        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }
    }
}

2、put(K key, V value)代码分析

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;
    }

首先通过int hash = hash(key);计算Key对象的hashCode值。注:hashCode()的默认行为是对在heap上的对象产生独特的值。

然后通过int i = indexFor(hash, table.length);计算该hashCode值存放在entry数组的哪个位置。

然后通过for (Entry<K,V> e = table[i]; e != null; e = e.next)判断该数组位置上的所有Entry对象
    (1)若hashCode相同,并且两个Key对象equals,那么就覆盖该key关联的value对象
    (2)若hashCode相同,但两个Key对象not equals,那么就执行addEntry(hash, key, value, i);

最后:

void addEntry(int hash, K key, V value, int bucketIndex) {
	Entry<K,V> e = table[bucketIndex];
	table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
}

(1)首先拿到该数组位置的Entry值e
(2)然后将该entry值e传递给新要添加的Entry值,并使next指向该entry值e,代码如上面的Entry构造方法

Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值