您好,登錄后才能下訂單哦!
/*
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;//16 默認初始化容量為16
static final int MAXIMUM_CAPACITY = 1 << 30;//最大容量
static final float DEFAULT_LOAD_FACTOR = 0.75f;//默認負載因子
static final int TREEIFY_THRESHOLD = 8;//樹化閥值
static final int UNTREEIFY_THRESHOLD = 6;//非樹化閥值
static final int MIN_TREEIFY_CAPACITY = 64;//最小樹化容量
transient Node<K,V>[] table;//存儲元素數組
transient Set<Map.Entry<K,V>> entrySet;
transient int size;//元素個數
transient int modCount;//修改次數
int threshold;//閥值
final float loadFactor;//負載因子
/*
Node內部類,元素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);
}
//默認構造函數
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // 負載因子默認0.75
}
//put方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//put方法調用: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;
//存儲元素數組為空并且長度為0
if ((tab = table) == null || (n = tab.length) == 0)
//進行擴容 tab為擴容后的數組 n為擴容后的長度
n = (tab = resize()).length;
//m%n和m&n 當n滿足2^k,是相等的
//p為table存在的同一索引的元素
//p為空,進行新增
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
//p不為空,進行鏈表添加
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實例 紅黑樹節點
else if (p instanceof TreeNode)
//p putTreeVal為TreeNode 返回
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
// p其他
//單鏈表循環處理
for (int binCount = 0; ; ++binCount) {
//單鏈表只有一個頭節點,直接新建一個節點
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//當大于等于樹化閥值8,將單鏈表轉紅黑樹
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;//更改操作次數
//大于臨界值
if (++size > threshold)
//擴容2倍,重新調整數組
resize();
afterNodeInsertion(evict);
return null;
}
//putVal方法調起:當存儲元素數組為空并且長度為0
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;//長度
int oldThr = threshold;//臨界值
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {//數組長度達到最大值,則不變
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold 擴容2倍
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY; //默認
newThr = (int)(DEFAULT_LOAD_FACTOR DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {//臨界值還為0,則設置臨界值
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;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。