Detailed explanation of HashMap automatic capacity expansion mechanism source code

Posted by MrLister on Mon, 14 Feb 2022 05:01:28 +0100

1, Introduction

  • HashMap's source code, which we have interpreted before, is an array plus a linked list. If the linked list is too long, it will split into a red black tree. The automatic capacity expansion mechanism is not detailed. Let's take a look at it in detail today

2, Capacity expansion mechanism

Let's start with the conclusion:

  • The capacity of hashmap is a multiple of 2, such as 2, 4, 8, 16, 32, 64
  • Each expansion is doubled, 2 to 4, 4 to 8, 8 to 16, 16 to 32, etc
  • Expansion factor: 0.75 by default, or a decimal point can be specified
  • Capacity expansion time point: when the number of elements in the container reaches: capacity * capacity expansion factor, start capacity expansion

3, Source code analysis

(1) Let's look at the constructor first

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
static final float DEFAULT_LOAD_FACTOR = 0.75f;

public HashMap() {
    this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}

The default constructor specifies the expansion factor: 0.75, and the default capacity is 16

public HashMap(int initialCapacity) {
    this(initialCapacity, DEFAULT_LOAD_FACTOR);
}

Specify the initial capacity. The default expansion factor is 0.75

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

Specify the initial capacity and expansion factor at the same time

/**
    * The next size value at which to resize (capacity * load factor).
    *
    * @serial
    */
int threshold;
  • Note this variable: the next value to be expanded, expansion capacity, capacity * expansion factor
  • Look at this sentence: 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;
}
  • This method is to take the multiple of 2 after rounding the given value, such as 3 - > 4, 15 - > 16, 27 - > 32
  • So far, the preparatory work has been done. Let's look at the put method

(2) put method

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;
    // ① At first, the table is null. Call the resize () method
    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;
    // ② At the end, judge whether the capacity is greater than the expanded capacity. If it is greater than the expanded capacity, call the resize method
    if (++size > threshold)
        resize();
    afterNodeInsertion(evict);
    return null;
}
  • ① At first, the table is null. Call the resize () method
  • ② At the end, judge whether the capacity is greater than the expanded capacity. If it is greater than the expanded capacity, call the resize () method
  • Look at the resize () method
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 {               // zero initial threshold signifies using defaults
        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;
}
  • First, analyze the first case: Map = new hashmap();
  • Take the last branch, with a capacity of 16 and an expansion capacity of 12
else {
    newCap = DEFAULT_INITIAL_CAPACITY;
    newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
  • Analyze the second case: Map map = new HashMap(20);
  • Take the second branch. As analyzed earlier, threshold = tableSizeFor(20) is 32
  • New capacity newcap = oldThr is 32
// capacity
else if (oldThr > 0) // initial capacity was placed in threshold
        newCap = oldThr;
  • New expansion capacity newThr = newCap * loadFactor is 24
// Expansion capacity
if (newThr == 0) {
    float ft = (float)newCap * loadFactor;
    newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                (int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
  • Analyze the third case: 24 elements have been inserted into the above map, and a new one needs to be expanded
  • Take the first branch, oldCap=32, oldThr=24
  • Capacity expansion: newcap = oldcap < < 1 = 64
  • Expansion capacity newthr = oldthr < < 1, 48
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
}
  • Finally, copy the elements to the new table
  • Direct copy of a single element
  • If it is a tree, call the copy method of the tree
  • If it is a linked list, copy the circular linked list

Welcome to WeChat official account: Feng Chi, more technical learning and sharing.

Topics: Java Back-end linked list