When the length of JDK1.8 HashMap link list is greater than 8, it will be converted to red black tree

——Light Rose appears frequently, pure aloe blossoms cautiously. May the motherland overcome the epidemic as soon as possible! Go China! Come on, Wuhan!

1. JDK1.8 HashMap source code

/**
     * Implements Map.put and related methods
     *
     * @param hash hash for key
     * @param key the key
     * @param value the value to put
     * @param onlyIfAbsent if true, don't change existing value
     * @param evict if false, the table is in creation mode.
     * @return previous value, or null if none
     */
    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. When bincount > = 7, it means that the length of the linked list under the current array node has reached at least 8. In this case, you need to convert the linked list to red black tree storage.

Why to convert when the chain length is 8

1. The first reason:

        

Default = 1 < 4 array default length is 16

Default? Load? Factor = 0.75f the expansion factor is 0.75

That is, when the array storage element > = 16 * 0.75 = 12, you need to start expanding.

However, according to the subscript calculated by hash() function, the probability of the number of table[i] buckets is:

Number of occurrences at the same location probability
0     0.60653066
1     0.30326533
2     0.07581633
3     0.01263606
4     0.00157952
5     0.00015795
6     0.00001316
7     0.00000094
8     0.00000006

According to the Poisson distribution, when there are eight elements in a position, it is considered that the hash () function is not well designed. It will automatically turn into a red black tree to improve performance.

2. The second reason:

Compare the difference between a linked list and a red black tree:

Linked list

    static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;
}

Mangrove

    static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
        TreeNode<K,V> parent;  // red-black tree links
        TreeNode<K,V> left;
        TreeNode<K,V> right;
        TreeNode<K,V> prev;    // needed to unlink next upon deletion
        boolean red;
}

In terms of spatial complexity: red black tree is twice as complex as linked list. (because of other redundant information, the linked list has only one next attribute, while the red black tree has two left and right attributes.).

In terms of time complexity:

Node number Time complexity of linked list Red black tree time complexity Time complexity and space complexity of linked list Red black tree time complexity combined with space complexity
1 1 1 1 2
2 2 2 2 4
3 3 2 3 4
4 4 3 4 6
5 5 3 5 6
6 6 4 6 8
7 7 4 7 8
8 8 4 8 8
9 9 4 9 8

Premise: the spatial complexity of the red black tree is twice that of the linked list.          

Description:

1. When the number of linked lists on table[i] is less than 8, the efficiency of spatial complexity combined with time complexity is better than that of red black tree.

2. When the number = 8, the efficiency of the linked list is the same as that of the red black tree.

3. When the number is more than 8, the efficiency of red black tree is better than that of linked list.

Three conclusion

So when the length of the linked list is more than 8, the bottom layer of Hash Map will convert the linked list into a red black tree to improve the efficiency.

46 original articles published, 79 praised, 160000 visitors+
Private letter follow

Tags: Attribute less

Posted on Thu, 20 Feb 2020 04:44:02 -0500 by zachatk1