HashMap中的域
(1) transient Node<K,V>[] table;//存储node的数组
(2)threshold:
(3)static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 默认map中table数组的大小
(3)static final float DEFAULT_LOAD_FACTOR = 0.75f;//负载因子,当map中的数量大于DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR时进行resize操作
(4)transient int size;//The number of key-value mappings contained in this map. map中key-value的总数,每次执行put操作后size会加1
,
1、HashMap的put操作:
(1)计算hash值
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);//取key的hashCode,确定哈希数组中索引的位置 return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
(2)调用put方法
- 如果table是空的,resizetable
- 如果tab[i]为空,在tab[i]位置创建节点
- 如果tab[i]位置不为空,并且tab[i]位置上元素的哈希值和传入元素的哈希值相等,覆盖这个位置上的元素
- 如果tab[i]位置不为空,并且tab[i]的位置是TreeNode执行treeNode的插入
- 遍历table[i],判断链表长度是否大于8,大于8的话执行treeifyBin(转为红黑树),否则进行链表的插入操作;遍历过程中若发现key已经存在直接覆盖value
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab =
resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
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)
resize();
afterNodeInsertion(evict);
return null;
}
2、HashMap的扩容操作
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) { //原来map中的容量已经是最大容量则不进行扩容操作
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY) //将新的容量设置为旧的容量的2倍,这个2倍的容量小于最大容量并且旧的容量大于DEFAULT_INITIAL_CAPACITY=1 << 4 (16)
newThr = oldThr << 1; // double threshold 将新的阈值设置为旧的2倍 //此时新的容量和阈值都是原来的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) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) { //oldTab[j])中的值赋给e
oldTab[j] = null; //清空原table中的值
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e; //重新计算元素e在新的table中的位置,并把e放到新的位置上
else if (e instanceof TreeNode) //如果e已经是一个红黑树了
((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;
}
}
3、get方法
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value; //取key的hash再调用getNode方法
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) { //在table中找到了当前hash值对应的node
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k)))) //如果找到的第一个node的哈希值和要定位的哈希值相等,则返回
return first;
if ((e = first.next) != null) { //否则遍历first节点
if (first instanceof TreeNode) //如果first节点已经到了红黑树上,去红黑树上寻找
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) //如果没到红黑树上,则一直遍历到链表结束,找到与给定的key相等额node返回
return e;
} while ((e = e.next) != null);
}
}
return null;
}
4、hashMap中的size;
每put一个元素的时候put成功了就会在seize上加1