Android HashMap的resize方法中(e.hash & oldCap) == 0

1、当(e.hash & oldCap) == 0时,我们看看下面这两个表达式会有什么规律:

e.hash & (oldCap-1) 和 e.hash & (2oldCap-1)

        e.hash & (oldCap-1) 表示老的节点数组中,节点的下标位置;

        e.hash & (2oldCap-1) 表示在新的节点数组中,新节点的下标位置;

当e.hash & oldCap == 0,hashMap中,oldCap默认是16;所以,e.hash & 16 = 0即:

e.hash & 10000 = 0

要想等式成立,那么e.hash的倒数第五值肯定是0,其他的位,可以是任意值,即:

e.hash的后五位肯定是:...0 xxxx

这时我们先看e.hash & (oldCap-1) 

                =    e.hash & (16-1)

                =    e.hash & (15)

                =    e.hash & 0 1111

我们再看e.hash & (2oldCap -1 )

              =   e.hash & (2*16 -1)

              =   e.hash & 31

              =   e.hash & 01 1111

因为 e.hash的后五位是:0 xxxx  ,所以e.hash & 01 1111的结果,倒数第五位肯定是0,

所以结果可以写成: e.hash & 00 1111 而它和 e.hash & 0 1111是一致的

所以 它就等于e.hash & (oldCap-1) 

所以当(e.hash & oldCap) == 0时,e.hash & (oldCap-1) 和e.hash & (2oldCap -1 )相等;即,它们的下标数值是相等的;

猜你喜欢

转载自blog.csdn.net/jian11058/article/details/123087053