当前位置:网站首页>解决哈希冲突的几种方式
解决哈希冲突的几种方式
2022-07-26 16:52:00 【斯沃福德】
1. 什么是hash冲突
哈希函数是一个映像,把任意长度的输入,通过Hash算法变换成固定长度的输出,这个输出就是Hash值;
当两个不同的输入,产生了同一个输出值即为哈希冲突
2. 解决方式
2.1 开放地址法
这种方法也称再散列法,其基本思想是:当关键字key的哈希地址p=H(key)出现冲突时,以p为基础,产生另一个哈希地址p1,如果p1仍然冲突,再以p为基础,产生另一个哈希地址p2,…,直到找出一个不冲突的哈希地址pi ,将相应元素存入其中。
线性探测法:在ThreadLocalMap中使用;
该方法一次探测下一个地址,直到有空的地址后插入,若整个空间都找不到空余的地址,则产生溢出;
优点:只要哈希表还有位置,通过不断的探测,总能找到合适的位置。
缺点:
1.是探测的次数不可控,一旦探测次数骤增,会严重影响哈希表的读写性能;
2.删除比较麻烦;
2.2 链地址法
将哈希码对应一个链表,插入元素时,如果哈希码冲突了,就将元素插入到链表,可选头插或尾插。
查询时,遍历哈希码对应的链表。
HashMap采用的就是这种方式。
优点:处理简单,容易删除,只需要简单地删去链表上相应的结点即可;
缺点:一旦哈希冲突多了,哈希表会退化成链表,查询效率会从O(1)变为O(n)。JDK8的HashMap针对这种情况有做优化,冲突超过8个会将链表转换为红黑树,提高查询效率。
2.3 再哈希法
采用哈希函数,而不是一个;
如果第一个哈希函数计算的哈希码发生冲突了,就采用第二个哈希函数重新计算哈希码,直到不冲突为止;
查询时也是一样,依次调用不同的哈希函数计算哈希码,直到Key相等。
int hash = hash1(key)、hash2(key)、hash3(key)......
缺点:这种方式会增加哈希计算的开销,影响读写的效率。
2.4 公共溢出区
在创建哈希表的同时,再额外创建一个公共溢出区,专门用来存放发生哈希冲突的元素。查找时,先从哈希表查,查不到再去公共溢出区查。
缺点:哈希冲突多了,公共溢出区会膨胀的非常厉害,查询的效率也有影响。
边栏推荐
- About the difference between BigDecimal conversion string toengineeringstring, toplainstring and toString
- Stop supporting Huawei! Signing 640billion 5g orders in Germany and 270billion 5g orders in the Middle East is pure nonsense!
- 03|实现 useReducer 和 useState
- 常用超好用正则表达式!
- 【机器学习】Mean Shift原理及代码
- CCS TM4C123新建工程
- Eureka Registry - from entry to application
- Relationship between standardization, normalization and regularization
- Week 4 Recurrent Neural Networks
- VIM visualization mode and its usage
猜你喜欢
![[virtual machine data recovery] data recovery cases in which XenServer virtual machine is unavailable due to accidental power failure and virtual disk files are lost](/img/99/e5404a09ec7f52a7c5d7be23e43e85.jpg)
[virtual machine data recovery] data recovery cases in which XenServer virtual machine is unavailable due to accidental power failure and virtual disk files are lost

【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用,虚拟磁盘文件丢失的数据恢复案例

Realizing DDD based on ABP -- related concepts of DDD

A detailed explanation of throughput, QPS, TPS, concurrency and other high concurrency indicators

树形dp问题

ASEMI整流桥KBPC2510,KBPC2510参数,KBPC2510规格书

About the adjustment of the game background, reading this article is enough

The principle of reliable transmission in TCP protocol

【OpenCV 例程 300篇】240. OpenCV 中的 Shi-Tomas 角点检测

得不偿失!博士骗领210万元、硕士骗领3万元人才补贴,全被判刑了!
随机推荐
Pytest (mind map)
Quickly build a development platform for enterprise applications
JS 闭包 模拟私有变量 面试题 立即执行函数IIFE
硬件开发与市场产业
The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry
环境搭建-MongoDB
2022 年有哪些流行的技术?
API for sellers -- description of the return value of adding baby API to Taobao / tmall sellers' stores
如何使用 align-regexp 对齐 userscript 元信息
In the first half of the year, sales increased by 10% against the trend. You can always trust Volvo, which is persistent and safe
性能调优bug层出不穷?这3份文档轻松搞定JVM调优
大家下午好,请教一个问题:如何从保存点启动一个之前以SQL提交的作业?问题描述:用SQL在cl
Implement softmax classification from zero sum using mxnet
重磅公布!ICML2022奖项:15篇杰出论文,复旦、厦大、上交大研究入选
uni-app
Cloud rendering volume cloud [theoretical basis and implementation scheme]
使用 Dired 快速移动文件
ASEMI整流桥KBPC2510,KBPC2510参数,KBPC2510规格书
2.1.2 synchronization always fails
How emqx 5.0 under the new architecture of mria+rlog realizes 100million mqtt connections