HashMap


HashMap

HashMap主要用来存放键值对,其基于哈希表的Map接口实现,是常用的Java集合之一,是非线程安全的

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {
	// ......
}

HashMap可以存储null的keyvalue,但是null作为键只能有一个,null作为值可以有多个

JDK1.8之前HashMap由数组+链表组成,数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的(“拉链法”解决冲突),在JDK1.8以后的HashMap在解决哈希冲突时有了较大的变化,当链表长度>=阈值(默认为8)(在链表转为红黑树之前会先判断,如果当前数组长度小于64,那么会先进行数组扩容,而不是转为红黑树)时,将链表转化为红黑树,以减少搜索时间

image-20240424105325142

HashMap默认的初始化大小为16,之后每次扩充,容量变为原来的2倍,并且,HashMap总是使用2的幂作为哈希表的大小

<<4相当于乘2^4,也就是16

/**
 * The default initial capacity - MUST be a power of two.
 */
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

image-20240424110504987

HashMap底层数据结构分析

JDK1.8之前

JDK1.8之前的HashMap底层是数组+链表结合在一起使用也就是链表散列

HashMap通过keyhashCode经过扰动函数处理后得到hash值,然后通过(n-1)&hash(n是数组长度)判断当前元素存放的位置,如果当前位置存在元素的话,就判断该元素与要存入的元素的hash值以及key是否相同,如果相同则直接覆盖,不同就用拉链法解决冲突

扰动函数:hash()

final int hash(Object k) {
    int h = hashSeed;
    if (0 != h && k instanceof String) {
        return sun.misc.Hashing.stringHash32((String) k);
    }

    h ^= k.hashCode();

    // This function ensures that hashCodes that differ only by
    // constant multiples at each bit position have a bounded
    // number of collisions (approximately 8 at default load factor).
    h ^= (h >>> 20) ^ (h >>> 12);
    return h ^ (h >>> 7) ^ (h >>> 4);
}

JDK1.8之后的hash(),相较于1.8之前的hash方法,1.8之后的原理不变,但是更加简化了,此外1.8之前的hash方法性能会稍差一点,因为扰动了4次

static final int hash(Object key) {
    int h;
    // key.hashCode():返回散列值(hashCode)
    // ^:按位异或
    // >>>:无符号右移,忽略符号位,空位用0补齐
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

拉链法?

将链表和数组结合,创建一个链表数组,数组中每一格就是一个链表,如果遇到哈希冲突,则将冲突的值添加到链表的末端即可

image-20240424171813688

JDK1.8之后

相比于之前的版本,JDK1.8之后在解决哈希冲突时有了比较大的变化

当链表长度>=阈值(8)的时候,会首先调用treeifyBin()方法,

image-20240424140038159

treeifyBin()方法会根据HashMap数组来决定是否转换为红黑树,只有当数组长度>=64的情况下才会执行转换红黑树的操作,以减少搜索时间,否则只是执行resize()方法对数组扩容:

treeifyBin()

static final int MIN_TREEIFY_CAPACITY = 64;
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);
    }
}

resize()

image-20240424140518203

类的属性

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {
	// 序列号
    private static final long serialVersionUID = 362498820763181265L;
	// 默认的初始容量(16)
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
	// 最大容量
    static final int MAXIMUM_CAPACITY = 1 << 30;
	// 默认的负载因子
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
	// 当桶(bucket)上的节点数>=这个值就会转成红黑树
    static final int TREEIFY_THRESHOLD = 8;
	// 当桶(bucket)上的节点数<=这个值时树转链表
    static final int UNTREEIFY_THRESHOLD = 6;
	// 桶中结构转化为红黑树对应的table的最小容量
    static final int MIN_TREEIFY_CAPACITY = 64;
	// 存储元素的数组,总是2的幂次倍
    transient Node<K,V>[] table;
	// 存放具体元素的集合
    transient Set<Map.Entry<K,V>> entrySet;
	// 存放元素的个数,该值不等于数组长度
    transient int size;
	// 每次扩容和更改map结构的计数器
    transient int modCount;
	// 阈值(容量*负载因子)当实际大小超过阈值时就会扩容
    int threshold;
	// 负载因子
    final float loadFactor;
}
  • loadFactor

    loadFactor负载因子时控制数组存放数据的疏密程度,loadFactor越趋近于1,那么数组中存放的数据(entry)也就越多、越密,也就是会让链表的长度增加,loadFactor越小,越趋近于0,数组中存放的数据(entry)也就越少、越稀疏

    loadFactor太大会导致查找元素效率降低,太小会导致数组的利用率低,存放的数据会很分散,因此官方给出的默认值为0.75f是一个比较好的临界值

    给定的默认容量为16,负载因子为0.75,Map在使用过程中不断地往里面存放数据,当数量超过了16*0.75=12就需要将当前16的容量进行扩容,而扩容的这个过程涉及到rehash、复制数据等操作,所以非常消耗性能

  • threshold

    threshold = capacity * loadFactor,当size>threshold时,就要考虑对数组的扩容了,即该属性是衡量数组是否需要扩容的一个标准

Node节点(继承自Map.Entry<K,V>

static class Node<K,V> implements Map.Entry<K,V> {
    final int hash; // 哈希值,存放元素到HashMap中时用来与其他元素hash值比较
    final K key; // 键
    V value; // 值
    Node<K,V> next; // 指向下一个节点Node

    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; }
	// 重写hashCode()方法
    public final int hashCode() {
        return Objects.hashCode(key) ^ Objects.hashCode(value);
    }
	
    public final V setValue(V newValue) {
        V oldValue = value;
        value = newValue;
        return oldValue;
    }
	// 重写equals()方法
    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;
    }
}

树节点类

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;		// needed to unlink next upon deletion
    boolean red;			// 判断颜色
    TreeNode(int hash, K key, V val, Node<K,V> next) {
        super(hash, key, val, next);
    }
	// 返回根节点
    final TreeNode<K,V> root() {
        for (TreeNode<K,V> r = this, p;;) {
            if ((p = r.parent) == null)
                return r;
            r = p;
        }
    }

    static <K,V> void moveRootToFront(Node<K,V>[] tab, TreeNode<K,V> root) {
        // ......
    }

    /**
     * Finds the node starting at root p with the given hash and key.
     * The kc argument caches comparableClassFor(key) upon first use
     * comparing keys.
     */
    final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
        // ......
    }

    /**
     * Calls find for root node.
     */
    final TreeNode<K,V> getTreeNode(int h, Object k) {
        return ((parent != null) ? root() : this).find(h, k, null);
    }

    /**
     * Tie-breaking utility for ordering insertions when equal
     * hashCodes and non-comparable. We don't require a total
     * order, just a consistent insertion rule to maintain
     * equivalence across rebalancings. Tie-breaking further than
     * necessary simplifies testing a bit.
     */
    static int tieBreakOrder(Object a, Object b) {
        // ......
    }

    /**
     * Forms tree of the nodes linked from this node.
     */
    final void treeify(Node<K,V>[] tab) {
        // ......
    }

    /**
     * Returns a list of non-TreeNodes replacing those linked from
     * this node.
     */
    final Node<K,V> untreeify(HashMap<K,V> map) {
        // ......
    }

    /**
     * Tree version of putVal.
     */
    final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
                                   int h, K k, V v) {
        // ......
    }

    /**
     * Removes the given node, that must be present before this call.
     * This is messier than typical red-black deletion code because we
     * cannot swap the contents of an interior node with a leaf
     * successor that is pinned by "next" pointers that are accessible
     * independently during traversal. So instead we swap the tree
     * linkages. If the current tree appears to have too few nodes,
     * the bin is converted back to a plain bin. (The test triggers
     * somewhere between 2 and 6 nodes, depending on tree structure).
     */
    final void removeTreeNode(HashMap<K,V> map, Node<K,V>[] tab,
                              boolean movable) {
        // ......
    }

    /**
     * Splits nodes in a tree bin into lower and upper tree bins,
     * or untreeifies if now too small. Called only from resize;
     * see above discussion about split bits and indices.
     *
     * @param map the map
     * @param tab the table for recording bin heads
     * @param index the index of the table being split
     * @param bit the bit of hash to split on
     */
    final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
        // ......
    }

    /* ------------------------------------------------------------ */
    // Red-black tree methods, all adapted from CLR
    static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
                                          TreeNode<K,V> p) {
        // ......
    }

    static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
                                           TreeNode<K,V> p) {
        // ......
    }

    static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
                                                TreeNode<K,V> x) {
        // ......
    }

    static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
                                               TreeNode<K,V> x) {
        // ......
    }

    /**
     * Recursive invariant check
     */
    static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
        // ......
    }
}

HashMap源码分析

构造器

来看看HashMap的构造方法

  1. 默认构造器

    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }
  2. 包含另一个Map的构造器

    public HashMap(Map<? extends K, ? extends V> m) {
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        putMapEntries(m, false);
    }
  3. 指定容量大小的构造器

    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
  4. 指定容量大小负载因子的构造器

    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);
    }

在上述的四个构造器中,都初始化了负载因子(loadFactor),由于HashMap中没有capacity这样的字段,即使指定了初始化容量initialCapacity,也只是通过tableSizeFor将其扩容到与initialCapacity最接近的2的幂次方大小,然后暂时赋值给threshold,后续通过resize方法将threshold赋值给newCap进行table的初始化

putMapEntries()

final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
    int s = m.size();
    if (s > 0) {
        // 判断table是否被初始化
        if (table == null) { // pre-size
            /**
             * 如果未被初始化,s是m的实际元素个数
             * ft = s / loadFactor ==> s = ft * loadFactor(阈值 = 容量 * 负载因子)
             * ft就是要添加s个元素的最小容量
             */
            float ft = ((float)s / loadFactor) + 1.0F;
            int t = ((ft < (float)MAXIMUM_CAPACITY) ?
                    (int)ft : MAXIMUM_CAPACITY);
            /**
             * 根据构造函数可以得知table未被初始化,threshold实际上存放的是初始化容量,
             * 如果添加s个元素所需的最小容量大于初始化容量,则将最小容量扩容为最接近的2的
             * 幂次方大小作为初始化
             * 这里并不是初始化阈值
             */
            if (t > threshold)
                threshold = tableSizeFor(t);
        }
        // 如果已经初始化,并且m元素个数大于阈值,则进行扩容处理
        else if (s > threshold)
            resize();
        // 将m中的所有元素添加到HashMap中,如果table未被初始化,putVal方法中会调用resize初始化或扩容
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
            K key = e.getKey();
            V value = e.getValue();
            putVal(hash(key), key, value, false, evict);
        }
    }
}

put()

HashMap只提供了put用于添加元素,putVal方法只是给put方法调用的一个方法,并没有提供给用户使用,

public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}

putVal方法流程图如下

image-20240424161041114

putputVal方法的源码,还有treeifyBin

public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}
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是否初始化或长度是否为0,是则进行扩容
    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);
    // 如果算出的位置已经有元素,则处理hash冲突
    else {
        Node<K,V> e; K k;
        // 判断第一个节点table[i]的key是否和插入的key一致,是则直接使用插入的值p替换掉旧的值e
        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(阈值)调用treeifyBin方法
                    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                        treeifyBin(tab, hash);
                    break;
                }
                // 判断链表中节点的key值与插入的元素的key值是否相等
                if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                // 遍历数组中的链表,与前面的e = p.next组合,可以遍历链表
                p = e;
            }
        }
        // 在数组对应位置的链表中找到key值、hash值与插入元素相等的节点
        if (e != null) { // existing mapping for key
            // 记录下value值
            V oldValue = e.value;
            // onlyIfAbsent为false或旧值(value值)为null
            if (!onlyIfAbsent || oldValue == null)
                // 用新值代替旧值
                e.value = value;
            // 访问后回调
            afterNodeAccess(e);
            // 返回旧值
            return oldValue;
        }
    }
    ++modCount;
    // 实际大小小于阈值则扩容
    if (++size > threshold)
        resize();
    afterNodeInsertion(evict);
    return null;
}

// 该方法会根据HashMap数组的长度来决定是否转为红黑树
// 只有当数组长度大于等于64的情况下才会执行转换红黑树的操作,以减少搜索事件,否则就只是对数组进行扩容
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);
    }
}

JDK1.8之前的put方法

  1. 如果定位到的数组位置没有元素就直接插入
  2. 如果有元素则遍历这个元素为头结点的链表,依次和要插入的key进行比较,如果相同就直接覆盖,不同就采取头插法插入元素
public V put(K key, V value) {
    if (table == EMPTY_TABLE) {
        inflateTable(threshold);
    }
    if (key == null)
        return putForNullKey(value);
    int hash = hash(key);
    int i = indexFor(hash, table.length);
    for (Entry<K,V> e = table[i]; e != null; e = e.next) {
        Object k;
        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }

    modCount++;
    addEntry(hash, key, value, i);
    return null;
}

get()

public V get(Object key) {
    Node<K,V> e;
    return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
    Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
    // first:数组中对应位置的链表的第一个节点
    if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) {
        // 判断第一个节点的哈希值与传入的键的哈希值是否相等、传入的键和第一个节点的键是否相等,相等则直接返回
        if (first.hash == hash && ((k = first.key) == key || (key != null && key.equals(k))))
            return first;
        if ((e = first.next) != null) {
            // 判断first是不是红黑树的节点,是的话就在红黑树中获取
            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;
}

resize()

该方法是用来扩容的,会伴随着一次重新hash分配,并且会遍历hash表中所有的元素,非常地耗时,

resize()方法实际上是将table初始化和table扩容进行了整合,底层的行为是给table赋了一个新的数组

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
    }
    else if (oldThr > 0) // initial capacity was placed in threshold
        newCap = oldThr;
    else {
        newCap = DEFAULT_INITIAL_CAPACITY;
        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
    }
    if (newThr == 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;
}
  • 超过最大值就不在扩充了

    if (oldCap >= MAXIMUM_CAPACITY) {
        threshold = Integer.MAX_VALUE;
        return oldTab;
    }
  • 没超过最大值就扩容为原来的2倍

    else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
             oldCap >= DEFAULT_INITIAL_CAPACITY)
        newThr = oldThr << 1; // double threshold
  • 创建对象时初始化容量大小放在threshold中,此时只需要将其作为新的数组容量

    else if (oldThr > 0) 
        newCap = oldThr;
  • 无参构造器创建的对象在这里计算阈值和容量

    else {
        newCap = DEFAULT_INITIAL_CAPACITY;
        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
    }
  • 创建时制定了初始化容量或者负载因子,在这里进行阈值初始化,或者扩容的旧容量<16,在这里计算新的resize上限

    float ft = (float)newCap * loadFactor;
    newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE);
  • 如果数组不为空则遍历数组

    // 把每个bucket都移动到新的bucket中(数组)
    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)
                // 将红黑树分为两棵子树,如果子树节点<=UNTREEIFY——THRESHOLD(默认是6)则将子树转换为链表
                // 如果大于则保持子树的结构
                ((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;
                    }
                    // 原来的索引+oldCap
                    else {
                        if (hiTail == null)
                            hiHead = e;
                        else
                            hiTail.next = e;
                        hiTail = e;
                    }
                } while ((e = next) != null);
                // 原索引放回bucket中(数组)
                if (loTail != null) {
                    loTail.next = null;
                    newTab[j] = loHead;
                }
                // 原来的索引+oldCap放到bucket里(数组)
                if (hiTail != null) {
                    hiTail.next = null;
                    newTab[j + oldCap] = hiHead;
                }
            }
        }
    }

文章作者: Feliks
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Feliks !
评论
  目录