HashMap源码 5

  1. treeifyBin()方法:
    大致过程为:
    链表长度超过TREEIFY_THRESHOLD(默认为8)时,会调用本方法,
    本方法会判断HashMap的长度,如果小于MIN_TREEIFY_CAPACITY(默认为64),
    则进行扩容,否则将链表转换为TreeNode链,最后调用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)    // 只有键值对数量大于 64 才会发生转换
            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)    // 将链表转换为红黑树链,并判断是否为null
                hd.treeify(tab);    // 红黑树链转换为红黑树
        }
    }
  2. putAll()方法:
    把已存在的Map添加至一个新的Map,putMapEntries()在HashMap源码2的开头部分已经分析过
    public void putAll(Map<? extends K, ? extends V> m) {
            putMapEntries(m, true);
        }
  3.  remove()方法:
    方法的作用是根据key,删除节点
     
    public V remove(Object key) {
            Node<K,V> e;
            return (e = removeNode(hash(key), key, null, false, true)) == null ?    // 判断key是否存在,存在则删除,否则返回null
                null : e.value;
        }
  4. removeNode()方法:
    真正执行删除节点的方法,matchValue用于判断删除节点时,节点的value是否和参数的value相等
     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;
            if ((tab = table) != null && (n = tab.length) > 0 &&
                (p = tab[index = (n - 1) & hash]) != null) {    // 判断哈希表和链表是否为空,为空则返回null
                Node<K,V> node = null, e; K k; V v;
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))    // 如果链表第一个节点的key和hash都相等,表示存在相同的节点,则调用该节点
                    node = p;
                else if ((e = p.next) != null) {    // 如果链表的下一个节点不为null
                    if (p instanceof TreeNode)    // 判断节点是否红黑树
                        node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
                    else {
                        do {    // 遍历链表
                            if (e.hash == hash &&
                                ((k = e.key) == key ||
                                 (key != null && key.equals(k)))) {    // key和hash都相等,表示存在相同的节点,则调用该节点,并停止遍历
                                node = e;
                                break;
                            }
                            p = e;
                        } while ((e = e.next) != null);
                    }
                }
                if (node != null && (!matchValue || (v = node.value) == value ||
                                     (value != null && value.equals(v)))) {
                    if (node instanceof TreeNode)    // 如果该节点是红黑树,就用红黑树的方式删除
                        ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
                    else if (node == p)    // 如果节点是链表第一个节点,就让下一个节点作为链表第一节点
                        tab[index] = node.next;
                    else    // 如果不是链表第一个节点,那么p必然是node的前一个节点
                        p.next = node.next;    // p.next = node.next以断开p和node节点的连接,进行删除
                    ++modCount;
                    --size;
                    afterNodeRemoval(node);
                    return node;
                }
            }
            return null;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值