hashMap 根据已有知识知道的

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

 

posted @ 2018-09-29 11:59  fleam  阅读(125)  评论(0编辑  收藏  举报