1.HashMap
HashMap实际上是一个节点数组table,只不过数组的索引是通过hash算法计算出来的。每一个节点都包含一个key,一个value,key的hash值,以及下一个节点next。jdk1.7中解决碰撞问题是通过链表实现的,所以会存next节点。图1是map中节点的定义。
- 1.1 put(K key, V value)
- a) 先通过
hash(key)
求出key的hash值,源码见图2和图3
- a) 先通过
- b) 判断当前map是否初始化,已初始化则跳过此步骤,没有初始化则调用
resize()方法初始化map
,此步骤中需要注意的几个点,全局变量和常量:threshold(变量,表示下次扩容时的map长度),loadFactor (常量,值为0.75,threshold=map.size()*loadFactor)
。当map.size() == threshold
时就会调用resize()方法进行扩容,扩容一倍。源码见图4,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;
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
}
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] = 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;
}
- c) 通过
(table.length - 1) & hash
计算value的索引,table.length通过位运算计算,始终为2的n次方,索引通过&计算得出,始终在数组范围内。当此索引的节点为null时,直接实例化一个节点存进去,如果此索引的节点值不为null,则会对比此节点key的值,key值相同则覆盖,否则遍历此索引上的节点链表,在链表的尾部存入新的节点。源码见图5。
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) //计算当前节点的索引,并判断节点是否为null,为null则直接创建新的节点
tab[i] = newNode(hash, key, value, null);
else { // 此key的索引位置已有节点
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 { //key值不重复,存链表
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;
// 当size > threshold时,重置map的size
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
- 1.2 get(Object key)
- a) 先根据
hash(key)
算法求key的hash值 - b) 然后通过
(table.length - 1) & hash
求出value所处的位置 - c) 通过比对该位置的key值是否相等来获取value值,如果不等且节点不为空,jdk1.7中走链表进行匹配查找value。
- a) 先根据