jdk1.8 hashMap 分析

1.  hash函数

 static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

重新hash,利用高16位和低16位进行异或运算来重新的到hash值,这样就能把高16位的信息也融入到hash里面来,更加均匀分布

2. put方法

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;  //第一次put必须调用resize对table数组初始化
        if ((p = tab[i = (n - 1) & hash]) == null)  //hash位置为空的直接放到该位置
            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;   //若hash位置有值并且跟当前put的key是相同的,则先标记然后最后替换val就行
            else if (p instanceof TreeNode) //若hash位置是树节点,则说明已经是红黑树了,直接按照红黑树的方式方添加。
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else { //说明还不是红黑树,还是链表结构,则遍历链表,若找到相同的key则替换val,若未找到则加入链尾,添加完后判断是否超过红黑树阈值,超过则将链表转成红黑树。
                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))))  //找到相同key
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;  //添加相同key,则替换val
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e); //linkhashmap结构使用,如果设置了ACCESSORDER则是LRU淘汰算法。
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold) //扩容判断
            resize();  //扩容
        afterNodeInsertion(evict);  //linkhashmap使用
        return null;
    }

3. putTreeVal 红黑树节点添加

 final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
                                       int h, K k, V v) {
            Class<?> kc = null;
            boolean searched = false;
            TreeNode<K,V> root = (parent != null) ? root() : this;
//红黑树也是个二叉排序树,所以先找要插入的父节点。
            for (TreeNode<K,V> p = root;;) { //循环查找
                int dir, ph; K pk;
                if ((ph = p.hash) > h)
                    dir = -1;
                else if (ph < h)
                    dir = 1;
                else if ((pk = p.key) == k || (k != null && k.equals(pk)))
                    return p;
                else if ((kc == null &&
                          (kc = comparableClassFor(k)) == null) ||
                         (dir = compareComparables(kc, k, pk)) == 0) {
                    if (!searched) { //如果比较大小相同的话,则有可能是同一个key,所以搜索当前插入key是否存在于当前红黑树中。只查找一次
                        TreeNode<K,V> q, ch;
                        searched = true;
                        if (((ch = p.left) != null &&
                             (q = ch.find(h, k, kc)) != null) ||
                            ((ch = p.right) != null &&
                             (q = ch.find(h, k, kc)) != null))
                            return q;
                    }
                    dir = tieBreakOrder(k, pk);
                }

                TreeNode
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值