-
map.put 方法
// map.put 方法 public V put(K key, V value) {// hash(key) 得到key的hash值 将数据尽量均匀分布 return putVal(hash(key), key, value, false, true);// put 方法实现 } // putVal 方法 final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; // 判断 tab 与 table 是否等于null if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length;// 执行初始化操作 初始大小为 16 if ((p = tab[i = (n - 1) & hash]) == null)// 判断 p 是否为null tab[i] = newNode(hash, key, value, null);// 如果为空 从根据hash计算的index开始 // put else { Node<K,V> e; K k; // 判断p的hash 与newPut的hash 是否相等 并且 key 不是 null if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p;// 成立 则将 p 赋值与 e else if (p instanceof TreeNode)// 判断 p 是否是属于 treeNode 类 // 成立则执行 红黑树 putTreeVal 方法,链表put长度 >= 8 执行此方法 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); else { // 普通的链表存储方法 for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) {// 判断 p 是否是链表存储数据的last // p.next = null 则 p 是链表的末尾Node p.next = newNode(hash, key, value, null);// 定义p.next 为 newNode if (binCount >= TREEIFY_THRESHOLD - 1) //循环是从0开始的 所以>= 7 //之后的Node 调用的是红黑树方法 treeifyBin(tab, hash); break; } // 判断 e.hash 是否与 hash 相等 相等说明是 e 节点的put 跳出循环 需要执行 // 覆盖方法 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e;// 条件都不成立 继续循环 } } if (e != null) { // 执行原存在 key.value 的覆盖方法 V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); // 用于LinkedHashMap 查其他地方的说明 待验证 return oldValue; 返回oldValue } } ++modCount;// 操作++ if (++size > threshold)// size ++ 大于扩容阀值 执行扩容方法 resize();// 扩容方法 afterNodeInsertion(evict); // 用于LinkedHashMap 查其他地方的说明 待验证 return null; } // resize 方法 执行扩容 final Node<K,V>[] resize() { Node<K,V>[] oldTab = table;// 定义oldTab // 定义oldCap 是 0 还是 oldTab.length int oldCap = (oldTab == null) ? 0 : oldTab.length; int oldThr = threshold;// 定义 oldThr 一般为0 或者是 oldTab.length * 0.75 int newCap, newThr = 0;// 定义 newCap,newThr = 0 if (oldCap > 0) {// 判断oldCap 是否 > 0 if (oldCap >= MAXIMUM_CAPACITY) {// 判断oldCap 是否大于MAXIMUM_CAPACITY threshold = Integer.MAX_VALUE;// 成立 定义 threshold = Integer.MAX_VALUE return oldTab;// 返回oldTab } // 定义 newCap = oldCap * 2 并且判断 NewCap < MAXIMUM_CAPACITY && oldCap >= 16 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) newThr = oldThr << 1; // 成立 定义 newThr 为 oldThr * 2 } else if (oldThr > 0) // oldCap = 0 && oldThr > 0 newCap = oldThr;// 定义 newCap = oldThr else { // oldCap = 0 && oldThr = 0 newCap = DEFAULT_INITIAL_CAPACITY;//设置 newCap 为 默认大小 16 // 设定 newThr = 0.75 * 16 newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } if (newThr == 0) {// 判断newThr == 0 // 从新计算 newThr 为 newCap < MAXIMUM_CAPACITY 并且 new阀值 < // MAXIMUM_CAPACITY 返回 new阀值 或者 Integer.Max_value float ft = (float)newCap * loadFactor;// newCap * 0.75(负载系数) newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE); } threshold = newThr;// 将new阀值 赋值为Threshold @SuppressWarnings({"rawtypes","unchecked"}) Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];//执行 newTab 的操作 table = newTab;// 从新定义 table if (oldTab != null) {// 判断oldTab != null for (int j = 0; j < oldCap; ++j) {// 循环执行node的迁移 Node<K,V> e; // 定义 e 判断 e != null if ((e = oldTab[j]) != null) { oldTab[j] = null;// 设置oldTab[j] = null 方便回收资源 if (e.next == null)// 判断 e 的下一个Node 是否是null // 通过hash值计算新表的索引位置, 直接将该节点放在该位置 newTab[e.hash & (newCap - 1)] = e; else if (e instanceof TreeNode)// 判断是否是 TreeNode // 执行TreeNode 的 split 方法 [方法复杂 随后补充] ((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;//存储索引位置为:原索引 // +oldCap的节点 Node<K,V> next; do {// do{}while()循环 // 定义 nextNode next = e.next; if ((e.hash & oldCap) == 0) { if (loTail == null)// 判断 loTail == null 代表是第一个Node loHead = e; else loTail.next = e;// 将e设置为loTail的下一个Node loTail = e;// 从新定义loTail } else { if (hiTail == null)// 判断 hiTail == null 代表是第一个Node hiHead = e; else hiTail.next = e;// 将e设置为hiTail的下一个Node hiTail = e;// 从新定义 hiTail } } while ((e = next) != null); // 找到末尾Node 从末尾开始赋值newTab if (loTail != null) {//定义末尾的Node loTail.next = null; newTab[j] = loHead;// 原索引位置 } if (hiTail != null) {//定义末尾的Node hiTail.next = null; newTab[j + oldCap] = hiHead;//原索引位置 + oldCap } } } } } return newTab;// 返回newTab 没有想到 hashMap.put 方法 如此复杂 }
-
map.get 方法
// map.get 方法 public V get(Object key) { Node<K,V> e; // 定义 e 根据key.hashCode 找到Node, 判断是否是 null, 返回null 或者 e.value return (e = getNode(hash(key), key)) == null ? null : e.value; } // getNode 方法 final Node<K,V> getNode(int hash, Object key) { Node<K,V>[] tab; Node<K,V> first, e; int n; K k; // 定义tab 定义n 定义first 并且都不为null if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) { if (first.hash == hash && // 判断是否是firstNode ((k = first.key) == key || (key != null && key.equals(k)))) return first;// 条件成立返回first // 定义e 是first的下一个 并且不是 null if ((e = first.next) != null) { if (first instanceof TreeNode)// 判断first 是否属于 TreeNode 类 // 执行TreeNode的getTreeNode方法 [随后补上] return ((TreeNode<K,V>)first).getTreeNode(hash, key); do {// do{}while()循环查找 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e;// 一直循环下一个 匹配返回 } while ((e = e.next) != null); } } return null;// 无匹配数据 返回null }
-
map.remove 方法
// map.remove 方法 public V remove(Object key) { Node<K,V> e; return (e = removeNode(hash(key), key, null, false, true)) == null ? null : e.value; } // removeNode 方法 final Node<K,V> removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable) { Node<K,V>[] tab; Node<K,V> p; int n, index; // 定义 tab,n,p是根据hash值计算出index的Node 都不为null if ((tab = table) != null && (n = tab.length) > 0 && (p = tab[index = (n - 1) & hash]) != null) { Node<K,V> node = null, e; K k; V v; //判断需要remove的Node是否是根据hash值计算出的 p if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) node = p;// 条件成立(成立的原因一般是Node != null 但是 key == null) 定义Node //定义 e 为p 判断 p的下一个Node 不是 null else if ((e = p.next) != null) { if (p instanceof TreeNode)// 判断p是否属于TreeNode类 //执行TreeNode 的 getTreeNode 方法[随后补充] node = ((TreeNode<K,V>)p).getTreeNode(hash, key); else { do {//do{}while()循环查询 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) { node = e;//条件成功 定义node 并结束 break; } p = e;//继续循环下一个 } while ((e = e.next) != null); } } // 判断 node != null matchValue:false 定义v (目前感觉只要走到这里 这里一定是成立 // 的) if (node != null && (!matchValue || (v = node.value) == value || (value != null && value.equals(v)))) { if (node instanceof TreeNode)// 判断node属于TreeNode类 // 执行 TreeNode 的 removeTreeNode 方法 [随后补充] ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable); else if (node == p)// 直接将node的下一个赋值给tab[index] tab[index] = node.next;//从新定义了tab[index] oldNode 被覆盖 else // 此处经过循环 p.next = node 从新定义p.next = node.next // 直接 remove node节点 p.next = node.next;//将 node的下一个node 赋值给p的下一个 链表操作 ++modCount;//操作++ --size;//size -- afterNodeRemoval(node); return node;//返回node } } return null;//返回null }
-
map.clear 方法
// map.clear 方法 public void clear() { Node<K,V>[] tab; modCount++;//操作++ if ((tab = table) != null && size > 0) { size = 0;//从新定义size 为 0 for (int i = 0; i < tab.length; ++i) tab[i] = null;//循环操作 定义每一个node为null } }
-
map.hash 方法
// hash(key) 方法 比较特殊 需要慢慢理解 static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }
Map的实现类HashMap的各种方法源码解析
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.