当前位置:网站首页>Several ways to resolve hash conflicts
Several ways to resolve hash conflicts
2022-07-26 17:47:00 【Swarford】
Hash Collisions
1. What is? hash Conflict
The hash function is an image , Put any length of input , adopt Hash The algorithm is transformed into a fixed length output , This output is Hash value ;
When two different inputs , If the same output value is generated, it is a hash conflict
2. Solution
2.1 Open address method
This method is also called rehash , The basic idea is this : When keyword key The hash address of p=H(key) When there is a conflict , With p Based on , Generates another hash address p1, If p1 Still conflict , And then to p Based on , Generates another hash address p2,…, Until you find a hash address that doesn't conflict pi , Store the corresponding element in it .
Linear detection method : stay ThreadLocalMap Use in ;
This method detects the next address at a time , Insert until you have a free address , If the whole space can't find a spare address , Then there is overflow ;
advantage : As long as the hash table has a place , Through constant detection , Always find the right place .
shortcoming :
1. The number of detections is uncontrollable , Once the number of detections increases dramatically , It will seriously affect the reading and writing performance of the hash table ;
2. Deleting is more cumbersome ;
2.2 Chain address
The hash code corresponds to a linked list , When inserting elements , If the hash code conflicts , Insert the element into the linked list , It can be inserted head or tail .
When inquiring , Traverse the linked list corresponding to the hash code .
HashMap That's how it works .
advantage : Simple processing , Easy to delete , Simply delete the corresponding node on the linked list ;
shortcoming : Once there are many hash conflicts , The hash table will degenerate into a linked list , Query efficiency will increase from O(1) Turn into O(n).JDK8 Of HashMap In view of this situation, we have done optimization , Conflict exceeds 8 Will convert the linked list into Red and black trees , Improve query efficiency .
2.3 Then the hash method
Use hash function , Instead of a ;
If the hash code calculated by the first hash function conflicts , Use the second hash function to recalculate the hash code , Until there is no conflict ;
The same is true when querying , Call different hash functions in turn to calculate hash code , until Key equal .
int hash = hash1(key)、hash2(key)、hash3(key)......
shortcoming : This method will increase the overhead of hash calculation , Affect the efficiency of reading and writing .
2.4 The public overflow area
While creating the hash table , Create an additional public overflow area , It is specially used to store elements with hash conflicts . When looking for , Look up the hash table first , If you can't find it, go to the public overflow area .
shortcoming : There are many hash conflicts , The public overflow area will swell very much , Query efficiency also has an impact .
边栏推荐
- 常用超好用正则表达式!
- JS function scope variables declare that the variables that promote the scope chain without VaR are global variables
- Leetcode:1206. design jump table [jump table board]
- 如何组装一个注册中心?
- Tree DP problem
- AI遮天传 DL-多层感知机
- 办公软件常用快捷键大全
- Heavy announcement! Icml2022 Awards: 15 outstanding papers, selected by Fudan University, Xiamen University and Shanghai Jiaotong University
- 二层管理型交换机如何设置IP
- How to use align regexp to align userscript meta information
猜你喜欢

【云原生】 iVX 低代码开发 引入腾讯地图并在线预览

JS 闭包 模拟私有变量 面试题 立即执行函数IIFE

【集训Day3】Reconstruction of roads

Machine learning - what are machine learning, supervised learning, and unsupervised learning

【模板】线段树 1

The latest interface of Taobao / tmall keyword search

The diagram of user login verification process is well written!

ACL实验演示(Huawei路由器设备配置)

Asemi rectifier bridge kbpc2510, kbpc2510 parameters, kbpc2510 specifications

ACL experiment demonstration (Huawei router device configuration)
随机推荐
Oracle is slow to perform a large number of DML operations. Is it the problem of CPU or hard disk?
Heavy! The 2022 China open source development blue book was officially released
Ascend目标检测与识别-定制自己的AI应用
AI遮天传 DL-回归与分类
ASEMI整流桥KBPC3510,KBPC3510封装,KBPC3510应用
性能调优bug层出不穷?这3份文档轻松搞定JVM调优
JS 闭包 模拟私有变量 面试题 立即执行函数IIFE
AI遮天传 ML-无监督学习
uni-app
Detailed explanation of openwrt's feeds.conf.default
Mondriaans‘s Dream(状态压缩DP)
kaggle猫狗数据集开源——用于经典CNN分类实战
Performance tuning bugs emerge in endlessly? These three documents can easily handle JVM tuning
数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
Machine learning - what are machine learning, supervised learning, and unsupervised learning
浅析接口测试
云渲染-体积云【理论基础与实现方案】
【集训Day2】Torchbearer
[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
2.1.2 synchronization always fails