16);} //默認(rèn)構(gòu)造函數(shù) public HashMap() {this.loadFactor = DEFAULT_LOAD_FACTOR; // 負(fù)載因子默認(rèn)..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

HashMap源碼解析

發(fā)布時(shí)間:2020-07-20 22:45:16 來源:網(wǎng)絡(luò) 閱讀:184 作者:mufeng07 欄目:編程語言

/*

*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;//16 默認(rèn)初始化容量為16
static final int MAXIMUM_CAPACITY = 1 << 30;//最大容量
static final float DEFAULT_LOAD_FACTOR = 0.75f;//默認(rèn)負(fù)載因子
static final int TREEIFY_THRESHOLD = 8;//樹化閥值
static final int UNTREEIFY_THRESHOLD = 6;//非樹化閥值
static final int MIN_TREEIFY_CAPACITY = 64;//最小樹化容量

transient Node<K,V>[] table;//存儲(chǔ)元素?cái)?shù)組
transient Set<Map.Entry<K,V>> entrySet;
transient int size;//元素個(gè)數(shù)
transient int modCount;//修改次數(shù)
int threshold;//閥值
final float loadFactor;//負(fù)載因子
/*
Node內(nèi)部類,元素key-value
*/
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;

    Node(int hash, K key, V value, Node<K,V> next) {
        this.hash = hash;
        this.key = key;
        this.value = value;
        this.next = next;
    }
 } 

//求hash值 大于2^16 65536的hash值有所改變
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
//默認(rèn)構(gòu)造函數(shù)
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // 負(fù)載因子默認(rèn)0.75
}
//put方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//put方法調(diào)用:hash 為key的hash值,onlyIfAbsent為fasle,evict為false
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//存儲(chǔ)元素?cái)?shù)組為空并且長(zhǎng)度為0
if ((tab = table) == null || (n = tab.length) == 0)
//進(jìn)行擴(kuò)容 tab為擴(kuò)容后的數(shù)組 n為擴(kuò)容后的長(zhǎng)度
n = (tab = resize()).length;
//m%n和m&n 當(dāng)n滿足2^k,是相等的
//p為table存在的同一索引的元素
//p為空,進(jìn)行新增
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
//p不為空,進(jìn)行鏈表添加
Node<K,V> e; K k;
//p的hash值等于put的hash值 并且p的key值等于put的key值或者key不為空并put的key值等于p的key值
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//p賦值給e
e = p;
//p為TreeNode實(shí)例 紅黑樹節(jié)點(diǎn)
else if (p instanceof TreeNode)
//p putTreeVal為TreeNode 返回
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
// p其他
//單鏈表循環(huán)處理
for (int binCount = 0; ; ++binCount) {
//單鏈表只有一個(gè)頭節(jié)點(diǎn),直接新建一個(gè)節(jié)點(diǎn)
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//當(dāng)大于等于樹化閥值8,將單鏈表轉(zhuǎn)紅黑樹
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}

                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                p = e;
            }
        }
        if (e != null) { // existing mapping for key
            V oldValue = e.value;
            if (!onlyIfAbsent || oldValue == null)
                e.value = value;
            afterNodeAccess(e);
            return oldValue;
        }
    }
    ++modCount;//更改操作次數(shù)
    //大于臨界值
    if (++size > threshold)
        //擴(kuò)容2倍,重新調(diào)整數(shù)組
        resize();
    afterNodeInsertion(evict);
    return null;
}     

//putVal方法調(diào)起:當(dāng)存儲(chǔ)元素?cái)?shù)組為空并且長(zhǎng)度為0
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;//長(zhǎng)度
int oldThr = threshold;//臨界值
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {//數(shù)組長(zhǎng)度達(dá)到最大值,則不變
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold 擴(kuò)容2倍
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY; //默認(rèn)
newThr = (int)(DEFAULT_LOAD_FACTOR DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {//臨界值還為0,則設(shè)置臨界值
float ft = (float)newCap
loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;@SuppressWarnings({"rawtypes","unchecked"})
br/>@SuppressWarnings({"rawtypes","unchecked"})
table = newTab;
//鏈表拆分
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI