diff --git a/util/HashMap.java b/util/HashMap.java index e959d8261ada53d55d75c1aab97a00d36c232aa1..47935cd75b7eb1904dc53f72ff88d2d28ef19d30 100644 --- a/util/HashMap.java +++ b/util/HashMap.java @@ -229,36 +229,35 @@ public class HashMap extends AbstractMap */ /** - * The default initial capacity - MUST be a power of two. + * 默认的初始容量-必须是二的幂。 */ - static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 + static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; /** - * The maximum capacity, used if a higher value is implicitly specified - * by either of the constructors with arguments. - * MUST be a power of two <= 1<<30. + * HashMap的容量极限,为2的30次幂; */ static final int MAXIMUM_CAPACITY = 1 << 30; /** - * The load factor used when none specified in constructor. + * 负载因子,来决定容量的大小以及是否扩容 + *

+ * 负载因子在初始化Map的时候被赋值,要么被用户传的参数赋值,要么被默认的0.75f给赋值; + *

+ * 默认的负载因子我们之前说过:0.75f,当然也可以自己设置,但 0.75 是最均衡的设置,没有特殊要求不要修改该值, + *

+ * 负载因子过小,说明Node元素比较少,理论上能减少 hash 冲突, + *

+ * 负载因子过大,可以节约空间。 */ static final float DEFAULT_LOAD_FACTOR = 0.75f; /** - * The bin count threshold for using a tree rather than list for a - * bin. Bins are converted to trees when adding an element to a - * bin with at least this many nodes. The value must be greater - * than 2 and should be at least 8 to mesh with assumptions in - * tree removal about conversion back to plain bins upon - * shrinkage. + * 链表变为红黑树时链表的长度阈值 */ 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; @@ -348,7 +347,7 @@ public class HashMap extends AbstractMap */ static final int hash(Object key) { int h; - return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); + return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);//扰动函数计算hash值 } /** @@ -388,7 +387,11 @@ public class HashMap extends AbstractMap } /** - * Returns a power of two size for the given target capacity. + * 返回给定目标容量的二次方大小。用于查找距离容量initialCapacity最近的2次幂值 + * 任何一个int 数字,都能找到离他最近的 2 的幂次方数字(且比他大)。 + * cap是5~7,那么该方法的返回结果就是8; + * cap是9~15,那么该方法的返回结果就是16; + * cap是17~31,那么该方法的返回结果就是32; */ static final int tableSizeFor(int cap) { int n = cap - 1; @@ -403,61 +406,40 @@ public class HashMap extends AbstractMap /* ---------------- Fields -------------- */ /** - * The table, initialized on first use, and resized as - * necessary. When allocated, length is always a power of two. - * (We also tolerate length zero in some operations to allow - * bootstrapping mechanics that are currently not needed.) + * 该表在首次使用时初始化,并根据需要调整大小。当被分配时,长度总是二的幂。 + * 可以将这个table理解为是一个entry数组;每一个Node即entry,本质都是一个单向链表 */ transient Node[] table; /** - * Holds cached entrySet(). Note that AbstractMap fields are used - * for keySet() and values(). + * 保留缓存的entrySet */ transient Set> entrySet; /** - * The number of key-value mappings contained in this map. + * HashMap的实际元素数量 */ transient int size; /** - * The number of times this HashMap has been structurally modified - * Structural modifications are those that change the number of mappings in - * the HashMap or otherwise modify its internal structure (e.g., - * rehash). This field is used to make iterators on Collection-views of - * the HashMap fail-fast. (See ConcurrentModificationException). + * HashMap已在结构上修改的次数 */ transient int modCount; /** - * The next size value at which to resize (capacity * load factor). - * - * @serial + * 下一次HashMap扩容的阀值大小,如果尚未扩容,则该字段保存初始entry数组的容量,或用零表示 */ - // (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.) int threshold; /** - * The load factor for the hash table. - * - * @serial + * 存储负载因子的常量,初始化的时候将默认的负载因子赋值给它; */ final float loadFactor; /* ---------------- Public operations -------------- */ /** - * Constructs an empty HashMap with the specified initial - * capacity and load factor. - * - * @param initialCapacity the initial capacity - * @param loadFactor the load factor - * @throws IllegalArgumentException if the initial capacity is negative - * or the load factor is nonpositive + * 构造函数 初始容量和负载因子 */ public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) @@ -635,7 +617,7 @@ public class HashMap extends AbstractMap } /** - * Implements Map.put and related methods + * 填入值 * * @param hash hash for key * @param key the key @@ -649,23 +631,23 @@ public class HashMap extends AbstractMap Node[] tab; Node p; int n, i; - if ((tab = table) == null || (n = tab.length) == 0) - n = (tab = resize()).length; - if ((p = tab[i = (n - 1) & hash]) == null) + 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 e; K k; if (p.hash == hash && - ((k = p.key) == key || (key != null && key.equals(k)))) + ((k = p.key) == key || (key != null && key.equals(k))))//替换value e = p; - else if (p instanceof TreeNode) + else if (p instanceof TreeNode)//如果是红黑树 e = ((TreeNode) p).putTreeVal(this, tab, hash, key, value); else { - for (int binCount = 0; ; ++binCount) { + 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 + if (binCount >= TREEIFY_THRESHOLD - 1) // 链表长度大于等于8,则转为红黑树 treeifyBin(tab, hash); break; } @@ -691,74 +673,115 @@ public class HashMap extends AbstractMap } /** - * Initializes or doubles table size. If null, allocates in - * accord with initial capacity target held in field threshold. - * Otherwise, because we are using power-of-two expansion, the - * elements from each bin must either stay at same index, or move - * with a power of two offset in the new table. + * 扩容 + * ①用来初始化一个Map, + * ②或者对Map容量进行扩容 + * 初始化或加倍表大小。如果为空,则根据字段阈值中保持的初始容量目标进行分配。 + * 否则,因为我们使用的是二次幂展开,所以每个bin中的元素必须保持在同一索引处, + * 或者在新表中以二次幂偏移量移动。 + *

+ *

+ * 第一部分: + * 1.初始化了newThr + * 2.初始化了newCap + * 3.创建newTab + * 第二部分:将原Node数组中的Node元素,迁移到扩容后的新的Node数组中 + * 1.将链表分成两个不同的部分,可以使得数据更加的分散,使得链表的长度变短 * * @return the table */ final Node[] resize() { + //扩容前,原Node数组 Node[] oldTab = table; + //扩容前,原Node数组的长度 int oldCap = (oldTab == null) ? 0 : oldTab.length; + //扩容前,原扩容阀值 int oldThr = threshold; + //扩容后,新扩容阀值,扩容后,新Node数组的长度 int newCap, newThr = 0; + //如果原Node数组的长度>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 + oldCap >= DEFAULT_INITIAL_CAPACITY) //新cap是旧cap的2倍,并且旧cap大于16 + //阈值翻倍 + newThr = oldThr << 1; + } else if (oldThr > 0) + newCap = oldThr;//旧阈值大于0 + else {//如果初始化的时候用户什么都没传,那么此时oldCap和oldThr都==0 + //将默认容量大小DEFAULT_INITIAL_CAPACITY(16)赋给newCap(新Node数组的长度) newCap = DEFAULT_INITIAL_CAPACITY; + //将(默认负载因子)0.75f * (默认的容量大小)DEFAULT_INITIAL_CAPACITY的计算结果,赋值给newThr(新的扩容阀值) newThr = (int) (DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } + /** + * 如果初始化的时候用户传入了容量参数和负载因子或者只传入了容量参数, + * 那么意味着oldCap==0、oldThr>0,那么上边的else里边是不会进入的,那么此时newThr(新的扩容阀值)仍然==0 + */ if (newThr == 0) { + //将用户传入的容量参数 * 用户传入的负载因子loadFactor(也可能是默认的负载因子) float ft = (float) newCap * loadFactor; + //将计算结果赋值给newThr(新的扩容阀值) newThr = (newCap < MAXIMUM_CAPACITY && ft < (float) MAXIMUM_CAPACITY ? (int) ft : Integer.MAX_VALUE); } + //初始化好了扩容阀值 threshold = newThr; @SuppressWarnings({"rawtypes", "unchecked"}) - Node[] newTab = (Node[]) new Node[newCap]; + //所以新建一个entry数组,容量为newCap,即创建出扩容后新的Node数组 + Node[] newTab = (Node[]) new Node[newCap];//扩容后,新Node数组 table = newTab; - if (oldTab != null) { - for (int j = 0; j < oldCap; ++j) { + if (oldTab != null) {//如果原Node数组不为空 + for (int j = 0; j < oldCap; ++j) {//遍历原Node数组 Node e; if ((e = oldTab[j]) != null) { oldTab[j] = null; - if (e.next == null) + if (e.next == null) {//如果该Node元素的next==null,则说明该Node元素后边既没有链表又没有红黑树 + //则将该Node元素直接存于新Node数组的指定位置 newTab[e.hash & (newCap - 1)] = e; - else if (e instanceof TreeNode) + + } else if (e instanceof TreeNode) {//如果该Node元素后边跟着的是一个红黑树结构 + //在新的Node数组中,将该红黑树进行拆分 + //如果拆分后的子树过小(子树的节点小于等于6个),则取消树化,即将其转为链表结构 ((TreeNode) e).split(this, newTab, j, oldCap); - else { // preserve order - Node loHead = null, loTail = null; - Node hiHead = null, hiTail = null; + } else {//如果是链表的情况下,则进行下面的链表数据转移的操作 + Node loHead = null, loTail = null;//低头尾 + Node hiHead = null, hiTail = null;//高头尾 Node next; do { + //对链表进行遍历,把链表中的节点分成两个类别,一类是需要更换数组下标的,另一类是不需要的 next = e.next; + //如果e.hash&oldCap进行与运算,算出的结果是为0,即说明该Node节点所对应的数组下标不需要改变 if ((e.hash & oldCap) == 0) { - if (loTail == null) + //如果loTail为null,说明该链表没有头节点 + if (loTail == null) { + //所以把头节点指向该节点 loHead = e; - else + } else { + //如果该链表有头结点,则把遍历出来的节点放在该链表的尾部 loTail.next = e; + } loTail = e; - } else { - if (hiTail == null) + } else {//如果e.hash&oldCap进行与运算,算出的结果不为0,则更新该Node节点所对应的数组下标 + if (hiTail == null) { + hiHead = e; - else + } else { + hiTail.next = e; + } hiTail = e; } } while ((e = next) != null); + //该Node节点所对应的数组下标不需要改变,直接把数组下标对应的节点指向新Node数组下标位置链表的头节点 if (loTail != null) { loTail.next = null; newTab[j] = loHead; } + //该Node节点所对应的数组下标需要改变,重新计算出所对应的数组下标值,然后指向新Node数组下标位置链表的头节点 if (hiTail != null) { hiTail.next = null; newTab[j + oldCap] = hiHead; @@ -2284,6 +2307,7 @@ public class HashMap extends AbstractMap } } if (hiHead != null) { + //如果拆分后的子树过小(子树的节点小于等于6个),则取消树化,即将其转为链表结构 if (hc <= UNTREEIFY_THRESHOLD) tab[index + bit] = hiHead.untreeify(map); else {