模拟hashmap,运行扩容流程

真实代码


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
                    //以e.hash & oldCap是否为0进行数据分割,将一个桶的数据分散到两个桶里。
                        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) {
                       //最后一个节点的next置空,防止死循环
                            loTail.next = null;
                        //此部分存储数据位置不变
                            newTab[j] = loHead;
                        }
                        if (hiTail != null) {
                        //最后一个节点的next置空,防止死循环
                            hiTail.next = null;
                            //此部分存储数据是从原来的桶切分出来的
                            //在新table中的位置为原位置+T[原表长度]
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
        return newTab;
    }

模拟代码

package action;

import java.util.Map;
import java.util.Objects;

public class TestClass<K,V>{


    static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        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; }

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

    public static void main(String[] args) {
        Node<String, String> loHead0 = new Node<String, String>(3303, "10", "10", null);
        Node<String, String> loHead1 = new Node<String, String>(3303, "20", "20", loHead0);
        Node<String, String> loHead2 = new Node<String, String>(3303, "30", "30", loHead1);
        Node<String, String> loHead3 = new Node<String, String>(3303, "40", "40", loHead2);
        Node<String, String> loHead4 = new Node<String, String>(3303, "50", "50", loHead3);
        Node<String, String> loHead5 = new Node<String, String>(3303, "60", "60", loHead4);

        Node<String, String>[] oldTab = (Node<String, String>[])new Node[16];
        oldTab[5] = loHead5;
        Node<String, String>[] newTab = (Node<String, String>[])new Node[32];

        for (int j = 0; j < oldTab.length ; ++j) {
            Node<String, String> e;
            if ((e = oldTab[j]) != null) {
                    Node<String, String> next;
                    Node<String, String> loHead = null, loTail = null;
                    Node<String, String> hiHead = null, hiTail = null;
                    boolean flag = true;
                    do {
                        next = loHead5.next;
                        if (flag) {
                            if (loTail == null)
                                loHead = loHead5;
                            else
                                loTail.next = loHead5;
                            loTail = loHead5;
                            flag = false;
                        }
                        else {
                            if (hiTail == null)
                                hiHead = loHead5;
                            else
                                hiTail.next = loHead5;
                            hiTail = loHead5;
                            flag = true;
                        }
                    } while ((loHead5 = next) != null);
                    if (loTail != null) {
                        loTail.next = null;
                        newTab[j] = loHead;
                    }
                    if (hiTail != null) {
                        hiTail.next = null;
                        newTab[j + oldTab.length] = hiHead;
                    }
                }
            }
        }

    }

扩容前:

这里写图片描述
扩容后:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值