美文网首页
JDK1.7 HashMap源码阅读#transfer()

JDK1.7 HashMap源码阅读#transfer()

作者: 丫头可乐 | 来源:发表于2020-03-23 06:27 被阅读0次
/**
     * Transfers all entries from current table to newTable.
     */
    void transfer(Entry[] newTable, boolean rehash) {
        int newCapacity = newTable.length;
        for (Entry<K,V> e : table) {
            while(null != e) {
                Entry<K,V> next = e.next;
                if (rehash) {
                    e.hash = null == e.key ? 0 : hash(e.key);
                }
                int i = indexFor(e.hash, newCapacity);
//最主要的就是这两句。导致的结果就是原先在链表上靠近顶部的元素,在新的table中,会称为链表底部的元素
                e.next = newTable[i];
                newTable[i] = e;
                e = next;
            }
        }
    }

这段代码真心让我看了很久,没有转过弯来。

相关文章

网友评论

      本文标题:JDK1.7 HashMap源码阅读#transfer()

      本文链接:https://www.haomeiwen.com/subject/yvciyhtx.html