hash map 中hash()方法分析
来源:互联网 发布:网络体系结构及作用 编辑:程序博客网 时间:2024/05/23 11:08
HashMap 中hash table 定位算法:
其中indexFor和hash源码如下:
indexFor这个方法论坛中已有人分析过,这里就不再分析。
现在分析一下hash算法:
假设key.hashCode()的值为:0x7FFFFFFF,table.length为默认值16。
上面算法执行如下:
得到i=15
其中h^(h>>>7)^(h>>>4) 结果中的位运行标识是把h>>>7 换成 h>>>8来看。
即最后h^(h>>>8)^(h>>>4) 运算后hashCode值每位数值如下:
8=8
7=7^8
6=6^7^8
5=5^8^7^6
4=4^7^6^5^8
3=3^8^6^5^8^4^7
2=2^7^5^4^7^3^8^6
1=1^6^4^3^8^6^2^7^5
结果中的1、2、3三位出现重复位^运算
3=3^8^6^5^8^4^7 -> 3^6^5^4^7
2=2^7^5^4^7^3^8^6 -> 2^5^4^3^8^6
1=1^6^4^3^8^6^2^7^5 -> 1^4^3^8^2^7^5
算法中是采用(h>>>7)而不是(h>>>8)的算法,应该是考虑1、2、3三位出现重复位^运算的情况。使得最低位上原hashCode的8位都参与了^运算,所以在table.length为默认值16的情况下面,hashCode任意位的变化基本都能反应到最终hash table 定位算法中,这种情况下只有原hashCode第3位高1位变化不会反应到结果中,即:0x7FFFF7FF的i=15。
- int hash = hash(key.hashCode());
- int i = indexFor(hash, table.length);
int hash = hash(key.hashCode()); int i = indexFor(hash, table.length);
其中indexFor和hash源码如下:
- /**
- * Applies a supplemental hash function to a given hashCode, which
- * defends against poor quality hash functions. This is critical
- * because HashMap uses power-of-two length hash tables, that
- * otherwise encounter collisions for hashCodes that do not differ
- * in lower bits. Note: Null keys always map to hash 0, thus index 0.
- */
- static int hash(int h) {
- // This function ensures that hashCodes that differ only by
- // constant multiples at each bit position have a bounded
- // number of collisions (approximately 8 at default load factor).
- h ^= (h >>> 20) ^ (h >>> 12);
- return h ^ (h >>> 7) ^ (h >>> 4);
- }
- /**
- * Returns index for hash code h.
- */
- static int indexFor(int h, int length) {
- return h & (length-1);
- }
/** * Applies a supplemental hash function to a given hashCode, which * defends against poor quality hash functions. This is critical * because HashMap uses power-of-two length hash tables, that * otherwise encounter collisions for hashCodes that do not differ * in lower bits. Note: Null keys always map to hash 0, thus index 0. */ static int hash(int h) { // This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); } /** * Returns index for hash code h. */ static int indexFor(int h, int length) { return h & (length-1); }
indexFor这个方法论坛中已有人分析过,这里就不再分析。
现在分析一下hash算法:
- h ^= (h >>> 20) ^ (h >>> 12);
- return h ^ (h >>> 7) ^ (h >>> 4);
h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4);
假设key.hashCode()的值为:0x7FFFFFFF,table.length为默认值16。
上面算法执行如下:
得到i=15
其中h^(h>>>7)^(h>>>4) 结果中的位运行标识是把h>>>7 换成 h>>>8来看。
即最后h^(h>>>8)^(h>>>4) 运算后hashCode值每位数值如下:
8=8
7=7^8
6=6^7^8
5=5^8^7^6
4=4^7^6^5^8
3=3^8^6^5^8^4^7
2=2^7^5^4^7^3^8^6
1=1^6^4^3^8^6^2^7^5
结果中的1、2、3三位出现重复位^运算
3=3^8^6^5^8^4^7 -> 3^6^5^4^7
2=2^7^5^4^7^3^8^6 -> 2^5^4^3^8^6
1=1^6^4^3^8^6^2^7^5 -> 1^4^3^8^2^7^5
算法中是采用(h>>>7)而不是(h>>>8)的算法,应该是考虑1、2、3三位出现重复位^运算的情况。使得最低位上原hashCode的8位都参与了^运算,所以在table.length为默认值16的情况下面,hashCode任意位的变化基本都能反应到最终hash table 定位算法中,这种情况下只有原hashCode第3位高1位变化不会反应到结果中,即:0x7FFFF7FF的i=15。
- hash map 中hash()方法分析
- java hash map深度分析(equal,hashcode,hash)
- HashMap hash方法分析
- HashMap hash方法分析
- Hash Map / Hash Set
- hash map
- 比较分析ArrayList,Vector以及Hash Map,Hash Table
- STL中Map 与Hash Map
- Java HashMap中hash方法的背景及分析
- Hash桶 map的遍历方法
- map 容器小例(hash map)
- Hash分析
- Hash分析
- Ruby中Hash常用方法
- C++中hash map的用法
- hash 方法
- 算法 哈希(hash——map)
- vua 10282 - Babelfish(Hash、map)
- iwcコピー時計の精致な石英の腕時計を全部で
- 关于EditText获取焦点,隐藏软键盘的问题
- android 实现由下至上弹出并位于屏幕底部的提示框
- 给定某年某月,输出十天后是几年几月几日
- 2013.11.20
- hash map 中hash()方法分析
- 从0到9985中的所有1
- java中关键字volatile的作用
- PHP调试工具Xdebug安装配置教程
- 有30个学生,每个学生有5门课,求其总成绩,和平均成绩
- activiti-接口1
- NSConnection的使用
- 链表建立输出删除插入的综合实例
- Linux下读写FLASH驱动——MTD设备分析