Hashmap作为常用的集合之一,必须深入的了解和学习,同时也是面试的经常考点之一。
Hashmap在JDK1.8以后采用数组+链表+红黑树的结构来存储数据,首先介绍下Hashmap的概念:
1.参数概念
默认初始化容量 16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
最大容量 2的30次方
static final int MAXIMUM_CAPACITY = 1 << 30;
默认的负载因子 0.75
static final float DEFAULT_LOAD_FACTOR = 0.75f;
当某个桶的数量大于8时,会转化为红黑树。
static final int TREEIFY_THRESHOLD = 8;
当某个桶节点数量小于6时,会转换为链表,前提是它当前是红黑树结构。
static final int UNTREEIFY_THRESHOLD = 6;
当整个hashMap中元素数量大于64时,也会进行转为红黑树结构。
static final int MIN_TREEIFY_CAPACITY = 64;
2.基本概念
Node<K,V>[] tab; Node<K,V> 数组
Node<K,V> node类
static class Node<K,V> implements Map.Entry<K,V> {
final int hash; //hash值
final K key; //map的key值
V value; //map的value值
Node<K,V> next; //下一个NODE
Node(int hash, K key, V value, Node<K,V> next) { 构造函数初始化
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
3.现在对hashmap的主要方法进行分析:
put方法分析
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)
n = (tab = resize()).length;
计算index,如果不存在node就在该点上创建node
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
如果key值一样,直接覆盖该值
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);
如果链表的值大于8,就转为红黑树
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
key值相等就覆盖
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;
}
resize方法分析:
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold; //最大容量(临界值)
int newCap, newThr = 0;
oldCap意思是数组存在元素时
if (oldCap > 0) {
当数组大于最大的容量时,返回它本身。
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
当它扩容2倍后,新的数组大小小于最大容量时,同时旧的数组大小大于默认数组大小。
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
当数组等于0时,同时临界值大于0时,临界值赋值给新的数组
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);
}
如果临界值等于0,初始化临界值
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] = 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;
}