前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HashMap 源码分析

HashMap 源码分析

作者头像
Jacob丶
发布2020-08-05 18:03:00
5610
发布2020-08-05 18:03:00
举报
文章被收录于专栏:JacobJacob

简介

HashMap 底层数据结构是数组、哈希表。数组称之为哈希桶,它是线程不安全的。

允许key为null,value为null。

遍历时无序。

在JDK8中,当链表长度达到8,会转化成红黑树,以提升它的查询、插入效率。

结构

HashMap继承AbstractMap<K,V>,实现了Map,Cloneable, Serializable。如下:

代码语言:javascript
复制
public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable 
  • 继承AbstractMap,实现了Map接口:提供了基本功能(size、isEmpty、containsKey、containsValue等到)。
  • 实现Cloneable:实现clone()方法,实现克隆。

数据结构

HashMap中有两种数据结构,一种链表,一种红黑树。

链表
代码语言:javascript
复制
    static class Node<K,V> implements Map.Entry<K,V> {
        // hash值
        final int hash;
        // key
        final K key;
        // value
        V value;
        // 下一节点
        Node<K,V> next;

        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }

        public final K getKey()        { return key; }
        public final V getValue()      { return value; }
        public final String toString() { return key + "=" + value; }

        // ^ : 位异或运算。二进制数,从高位到低位逐一比较,相同为0,不同为1。
        // 每个节点的hash值是通过key和value的hash值亦或得到。
        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

        // 设置新值,覆盖旧值,同时返回旧值
        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        // 比较是否是同一个对象
        public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                    Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
    }

总结:通过分析得知,这是一个单链表;每个节点的hash值是通过key和value的hash值亦或得到。

红黑树
代码语言:javascript
复制
    static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
        TreeNode<K,V> parent;  // 红黑树的链接
        TreeNode<K,V> left;
        TreeNode<K,V> right;
        TreeNode<K,V> prev;    // 需要取消链接后删除
        boolean red;
        TreeNode(int hash, K key, V val, Node<K,V> next) {
            super(hash, key, val, next);
        }
    //只是部分代码,缺少一个右大括号

常量值

代码语言:javascript
复制
    /**
     * The default initial capacity - MUST be a power of two.
     * - 默认的初始容量(必须是2的幂)。
     */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    /**
     * 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.
     * - << : 左移运算符,eg:1<<1 = 2^1 ; 1<<30 = 2^30
     * - 最大容量,如果更高的值是由任何一个带有参数的构造函数隐式指定的,则使用该值。必须是2的幂<= 1<<30。
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;

    /**
     * The load factor used when none specified in constructor.
     * - 默认加载因子,在构造函数中没有指定时使用的负载因子
     */
    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.
     * -  桶的树化阈值:链表转成红黑树的阈值
     * - 当冲突的元素数增加到8时,链表变为树。
     */
    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.
     * - 桶的链表还原阈值:红黑树转为链表的阈值。
     * - 当减少至6时,树切换为链表。
     */
    static final int UNTREEIFY_THRESHOLD = 6;

    /**
     * The smallest table capacity for which bins may be treeified.
     * (Otherwise the table is resized if too many nodes in a bin.)
     * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
     * between resizing and treeification thresholds.
     * - 最小树形化容量阈值:当哈希表中的容量 > 该值时,才允许树形化链表 (即 将链表 转换成红黑树)
     * - 否则,若桶内元素太多时,则直接扩容,而不是树形化
     * - 为了避免进行扩容、树形化选择的冲突,这个值不能小于 4 * TREEIFY_THRESHOLD
     */
    static final int MIN_TREEIFY_CAPACITY = 64;

字段

代码语言:javascript
复制
    /**
     * 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.)
     * - 哈希桶,存放链表。 长度是2的N次方,或者初始化时为0.
     */
    transient Node<K,V>[] table;

    /**
     * Holds cached entrySet(). Note that AbstractMap fields are used
     * for keySet() and values().
     * - 数据转换成set的另一种存储形式,主要用于迭代功能。
     */
    transient Set<Map.Entry<K,V>> entrySet;

    /**
     * The number of key-value mappings contained in this map.
     * - 列表大小:此映射中包含的键-值映射的数目。
     */
    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).
     * - 记录被修改的次数
     */
    transient int modCount;

    /**
     * The next size value at which to resize (capacity * load factor).
     *
     * @serial
     */
    // (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.
     * - 负载因子,可计算出当前table长度下的扩容阈值:threshold = loadFactor * table.length。
     * @serial
     */
    final float loadFactor;

构造方法

HahsMap提供了四种构造方法
HashMap()

无参构造:构造一个空的HashMap,具有默认的初始容量16和默认的负载因子0.75。

代码语言:javascript
复制
    /**
     * Constructs an empty HashMap with the default initial capacity
     * (16) and the default load factor (0.75).
     * - 构造一个空的HashMap,具有默认的初始容量16和默认的负载因子0.75。
     */
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }
HashMap(int initialCapacity, float loadFactor)

使用指定的初始容量和负载因子构造一个空的HashMap。

代码语言:javascript
复制
    /**
     * Constructs an empty HashMap with the specified initial
     * capacity and load factor.
     * - 使用指定的初始容量和负载因子构造一个空的HashMap。
     * @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) {
        // 初始容量 < 0 抛出异常
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        //初始容量大于对打容量,取最大容量
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        // 负载因子 <= 0 或 负载因子为空
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        // 赋值
        this.loadFactor = loadFactor;
        // 新的扩容临界值
        this.threshold = tableSizeFor(initialCapacity);
    }

    /**
     * Returns a power of two size for the given target capacity.
     */
    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

>>> : 无符号右移详情 →Java 按位运算符

HashMap(int initialCapacity)

构造一个空的HashMap,具有指定的初始容量和缺省负载因子0.75。

代码语言:javascript
复制
    /**
     * Constructs an empty HashMap with the specified initial
     * capacity and the default load factor (0.75).
     * - 构造一个空的HashMap,具有指定的初始容量和缺省负载因子0.75。
     * @param  initialCapacity the initial capacity.
     * - 初始容量
     * @throws IllegalArgumentException if the initial capacity is negative.
     * - 抛出异常
     */
    public HashMap(int initialCapacity) {
        // 使用HashMap(int initialCapacity, float loadFactor)构造,创建
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
HashMap(Map<? extends K, ? extends V> m)

使用与指定的Map构造新的HashMap,新HashMap使用默认负载因子(0.75)和足够容纳指定Map中的元素的初始容量创建的。

代码语言:javascript
复制
    /**
     * Constructs a new HashMap with the same mappings as the
     * specified  Map .  The HashMap is created with
     * default load factor (0.75) and an initial capacity sufficient to
     * hold the mappings in the specified  Map .
     *
     * @param   m the map whose mappings are to be placed in this map
     * - 这个Map的元素将被放在这个Map中
     * @throws  NullPointerException if the specified map is null
     */
    public HashMap(Map<? extends K, ? extends V> m) {
        // 默认负载因子 0.75f
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        putMapEntries(m, false);
    }

    /**
     * - 哈希桶,存放链表。 长度是2的N次方,或者初始化时为0.
     */
    transient Node<K,V>[] table;

    /**
     * Implements Map.putAll and Map constructor.
     *
     * @param m the map
     * @param evict false when initially constructing this map, else true (relayed to method afterNodeInsertion).
     * - 在最初构造这个映射时为false,否则为true。
     */
    final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
        int s = m.size();
        // Map的大小是不是大于0(等于0就没必要初始化了)
        if (s > 0) {
            // 通过构造器新建一个HashMap,所以table是null(table是Node数组)
            if (table == null) { // pre-size
                //计算阀值,判断是否大于最大容量,如果没超过则还是原阀值ft,将ft赋给t。
                float ft = ((float)s / loadFactor) + 1.0F;
                int t = ((ft < (float)MAXIMUM_CAPACITY) ? (int)ft : MAXIMUM_CAPACITY);
                //判断阀值t是否大于原阀值(原阀值是16,默认容量是16),如果大于原阀值,重新计算tableSizeFor(t)。
                if (t > threshold)
                    threshold = tableSizeFor(t);
            }
            else if (s > threshold)
                //构造调用,先不做解释
                resize();
            //
            for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
                K key = e.getKey();
                V value = e.getValue();
                // putVal()方法先不看,主要功能是创建一个节点,把这个节点放到了tab中
                putVal(hash(key), key, value, false, evict);
            }
        }
    }

    /**
     * Returns a power of two size for the given target capacity.
     */
    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

普通方法

size()

返回此Map中的键值对个数。

isEmpty()

如果此Map不包含键值映射,则返回true

代码语言:javascript
复制
public boolean isEmpty() { return size == 0; }
get(Object key)

返回指定key对应的value,如果没有改键值对,则返回 null。

代码语言:javascript
复制
    public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }

    /**
     * Implements Map.get and related methods.
     * - 实现于 Map.get 方法 和 相关方法
     * @param hash hash for key
     * @param key the key
     * @return the node, or null if none
     */
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab;
        Node<K,V> first, e;
        int n; K k;
        // 判断Node[]不等与空,并且第一个元素不等于空
        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)
                    // 红黑树循环遍历获取ke对应的value
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                // 链表循环遍历获取ke对应的value
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }
containsKey(Object key)

判断指定key是否存在,如果存在则返回true,否则返回false。

代码语言:javascript
复制
    public boolean containsKey(Object key) {
        return getNode(hash(key), key) != null;
    }

    /**
     * Implements Map.get and related methods.
     * - 实现于 Map.get 方法 和 相关方法
     * @param hash hash for key
     * @param key the key
     * @return the node, or null if none
     */
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab;
        Node<K,V> first, e;
        int n; K k;
        // 判断Node[]不等与空,并且第一个元素不等于空
        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)
                    // 红黑树循环遍历获取ke对应的value
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                // 链表循环遍历获取ke对应的value
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }
containsValue(Object value)

判断指定value是否存在,如果存在则返回true,否则返回false。

代码语言:javascript
复制
    public boolean containsValue(Object value) {
        Node<K,V>[] tab; V v;
        //判断table不等与空
        if ((tab = table) != null && size > 0) {
            //循环遍历table数组
            for (int i = 0; i < tab.length; ++i) {
                //循环遍历链表
                for (Node<K,V> e = tab[i]; e != null; e = e.next) {
                    //判断value是否存在,存在返回true
                    if ((v = e.value) == value || (value != null && value.equals(v)))
                        return true;
                }
            }
        }
        return false;
    }
clear()

删除Map中的元素

代码语言:javascript
复制
    public void clear() {
        Node<K,V>[] tab;
        //集合修改次数加1
        modCount++;
        //循环遍历Node数组
        if ((tab = table) != null && size > 0) {
            //长度置空
            size = 0;
            //循环遍历,链表置空
            for (int i = 0; i < tab.length; ++i)
                tab[i] = null;
        }
    }
put(K key, V value)

将指定键与值进行关联,并存储。如果键已存在,则新value值覆盖旧value值。

代码语言:javascript
复制
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict)
代码语言:javascript
复制
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
        Node<K,V>[] tab;
        Node<K,V> p;
        int n, i;
        // 如果table为空,则进行必要字段的初始化
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;// 获取长度(16)
        // 如果根据hash值获取的结点为空,则新建一个结点
        if ((p = tab[i = (n - 1) & hash]) == null)  // 此处 & 代替了 % (除法散列法进行散列)
            // 这里的p结点是根据hash值算出来对应在数组中的元素
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e;
            K k;
            // 如果新插入的结点和table中p结点的hash值,key值相同的话
            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);
                        // 链表长度大于8时,将链表转红黑树
                        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
                    p = e;
                }
            }
             // 如果存在这个映射就覆盖
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                //判断是否允许覆盖,并且value是否为空
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e); //回调以允许LinkedHashMap后置操作
                return oldValue;
            }
        }
        // 更改操作次数
        ++modCount;
        // 大于临界值
        if (++size > threshold)
            // 将数组大小设置为原来的2倍,并将原先的数组中的元素放到新数组中
            // 因为有链表,红黑树之类,因此还要调整他们
            resize();
        afterNodeInsertion(evict);  //回调以允许LinkedHashMap后置操作
        return null;
    }
resize()
代码语言:javascript
复制
    /**
     *初始化或者扩容之后元素调整
     */
    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) {
            // 如果旧元素长度达到最大值,则修改临界值为Integer.MAX_VALUE
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }
            // 下面就是扩容操作(2倍)
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY)
                // threshold也变为二倍
                newThr = oldThr << 1; // double threshold
        }
        
        else if (oldThr > 0) // 初始容量设置为旧元素的临界值
            newCap = oldThr;
        else {               // threshold为0,则使用默认值
            newCap = DEFAULT_INITIAL_CAPACITY;
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        if (newThr == 0) {  // 如果临界值还为0,则设置临界值
            float ft = (float)newCap * loadFactor;
            newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                      (int)ft : Integer.MAX_VALUE);
        }
        threshold = newThr; // 更新填充因子
        @SuppressWarnings({"rawtypes","unchecked"})
        Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
        table = newTab;
        if (oldTab != null) {    // 调整数组大小之后,需要调整红黑树或者链表的指向
            for (int j = 0; j < oldCap; ++j) {
                Node<K,V> e;
                if ((e = oldTab[j]) != null) {
                    oldTab[j] = null;
                    if (e.next == null)
                        newTab[e.hash & (newCap - 1)] = e;
                    else if (e instanceof TreeNode)  // 红黑树调整
                        ((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;
                        Node<K,V> next;
                        do {
                            next = e.next;
                            if ((e.hash & oldCap) == 0) {
                                if (loTail == null)
                                    loHead = e;
                                else
                                    loTail.next = e;
                                loTail = e;
                            }
                            else {
                                if (hiTail == null)
                                    hiHead = e;
                                else
                                    hiTail.next = e;
                                hiTail = e;
                            }
                        } while ((e = next) != null);
                        if (loTail != null) {
                            loTail.next = null;
                            newTab[j] = loHead;
                        }
                        if (hiTail != null) {
                            hiTail.next = null;
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
        return newTab;
    }
treeifyBin(Node<K,V>[] tab, int hash)
代码语言:javascript
复制
    // 链表转双向链表操作
    final void treeifyBin(Node<K,V>[] tab, int hash) {
        int n, index; Node<K,V> e;  
        // 如果元素总个数小于64,则继续进行扩容,结点指向调节
        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);
        }
    }
treeify(Node<K,V>[] tab)
代码语言:javascript
复制
        //将链表中每个值进行红黑树插入操作
        final void treeify(Node<K,V>[] tab) {
            TreeNode<K,V> root = null;
            // TreeNode<K,V> x = this  相当于初始化了一个结点
            for (TreeNode<K,V> x = this, next; x != null; x = next) {
                next = (TreeNode<K,V>)x.next;
                // 初始化Root
                x.left = x.right = null;
                if (root == null) {
                    x.parent = null;
                    x.red = false;
                    root = x;
                }
                else {
                    K k = x.key;
                    int h = x.hash;
                    Class<?> kc = null;
                    for (TreeNode<K,V> p = root;;) {
                        int dir, ph;
                        K pk = p.key;
                        if ((ph = p.hash) > h)
                            dir = -1;
                        else if (ph < h)
                            dir = 1;
                        else if ((kc == null &&
                            // comparableClassFor(k) 返回 k 类型的比较器
                                  (kc = comparableClassFor(k)) == null) ||
                            // compareComparables(kc, k, pk) 返回p,pk比较的结果
                                 (dir = compareComparables(kc, k, pk)) == 0)
                            // tieBreakOrder(k, pk) 比较两个hash码
                            dir = tieBreakOrder(k, pk);
                        // 此处进行红黑树操作
                        TreeNode<K,V> xp = p;
                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
                            x.parent = xp;
                            if (dir <= 0)
                                xp.left = x;
                            else
                                xp.right = x;
                            // 平衡调节
                            root = balanceInsertion(root, x);
                            break;
                        }
                    }
                }
            }
            // 确保给定的根是根结点
            moveRootToFront(tab, root);
        }
remove(Object key)

如果存在,则从此映射中删除指定键的映射。

代码语言:javascript
复制
    public V remove(Object key) {
        Node<K,V> e;
        return (e = removeNode(hash(key), key, null, false, true)) == null ? null : e.value;
    }

    /**
     * Implements Map.remove and related methods.
     * - 实现了与remove相关操作
     * @param hash hash for key
     * @param key the key
     * @param value the value to match if matchValue, else ignored
     * @param matchValue if true only remove if value is equal
     * @param movable if false do not move other nodes while removing
     * @return the node, or null if none
     */
    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;
        //table不为空
        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;
            //如果要删除的结点和table中p结点的hash值,key值相同的话
            if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k))))
                node = p;
            else if ((e = p.next) != null) {
                //如果是红黑树结点,则进行红黑树查找key对应的value
                if (p instanceof TreeNode)
                    node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
                else {
                    // 链表循环遍历获取ke对应的value
                    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); //回调以允许LinkedHashMap后置操作
                return node;
            }
        }
        return null;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 结构
  • 数据结构
    • 链表
      • 红黑树
      • 常量值
      • 字段
      • 构造方法
        • HahsMap提供了四种构造方法
          • HashMap()
          • HashMap(int initialCapacity, float loadFactor)
          • HashMap(int initialCapacity)
          • HashMap(Map<? extends K, ? extends V> m)
      • 普通方法
        • size()
          • isEmpty()
            • get(Object key)
              • containsKey(Object key)
                • containsValue(Object value)
                  • clear()
                    • put(K key, V value)
                      • putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict)
                        • resize()
                          • treeifyBin(Node<K,V>[] tab, int hash)
                            • treeify(Node<K,V>[] tab)
                              • remove(Object key)
                              领券
                              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档