🍁 作者:知识浅谈,CSDN 博客专家,阿里云签约博主,InfoQ 签约博主,华为云云享专家
📌 擅长领域:全栈工程师、爬虫、ACM 算法
💒 公众号:知识浅谈
HashMap 源码分析(四)总结🤞这次都给他拿下🤞
正菜来了⛳⛳⛳
🎈HashMap 的相关函数
🍮Node<K,V> getNode()
含义:这个函数的意思是先通过其 key 对应的 hash 值,找到其在 hashtable 数组中的位置,先获取数组中对应位置的节点,使用 equals 比较节点的值,直到找到相等的节点值并返回。
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) { 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;}
复制代码
🍮boolean containsKey(Object key)
含义:获取 table 中是否存在 key 值对应的键值对,还是调用 getNode 函数返回对应的节点为 null 就是 false。
public boolean containsKey(Object key) { return getNode(hash(key), key) != null;}
复制代码
🍮V put(K key, V value)
含义:把 key 和 value 添加到对应 HashMap 中,调用 putVal 方法,把 key,value 添加到 hashMap 中。
public V put(K key, V value) { return putVal(hash(key), key, value, false, true);}
复制代码
把 putVal 函数中 key 的哈希值,对应的 key,value,对 map 进行添加,我们接下来查看 putVal 函数的使用。
🍮V putVal()
含义:这个函数就是 put 函数中的具体的实现,如果 HashMap 是树节点的时候,就需要调用 putTreeVal 函数,如果是链表加入链表节点,如果链表中的节点大于 8 且总的节点数大于 64 的时候,就需要进行链表转换为红黑树,最后如果节点数大于阈值的时候就进行 resize() 操作。
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) tab[i] = newNode(hash, key, value, null); else { Node<K,V> e; K k; 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 { 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; if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
复制代码
🍮Node<K,V>[] resize()
含义: 初始化或加倍表大小。如果为空,则按照字段阈值中保存的初始容量目标进行分配。否则,因为我们使用二次幂展开,每个 bin 中的元素必须保持相同的索引,或者在新表中以二次幂的偏移量移动,简单来说这个就是用来初始化表或者加倍表的大小。
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 }}
复制代码
🍮treeifyBin(Node<K,V>[] tab, int hash)
含义:替换给定哈希索引处 bin 中的所有链接节点,除非表太小,在这种情况下调整大小。里边调用的函数 treeify 表示从此节点链接的节点的表单树。
final void treeifyBin(Node<K,V>[] tab, int hash) { int n, index; Node<K,V> e; if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) resize(); else if ((e = tab[index = (n - 1) & hash]) != null) { TreeNode<K,V> hd = null, tl = null; do { TreeNode<K,V> p = replacementTreeNode(e, null); if (tl == null) hd = p; else { p.prev = tl; tl.next = p; } tl = p; } while ((e = e.next) != null); if ((tab[index] = hd) != null) hd.treeify(tab); }}
复制代码
🍚总结
以上就是 HashMap 源码分析的过程,希望有所帮助,Written By 知识浅谈
评论