阅读源码,从hashMap做起,如有错误问题,欢迎大牛们指教
一、基本参数
1.1 初始常量
//默认初始化容量,就是数组的大小 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 //数组最大容量 static final int MAXIMUM_CAPACITY = 1 << 30; //默认负载因子 static final float DEFAULT_LOAD_FACTOR = 0.75f;//链表转红黑树的阈值
static final int TREEIFY_THRESHOLD = 8;/** * The bin count threshold for untreeifying a (split) bin during a * resize operation. Should be less than TREEIFY_THRESHOLD, and at * most 6 to mesh with shrinkage detection under removal. */static final int UNTREEIFY_THRESHOLD = 6;/** * The smallest table capacity for which bins may be treeified. * (Otherwise the table is resized if too many nodes in a bin.) * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts * between resizing and treeification thresholds. */static final int MIN_TREEIFY_CAPACITY = 64;
//红黑树转链表的阈值
1.2 基本数据
// node节点的数组表table
transient Node<K,V>[] table;
/**
* Holds cached entrySet(). Note that AbstractMap fields are used
* for keySet() and values().
*/
transient Set<Map.Entry<K,V>> entrySet;
//HashMap中实际存在的键值对数量
transient int size;
// 记录HashMap内部结构发生变化的次数,主要用于迭代的快速失败。
// 内部结构发生变化指的是结构发生变化,例如put新键值对,但是某个key对应的value值被覆盖不属于结构变化。
transient int modCount;
// (The javadoc description is true upon serialization.
// Additionally, if the table array has not been allocated, this
// field holds the initial array capacity, or zero signifying
// DEFAULT_INITIAL_CAPACITY.)
// threshold是HashMap所能容纳的最大数据量的Node(键值对)个数。
// threshold = length * Load factor
// 超过这threshold就扩容
int threshold;// 负载因子final float loadFactor;
二、基本结构
Hashmap的基础是Node,Node是HashMap的内部类,每一个节点都是个Node,HashMap就是个Node的数组Node[],而Node实现了Map的Entry接口,Map的Entry可以理解为一组键值对
static class Node<K,V> implements Map.Entry<K,V> { final int hash; // hash值,final,只能赋值一次,不可改变 final K key; // node的key值,final,只能赋值一次,不可改变 V value; // node的value值 Node<K,V> next; // 出现hash冲突后,链表指向的下一个 Node(int hash, K key, V value, Node<K,V> next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } public final K getKey() { return key; } public final V getValue() { return value; } public final String toString() { return key + "=" + value; } public final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); } public final V setValue(V newValue) { V oldValue = value; value = newValue; return oldValue; } public final boolean equals(Object o) { if (o == this) return true;//instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例,
//通过返回一个boolean来指出该对象是否是这个特定类或者是它的子类的一个实例
if (o instanceof Map.Entry) {Map.Entry<?,?> e = (Map.Entry<?,?>)o; if (Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue())) return true; } return false; } }
三、方法
3.1 hash运算
// hash操作,做高位运算,即高16位与低16位做异或操作 static final int hash(Object key) { int h; // 1. 取hashCode,2. 高位运算, 3. 取模运算(此处没有,在putVal里面的(n-1)&hash ) return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);// >>>无符号右移,高位补0 }
3.2 get方法
public V get(Object key) { Node<K,V> e; return (e = getNode(hash(key), key)) == null ? null : e.value; } /** * Implements Map.get and related methods * * @param hash key值得hash* @param key key值* @return the node, or null if none */final Node<K,V> getNode(int hash, Object key) {// tab为node数组,first为node链表的首个node,e遍历node用,n为当前tab长度,i是index
Node<K,V>[] tab; Node<K,V> first, e; int n; K k; // tab不为空且有长度(有值),first也不为空 if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) {// 传入hash==first的hash值,则返回first
if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) return first; // first节点的next不为空,则遍历链表 if ((e = first.next) != null) {// first节点是TreeNode节点,则get红黑树node
if (first instanceof TreeNode) return ((TreeNode<K,V>)first).getTreeNode(hash, key); do {// key相等的则找到了,即可返回
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } while ((e = e.next) != null); } } return null;}
3.3 put方法
public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } /** * Implements Map.put and related methods * * @param hash key值的hash * @param key key值 * @param value value值 * @param onlyIfAbsent 如果true, 不改变已存在value * @param evict 如果false, table表进入创建模式. * @return previous value, or null if none */ final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {put的整体流程如下:// tab为node数组,p为当前node,n为当前tab长度,i是index
Node<K,V>[] tab; Node<K,V> p; int n, i; // tab为空,则resize 【第1步】 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; // tab的i不存在node,则直接new一个node 【第2步】 if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); // tab的i已存在node
else { Node<K,V> e; K k; // p的hash==传入hash,p的key==传入key,则直接覆盖 【第3步】 if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; // p节点是TreeNode,则插入树的值 【第4步】 else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); else {// 遍历链表 【第5步】
for (int binCount = 0; ; ++binCount) {// next为空,则在尾部插入new Node
if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); // 链表中节点个数大于threshold则转换为红黑树 【第5步】
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); break; }// key已存在则覆盖 【第5步】
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; // 超过最大容量则resize扩容 【第6步】 if (++size > threshold) resize(); afterNodeInsertion(evict); return null;}
1. 判断键值对数组table[i]是否为空或null,否则进行resize扩容操作
2. 根据键值key计算hash值得到插入数组的索引i,如果table[i]==null,直接新建节点添加,转第6步,如果table[i]不为空(hash碰撞),则转第3步
3. 判断table[i]首个元素是否和key一样,相同则直接覆盖value,否则转第4步
4. 判断table[i]是否为TreeNode(红黑树),如果是则在树中插入键值对
5. 遍历table[i],将Node插入尾部,判断链表长度是否大于8,大于8则链表转换为红黑树,若发现key已存在,则覆盖
6. 插入成功后,判断实际存在的键值对数量size,若超过最大容量threshold,则进行扩容
3.4 resize方法
final Node<K,V>[] resize() {// 原node数组table
Node<K,V>[] oldTab = table;// 原node数组table的容量
int oldCap = (oldTab == null) ? 0 : oldTab.length;// 原threshold
int oldThr = threshold; int newCap, newThr = 0;
if (oldCap > 0) {// oldCap大于最大容量,threshold阈值设为无穷大,并不再扩容
if (oldCap >= MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return oldTab; }// newCap扩容为oldCap的2倍, // 如果newCap小于最大容量,且oldCap大于默认初始容量,newThr阈值也扩大2倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) newThr = oldThr << 1; // double threshold }// oldCap小于等于0,但oldThr不为空,根据threshold初始化table
else if (oldThr > 0) // initial capacity was placed in threshold newCap = oldThr;// oldCap、oldThr都为空,更改newCap、newThr
else { // zero initial threshold signifies using defaults newCap = DEFAULT_INITIAL_CAPACITY; newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); }// newThr为0,newCap*loadFactor和newCap都小于最大容量,if (newThr == 0) { float ft = (float)newCap * loadFactor; newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE); }// 则newThr设为无穷大,否则设为newCap*loadFactor
// 新threshold,以newCap新建node[]的table数组
threshold = newThr; @SuppressWarnings({"rawtypes","unchecked"}) Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; table = newTab; if (oldTab != null) {// 遍历oldTab
for (int j = 0; j < oldCap; ++j) { Node<K,V> e; if ((e = oldTab[j]) != null) {// 释放oldTab[j]
oldTab[j] = null;// node的next为空(无后续链表),则直接set为新node的头结点
if (e.next == null) newTab[e.hash & (newCap - 1)] = e;// 如果是TreeNode,
else if (e instanceof TreeNode) ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order JDK1.8链表优化重新hash// 原索引位置的头Node、尾Node
Node<K,V> loHead = null, loTail = null;Node<K,V> hiHead = null, hiTail = null; Node<K,V> next; do {// 原索引+oldCap位置的头Node、尾Node
next = e.next;// node的rehash和oldCap按位与,为0则保持原索引
if ((e.hash & oldCap) == 0) { if (loTail == null) loHead = e; else loTail.next = e; loTail = e; }// 为1则索引设为原索引+oldCap
else { if (hiTail == null) hiHead = e; else hiTail.next = e; hiTail = e; } } while ((e = next) != null);// 原索引放入newTab
if (loTail != null) { loTail.next = null; newTab[j] = loHead; }if (hiTail != null) { hiTail.next = null; newTab[j + oldCap] = hiHead; } } } } } return newTab;}// 原索引+oldCap放入newTab
JDK1.7的扩容如下
void resize(int newCapacity) { //传入新的容量
Entry[] oldTable = table; //记录扩容前的Entry数组
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) { //扩容前的数组大小如果已经达到最大容量了
threshold = Integer.MAX_VALUE; //修改阈值为无穷大,这样以后就不会扩容了
return;
}
Entry[] newTable = new Entry[newCapacity]; //初始化一个新的Entry数组
transfer(newTable); //将数据转移到新的Entry数组里
table = newTable; //HashMap的table属性引用新的Entry数组
threshold = (int)(newCapacity * loadFactor);//修改阈值
}
void transfer(Entry[] newTable) {
Entry[] src = table; // 旧的Entry数组
int newCapacity = newTable.length;
for (int j = 0; j < src.length; j++) { // 遍历旧Entry数组
Entry<K,V> e = src[j];
if (e != null) {
src[j] = null; // 释放旧Entry数组的对象引用
do {
Entry<K,V> next = e.next;
int i = indexFor(e.hash, newCapacity); //重新计算每个元素在数组中的位置
e.next = newTable[i]; // 标记
newTable[i] = e; // 将元素放在数组上
e = next; // 访问下一个Entry链上的元素
} while (e != null);
}
}
}
JDK1.7和JDK1.8的resize主要在重新hash上有区别,1.7resize之后需将所有node的链表都进行rehash,而1.8在元素在重新计算hash之后,因为n变为2倍,那么n-1的mask范围在高位多1bit,因此只需要看看原来的hash值新增的那个bit是1还是0就好了,是0的话索引没变,是1的话索引变成“原索引+oldCap”
3.5 treeifyBin链表转红黑树
final void treeifyBin(Node<K,V>[] tab, int hash) { int n, index; Node<K,V> e; // tab为空,或tab长度为达到最小红黑树容量 if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) resize(); // 对应hash位置的node不为空,则开始转换红黑树 else if ((e = tab[index = (n - 1) & hash]) != null) {// hd为转化后TreeNode,tl为上一个node
TreeNode<K,V> hd = null, tl = null; do {// 将当前节点转换为TreeNode
TreeNode<K,V> p = replacementTreeNode(e, null); // tl为空,则说明为头结点
if (tl == null) hd = p; else { p.prev = tl; tl.next = p; } tl = p; } while ((e = e.next) != null); // 若hd不为空,将index位上的node,赋值为红黑树 if ((tab[index] = hd) != null) hd.treeify(tab); }}// For treeifyBin TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) { return new TreeNode<>(p.hash, p.key, p.value, next); }