//Node是单向链表,实现了Map.Entry接口 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; } // getter and setter ... toString ... 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; 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; } }
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> { TreeNode<K,V> parent; // red-black tree links TreeNode<K,V> left; TreeNode<K,V> right; TreeNode<K,V> prev; // needed to unlink next upon deletion boolean red; TreeNode(int hash, K key, V val, Node<K,V> next) { super(hash, key, val, next); } /** * Returns root of tree containing this node. */ final TreeNode<K,V> root() { for (TreeNode<K,V> r = this, p;;) { if ((p = r.parent) == null) return r; r = p; } }
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
/** * 默认初始容量16(必须是2的幂次方) */ static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; /** * 最大容量,2的30次方 */ static final int MAXIMUM_CAPACITY = 1 << 30; /** * 默认加载因子,用来计算threshold */ static final float DEFAULT_LOAD_FACTOR = 0.75f; /** * 链表转成树的阈值,当桶中链表长度大于8时转成树 threshold = capacity * loadFactor */ static final int TREEIFY_THRESHOLD = 8; /** * 进行resize操作时,若桶中数量少于6则从树转成链表 */ static final int UNTREEIFY_THRESHOLD = 6; /** * 桶中结构转化为红黑树对应的table的最小大小 当需要将解决 hash 冲突的链表转变为红黑树时, 需要判断下此时数组容量, 若是由于数组容量太小(小于 MIN_TREEIFY_CAPACITY ) 导致的 hash 冲突太多,则不进行链表转变为红黑树操作, 转为利用 resize() 函数对 hashMap 扩容 */ static final int MIN_TREEIFY_CAPACITY = 64; /** 保存Node<K,V>节点的数组 该表在首次使用时初始化,并根据需要调整大小。 分配时, 长度始终是2的幂。 */ transient Node<K,V>[] table; /** * 存放具体元素的集 */ transient Set<Map.Entry<K,V>> entrySet; /** * 记录 hashMap 当前存储的元素的数量 */ transient int size; /** * 每次更改map结构的计数器 */ transient int modCount; /** * 临界值 当实际大小(容量*填充因子)超过临界值时,会进行扩容 */ int threshold; /** * 负载因子:要调整大小的下一个大小值(容量*加载因子)。 */ final float loadFactor;
/** * 传入初始容量大小,使用默认负载因子值 来初始化HashMap对象 */ public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); } /** * 默认容量和负载因子 */ public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted } /** * 传入初始容量大小和负载因子 来初始化HashMap对象 */ public HashMap(int initialCapacity, float loadFactor) { // 初始容量不能小于0,否则报错 if (initialCapacity < 0) throw new IllegalArgumentException("Illegal initial capacity: " initialCapacity); // 初始容量不能大于最大值,否则为最大值 if (initialCapacity > MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; //负载因子不能小于或等于0,不能为非数字 if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal load factor: " loadFactor); // 初始化负载因子 this.loadFactor = loadFactor; // 初始化threshold大小 this.threshold = tableSizeFor(initialCapacity); } /** * 找到大于或等于 cap 的最小2的整数次幂的数。 */ static final int tableSizeFor(int cap) { int n = cap - 1; n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n 1; }
public V get(Object key) { Node<K,V> e; return (e = getNode(hash(key), key)) == null ? null : e.value; } // 获取hash值 static final int hash(Object key) { int h; // 拿到key的hash值后与其五符号右移16位取与 // 通过这种方式,让高位数据与低位数据进行异或,以此加大低位信息的随机性,变相的让高位数据参与到计算中。 return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } 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) { // 判断桶中第一项(数组元素)相等 if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) return first; // 桶中不止一个结点 if ((e = first.next) != null) { // 是否是红黑树,是的话调用getTreeNode方法 if (first instanceof TreeNode) return ((TreeNode<K,V>)first).getTreeNode(hash, key); // 不是红黑树的话,在链表中遍历查找 do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } while ((e = e.next) != null); } } return null; }
public V put(K key, V value) { // 调用hash(key)方法来计算hash return putVal(hash(key), key, value, false, true); } final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; // 容量初始化:当table为空,则调用resize()方法来初始化容器 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; // 比较桶中第一个元素(数组中的结点)的hash值相等,key相等 if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) //如果键的值以及节点 hash 等于链表中的第一个键值对节点时,则将 e 指向该键值对 e = p; // 如果桶中的引用类型为 TreeNode,则调用红黑树的插入方法 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; } // 判断链表中结点的key值与插入的元素的key值是否相等 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } //判断要插入的键值对是否存在 HashMap 中 if (e != null) { // existing mapping for key V oldValue = e.value; // onlyIfAbsent 表示是否仅在 oldValue 为 null 的情况下更新键值对的值 if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } modCount; // 键值对数量超过阈值时,则进行扩容 if ( size > threshold) resize(); afterNodeInsertion(evict); return null; }
final Node<K,V>[] resize() { // 拿到数组桶 Node<K,V>[] oldTab = table; int oldCap = (oldTab == null) ? 0 : oldTab.length; int oldThr = threshold; int newCap, newThr = 0; // 如果数组桶的容量大与0 if (oldCap > 0) { // 如果比最大值还大,则赋值为最大值 if (oldCap >= MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return oldTab; } // 如果扩容后小于最大值 而且 旧数组桶大于初始容量16, 阈值左移1(扩大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; // 如果数组桶容量<=0 且 旧阈值 <=0 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; }
static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }