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

1、计算出key值的hashcode;
2、h = key.hashCode()) ^ (h >>> 16)可以理解为hashcode高16位右移,作为低16位,与hashcode进行异或^运算,;

public class MainClass<K, V> extends HashMap<K, V> {public static void main(String[] args) throws ExecutionException, InterruptedException {Float a = 123.37f;Float b = 123.36f;Integer c = 1;Integer d = 2;System.out.println(a.hashCode());System.out.println(b.hashCode());System.out.println(c.hashCode());System.out.println(d.hashCode());Student student = new Student();student.setName("123");Student student2 = new Student();student2.setName("123");student2.setAge(1);System.out.println(student.hashCode());System.out.println(student2.hashCode());System.out.println(new Student().hashCode());int i = student.hashCode();int i1 = i >>> 16;System.out.println(i);System.out.println(i1);System.out.println(i ^ i1);System.out.println(i & 15);System.out.println((i ^ i1) & 3);int i2 = student2.hashCode();int i3 = i2 >>> 16;System.out.println(i2);System.out.println(i3);System.out.println(i2 & 3);System.out.println((i2 ^ i3) & 3);}
}

1123466609
1123465298
1
2
-1648875293
-1648875335
1244954382
-1648875293
40376
-1648839333
3
3
-1648875335
40376
1
1

3、代码运行发现,经过第2步运算后,得到的hash值高位不会变,低位会有小的变化。网上说高位参与运算后,得到的hash值会更加散列。

4、由于会有tab[i = (n - 1) & hash]操作,我们的n始终比较小,hash的高16位几乎不可能参与运算,所以采用第2步操作;

5、第4步操作,可以看出为什么map的capacity应该是2的幂,因为2的幂减1后,转成二进制,所有位都是1111111,这样进行&运算后,可以保证得到的结果,也就是table数组的下标会更加散列。

HashMap 计算key的hash值方法hash()相关推荐

  1. 计算文件的hash值方法 | 使用powershell 以及 使用python

    使用windows的powershell进行get-filehash命令计算 使用python的hashlib库,进行文件的hash运算

  2. 图像相似度计算之哈希值方法OpenCV实现

    感知哈希算法(perceptual hash algorithm),它的作用是对每张图像生成一个"指纹"(fingerprint)字符串,然后比较不同图像的指纹.结果越接近,就说明 ...

  3. 存储 dict 的元素前是计算 key 的 hash 值?

    dict 的高性能与其存储方式是分不开的,我们知道 dict 的存储是基于哈希表(又称散列表),需要计算 hash 值,那么是计算谁的 hash 值呢?是像别人说的:存储 dict 元素前计算 key ...

  4. Hash(哈希)简述 —— Hash函数、Hash值、HashTable、HashMap

    总览 - Hash(哈希.散列) Hash是一种 散列函数或方法 的统称. · 该方法就是:把任意长度的输入通过散列算法变换成固定长度的输出,该输出就是散列值.-- (散列方法) · 这种转换是一种压 ...

  5. day15:hash值、Set(hashCode(),equals())、HashSet、LinkedHashSet、TreeSet、Map(keySet,entry(get.set)、HashMap

    一 回顾 1.集合的分类:单列集合 Collection 双列集合 Map 2.Collection 四大all的方法 3.List 特点: 有序 有索引 可以重复四个带索引操作的方法 4.Array ...

  6. HashMap中Entry以及Hash值的理解

    HashMap的底层结构如下: HashMap的底层结构是数组+链表 数组: HashMap以键值对存储数据,其中Key-Value都是Map.Entry中的属性.数组的值对应Value值,数组的下标 ...

  7. 【C++】字符串的 hash 值计算

      C++ 11 中新加入的容器 unordered_map 和 unordered_set 底层都是哈希表实现的,那么对于内置类型,肯定是可以自动计算出 hash 值的,但是对于像 pair< ...

  8. ruby hash方法_Ruby中带有示例的Hash.invert方法

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

  9. as_hash ruby_Ruby中带有示例的Hash.each_pair方法

    as_hash ruby Hash.each_pair方法 (Hash.each_pair Method) In this article, we will study about Hash.each ...

最新文章

  1. VTK:小部件之ImageTracerWidgetInsideContour
  2. Spring之HelloWorld
  3. jq动态获取input的值传给html,jquery 保证html()拿到的html字符串包含input的value值
  4. 【公众号系列】SAP的新零售
  5. c语言程序设计指针教学,C语言程序设计中指针教学要点分析
  6. 还香吗?iPhone 13/Pro 手机“失灵”:不支持通过 Apple Watch 解锁屏幕
  7. [Translation]Silverlight 4-MVVM with Commanding and WCF RIA Services
  8. 【翻译】Motion Blur for mobile devices in Unity
  9. 2.4.PHP7.1 狐教程-【PHP常量】
  10. 基于生物特征密钥生成研究 ------应用于区块链领域密钥的生成办法
  11. 02 CODESYS应用基础之ST语言语法基础
  12. 计算机注册表命令,进入注册表的方法和命令(电脑的注册表怎么打开)
  13. 电脑蓝屏,问题:你的电脑未正确启动,按“重启”以重启你的电脑,有时这样可以解决问题,你还可以按“高级选项”,尝试使用其他选项修复你的电脑
  14. 小手一敲,让JS Map现原形
  15. iOS越狱过程:越狱工具做了什么事情?( iOS系统结构、常见的二进制文件类型)
  16. 舞伴配对问题(顺序队列)
  17. [渝粤教育] 西南科技大学 投资项目评估 在线考试复习资料
  18. 照片转化成二维图片怎么做?图片内容如何生成二维码?
  19. 022 PHP获取从周一到周日的时间戳
  20. ADV-166 聪明的美食家

热门文章

  1. 得数据者得天下!作为后端开发必备技能之一的MySQL,这份十多年经验总结的应用实战与性能调优我想你肯定是需要的!
  2. Rosalind Java| Counting DNA Nucleotides
  3. Nexus 07 制品库制品晋级实践
  4. 华为微认证——鲲鹏处理器
  5. Javascript实现扫雷游戏
  6. 【NLP】DSSM深度结构化语义模型原理
  7. 机器学习cae_智铸超云——适创科技为压铸企业量身定制的专业CAE云平台
  8. BZOJ2277: [Poi2011]Strongbox
  9. 手机如何修改MD5视频?修改MD5视频的办法你清楚吗?
  10. R语言用GAM广义相加模型研究公交专用道对行程时间变异度数据的影响