在WeakHashMap和HashMap中使用了不同的哈希函数

WeakHashMap

/*** Retrieve object hash code and applies a supplemental hash function to the* result hash, 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.*/
final int hash(Object k) {int h = k.hashCode();// 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);
}

HashMap

/*** Computes key.hashCode() and spreads (XORs) higher bits of hash* to lower.  Because the table uses power-of-two masking, sets of* hashes that vary only in bits above the current mask will* always collide. (Among known examples are sets of Float keys* holding consecutive whole numbers in small tables.)  So we* apply a transform that spreads the impact of higher bits* downward. There is a tradeoff between speed, utility, and* quality of bit-spreading. Because many common sets of hashes* are already reasonably distributed (so don't benefit from* spreading), and because we use trees to handle large sets of* collisions in bins, we just XOR some shifted bits in the* cheapest possible way to reduce systematic lossage, as well as* to incorporate impact of the highest bits that would otherwise* never be used in index calculations because of table bounds.*/
static final int hash(Object key) {int h;return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

如何设计哈希

找了很久才发现没有银弹,哈希方法要考虑分散,性能,安全等方面。这篇文章给了很多启发

http://ticki.github.io/blog/designing-a-good-non-cryptographic-hash-function/

尤其是里面的坐标图,说明设计的哈希函数可以用坐标图的方式验证分散性好不好。

为什么是异或

常见的map中的哈希函数一般都少不了移位操作和异或操作。为什么偏爱异或?

Assuming uniformly random (1-bit) inputs, the AND function output probability distribution is 75% 0and 25% 1. Conversely, OR is 25% 0 and 75% 1.

The XOR function is 50% 0 and 50% 1, therefore it is good for combining uniform probability distributions.

This can be seen by writing out truth tables:

 a | b | a AND b
---+---+--------0 | 0 |    00 | 1 |    01 | 0 |    01 | 1 |    1a | b | a OR b
---+---+--------0 | 0 |    00 | 1 |    11 | 0 |    11 | 1 |    1a | b | a XOR b
---+---+--------0 | 0 |    00 | 1 |    11 | 0 |    11 | 1 |    0

Exercise: How many logical functions of two 1-bit inputs a and b have this uniform output distribution? Why is XOR the most suitable for the purpose stated in your question?

为什么table的长度是2的指

Map的底层实现一般都是数组,为什么要求数组的长度是2的倍数?长度是2的倍数,长度 - 1 可以得到全1的二进制数,

再与hash值进行"与"运算,得到该值在数组中的位置。

/*** Returns index for hash code h.*/
private static int indexFor(int h, int length) {return h & (length-1);
}

如何设计hash函数相关推荐

  1. hash函数查找和ASL计算

    Hash表的"查找成功的ASL"和"查找不成功的ASL" ASL指的是 平均查找时间 关键字序列:(7.8.30.11.18.9.14) 散列函数:  H(Ke ...

  2. 信息摘要函数(Hash函数)的设计与性质验证

    1.信息摘要函数(Hash函数)的设计与性质验证实验 2.实验目的:信息摘要函数(Hash函数)的设计与性质验证. 2.1实验设备:PC机 一台/人 2.2实验原理: 2.2.1.信息摘要函数具有固定 ...

  3. HashMap中的hash函数

    在写一个HashSet时候有个需求,是判断HashSet中是否已经存在对象,存在则取出,不存在则add添加.HashSet也是通过HashMap实现,只用了HashMap的key,value都存储一个 ...

  4. 文本去重之MinHash算法——就是多个hash函数对items计算特征值,然后取最小的计算相似度...

    来源:http://my.oschina.net/pathenon/blog/65210 1.概述 跟SimHash一样,MinHash也是LSH的一种,可以用来快速估算两个集合的相似度.MinHas ...

  5. Cuckoo hash算法分析——其根本思想和bloom filter一致 增加hash函数来解决碰撞 节省了空间但代价是查找次数增加...

    基本思想: cuckoo hash是一种解决hash冲突的方法,其目的是使用简单的hash 函数来提高hash table的利用率,同时保证O(1)的查询时间 基本思想是使用2个hash函数来处理碰撞 ...

  6. Hash函数及其应用

    本文部分内容摘自网络,参考资料链接会在文后给出,在此感谢原作者的分享. 计算理论中,没有Hash函数的说法,只有单向函数的说法.所谓的单向函数,是一个复杂的定义,大家可以去看计算理论或者密码学方面的数 ...

  7. 密码学基础知识(六)Hash函数与消息认证

    Hash函数和消息认证 先说Hash 哈希函数,可以将任意长度的消息压缩为某一固定长度的消息摘要函数.一句话,Hash简直了. 当然有逆天的一面就有大缺点,过程不可逆.傻了吧,哈哈. Hash的性质: ...

  8. 密码学hash函数-SHA256-512

    Hash函数又称哈希函数.散列函数.杂凑函数.它是一种单向密码体制,即从一个从明文到密文的不可逆映射,只有加密过程,没有解密过程. Hash函数H将可变长度的数据块M作为输入,产生固定长度的Hash值 ...

  9. hash函数的简单介绍

    HASH函数 应用Hash函数 作者:冲处宇宙 时间:2007.1.25 计算理论中,没有Hash函数的说法,只有单向函数的说法.所谓的单向函数,是一个复杂的定义,大家可以去看计算理论或者密码学方面的 ...

最新文章

  1. linux清屏命令_linux下的7个常用命令的基本使用
  2. Windows MySQL8.0安装出错解决方案(Start Server 失败)
  3. 通过反射获取DLL的类实现加载窗体
  4. SpringBoot 基础上传操作
  5. 正则不以什么开头_python基础 | 正则扫盲
  6. PostgreSQL查看版本信息
  7. simulink和psim仿真结果不同_(格麟倍)航空航天零件硬铬电镀工艺专业仿真评估工具...
  8. notepad怎么设置python为环境变量_notepad
  9. 串行舵机/数字舵机的替代方案,低成本的舵机级联方案。数字舵机的驱动芯片。普通舵机改数字舵机
  10. Java LRU的实现
  11. 【渝粤教育】国家开放大学2018年秋季 0708-22T互联网创业基础 参考试题
  12. 代码打包机 php,我想问问我这写的php 代码到底错哪了!
  13. 销售订单批量导入(1)
  14. Google Play 开发者账户被封
  15. CORS手机测试软件,司南导航RTK手簿软件测量大师连接千寻cors账号进行测量的方法教程...
  16. python hist2d_matplotlib可视化之hist直方图
  17. SQL中常用的字符串LEFT函数和RIGHT函数详解
  18. 【学习摘记】马士兵HTML CSS_课时4-5_表格和表单
  19. 我们如何获取信息,组织知识
  20. 淘宝吱口令效果实现,获取系统剪贴板内容

热门文章

  1. Apache之搭建静态网站
  2. IllegalArgumentException :argument type mismatch
  3. spring boot 多模块项目打包部署
  4. 利用huffman编码对文本文件进行压缩与解压(java实现)
  5. 华为RH2288H V3服务器raid配置
  6. java连接redis存取数据(详细)
  7. 人工智能机器人专业就业前景怎样
  8. APP逆向案例之(一)过 app 更新提示
  9. MySQL删除注册表的卸载方式
  10. 利用sql profile固定执行计划加快OGG同步