Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于HashMap文章中的一个小建议 #25

Open
JachinDu opened this issue Mar 21, 2020 · 1 comment
Open

关于HashMap文章中的一个小建议 #25

JachinDu opened this issue Mar 21, 2020 · 1 comment

Comments

@JachinDu
Copy link

@JachinDu JachinDu commented Mar 21, 2020

丙哥,读了你的HashMap.md感觉收获满满,不过有两个地方我想提一下:

  1. 文中 “因为在使用不是2的幂的数字的时候,Length-1的值是所有二进制位全为1,这种情况下,index的结果等同于HashCode后几位的值。”一句中应该是“是2的幂”,Length-1才会全是1吧。
  2. “只要输入的HashCode本身分布均匀,Hash算法的结果就是均匀的。” 这里我觉得可以加上HashMap源码中对hashCode值的一个操作。
    源码中对key的hash方法:
static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

其中将原32位hashcode与其右移16位后的结果做了异或,为了让高16位也参与到“让hashcode分布更均匀”中,这样更能让hashcode分布均匀吧。
(第一次提issue,如有不妥,望丙哥告知🤓

@Dcwjh
Copy link

@Dcwjh Dcwjh commented Jun 28, 2020

丙哥,读了你的HashMap.md感觉收获满满,不过有两个地方我想提一下:

  1. 文中 “因为在使用不是2的幂的数字的时候,Length-1的值是所有二进制位全为1,这种情况下,index的结果等同于HashCode后几位的值。”一句中应该是“是2的幂”,Length-1才会全是1吧。
  2. “只要输入的HashCode本身分布均匀,Hash算法的结果就是均匀的。” 这里我觉得可以加上HashMap源码中对hashCode值的一个操作。
    源码中对key的hash方法:
static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

其中将原32位hashcode与其右移16位后的结果做了异或,为了让高16位也参与到“让hashcode分布更均匀”中,这样更能让hashcode分布均匀吧。
(第一次提issue,如有不妥,望丙哥告知🤓

好巧,我也注意到这个点了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.