JDK1.8HashMap源码分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baijunzhijiang_01/article/details/83892344

关键变量解析

   static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    /**
     * 最大容量 2^30
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;

    /**
     * 碰撞因子 默认为0.75
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

    /**
     * 链表转红黑树的域值 8
     */
    static final int TREEIFY_THRESHOLD = 8;

    /**
     * 红黑树转链表的域值 6
     */
    static final int UNTREEIFY_THRESHOLD = 6;

    /**
     * 转变成树之前,还会有一次判断,只有键值对数量大于 64 
     * 才会发生转换。这是为了避免在哈希表建立初期,多个键值对恰好被放入了同* 一个链表中而导致不必要的转化。
     */
    static final int MIN_TREEIFY_CAPACITY = 64;

put方法

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;
    }

  1. 根据hash值计算桶的位置,如果没有发现桶,则新建一个桶
  2. 根据hash值找到了桶,若桶中第一个元素的key和put的key一致,则替换一下value
  3. 若桶中第一个节点key和put的key不一致,且节点为树节点,则调用树节点的节点添加方法。否者,调用链表的节点添加方法。
  4. 链表添加节点时,要做链表转树的判断,如果添加之前桶中数量大于等于7,要转为树。
  5. 返回就得节点的value,size数量+1,操作数+1,如果size大于域值,重新分配空间。

Remove方法

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) {
            Node<K,V> node = null, e; K k; V v;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                node = p;
            else if ((e = p.next) != 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)))) {
                            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.next = node.next;
                ++modCount;
                --size;
                afterNodeRemoval(node);
                return node;
            }
        }
        return null;
    }

HashMap删除节点逻辑:

  1. 首先校验hash桶和传入参数的合法性,根据hash值计算出该hash值对应的tab数组中的索引位置
  2. 取出第一个节点,若当前节点的hash值和节点的k值是和目标节点的数据一致,返回查找到的节点。
  3. 若当前节点不是目标节点,如果当前节点是红黑树节点,则执行红黑树的查找节点方法。如果是链表节点,则通过while循环,查找到目标节点。
  4. 查找到节点之后,如果目标节点是红黑树节点,则调用红黑树的节点删除方法,如果目标节点是链表节点,且上一个节点和目标节点是同一个节点,则直接更改头节点为下一个节点。如果上一个节点和目标节点不是同一个节点,则将目标节点移除。
  5. 将size减一,返回删除的目标节点。

猜你喜欢

转载自blog.csdn.net/baijunzhijiang_01/article/details/83892344