关于HashMap的两个小知识点

  1 在put的时候,使用的并不是原始的hash值

  而是高16位不变,低16位是 高十六位和低十六位异或

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

  2 做扩容的时候,高位链表和低位链表

  直接和oldCap做按位与操作 判断是不是等于0,等于0就是低位的。

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);
原文地址:https://www.cnblogs.com/juniorMa/p/13841682.html