HashMap-数组+链表集合

field

常量
    //默认初始化容量,最好为2的幂
    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;
    //由哈希冲突的链表结构转为平衡二叉树结构节点数阈值(桶的数量需要大于MIN_TREEIFY_CAPACITY )
    static final int TREEIFY_THRESHOLD = 8;
    //恢复为链表的阈值
    static final int UNTREEIFY_THRESHOLD = 6;
    //TREEIFY_THRESHOLD  对应需要的桶数量
    static final int MIN_TREEIFY_CAPACITY = 64;
变量
    //hash节点数组
    transient Node<K,V>[] table;
    //元素节点
    transient Set<Map.Entry<K,V>> entrySet;
    //大小
    transient int size;
    //操作数
    transient int modCount;
    //扩容临界值
    int threshold;
    //加载因子
    final float loadFactor;

method

tableSizeFor
//相当机智的算法,用来固定容量为2的倍数
 static final int tableSizeFor(int cap) {  //10000
        int n = cap - 1;   //可能初始化就为2的倍数,则减去1   1111
        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;
    }
hash
//高16位于hashcode的低16位 异或取值,保证高16位和低16位的变化同时影响hash值
static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
resize
//扩容操作,将旧hash表数据移到新的hash表
 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);
        }
        //计算新的threshold
        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;
        //将旧hash表移到新的hash表
        if (oldTab != null) {
            for (int j = 0; j < oldCap; ++j) {
                Node<K,V> e;
                //如果当前旧节点不为空的情况下
                if ((e = oldTab[j]) != null) {
                    oldTab[j] = null;
                    //如果该节点没有hash冲突,是单节点
                    if (e.next == null)
                        //直接将hash值与桶的容量与运算求桶的索引位。
                        newTab[e.hash & (newCap - 1)] = e;
                    else if (e instanceof TreeNode)
                        //如果e是平衡树节点,则添加到平衡树中
                        ((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;
                            //与原来的容量做与运算
                                //只有两种结果: 0 或 oldCap(2的幂,这是必然的)
                               //这是hash本来就小于oldCap的情况
                            if ((e.hash & oldCap) == 0) {
                                //将剩下的节点逐个copy
                                if (loTail == null)
                                    loHead = e;
                                else
                                    loTail.next = e;
                                loTail = e;
                            }
                            //这是hash本来就大于oldCap的情况
                            else {
                                //将剩下的节点逐个copy
                                if (hiTail == null)
                                    hiHead = e;
                                else
                                    hiTail.next = e;
                                hiTail = e;
                            }
                            //多线程条件下可能反正死循环
                            //循环列表
                        } while ((e = next) != null);
                        //小于oldCap的索引,如果有节点数据,则保持不变
                        if (loTail != null) {
                            loTail.next = null;
                            newTab[j] = loHead;
                        }
                        //大于oldCap的索引则,如果有节点数据,在原基础上加上oldCap
                        if (hiTail != null) {
                            hiTail.next = null;
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
        return newTab;
    }
putVal
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        //如果tab为空或者长度为0,则初始化
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
         //如果桶中计算出的索引无hash冲突,则直接添加
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
        //具有hash冲突
            Node<K,V> e; K k;
            //如果hash值,key值都相同,则覆盖
            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) {
                    //如果p的下一个元素为null,则将元素添加到P后
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        //到达节点数阈值,则转变为红黑树
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    //如果有重复key,则覆盖
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            //e 不为null ,则表示已存在相同的key
            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;
    }
getNode
final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        //check 是否含有该元素
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            //检查链表或者二叉树第一个元素
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
                //该节点不在第一个,开始循环检查
            if ((e = first.next) != null) {
                //红黑树的情况
                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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值