Hash表与Hash冲突
Hash表
定义:
哈希表hashtable(key,value) 就是把Key通过一个固定的算法函数既所谓的哈希函数转换成一个整型数字,然后就将该数字对数组长度进行取余,取余结果就当作数组的下标,将value存储在以该数字为下标的数组空间里。(或者:把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。)
而当使用哈希表进行查询的时候,就是再次使用哈希函数将key转换为对应的数组下标,并定位到该空间获取value,如此一来,就可以充分利用到数组的定位性能进行数据定位。
组成
在jdk1.8之前,Hash表时由数组+链表组成,jdk1.8之后,为了解决Hash冲突情况下查询速度慢的问题,当Hash的子节点超过8个的时候,会自动的将链表转换为红黑树,当子节点小于6的时候,又会将红黑树转换为链表。
几种数据结构的特点
数组:查询快,增删慢
链表:查询慢,增删快
红黑树:查询速度非常快
Hash冲突
Hash表插入数据过程:
通过一段伪代码来理解过程:
根据键(Key)而直接访问在内存存储位置的[数据结构]
- 实体类
public class Info {
private String key;
private String name;
}
- Hash表
public class HashTable {
private Info[] arr;
public HashTable() {
arr = new Info[100];
}
public HashTable(int maxSize) {
arr = new Info[maxSize];
}
}
- 计算key的Hash值
public int hashCode(String key) {
/**
* 这种方式进行转换,如果key='abc' || 'acb' || 'cba'
* 那么这三个不同的key会计算出相同的hashCode值,造成数据的被覆盖!
*/
// int hashVal = 0;
// for (int i = key.length() - 1; i >= 0; i--) {
// int letter = key.charAt(i) - 96;
// hashVal += letter;
// }
// return hashVal;
BigInteger hashVal = new BigInteger("0");
BigInteger pow27 = new BigInteger("1");
for (int i = key.length() - 1; i >= 0; i--) {
int letter = key.charAt(i) - 96;
BigInteger letterB = new BigInteger(String.valueOf(letter));
hashVal = hashVal.add(letterB.multiply(pow27));
pow27 = pow27.multiply(new BigInteger(String.valueOf(27)));
}
return hashVal.mod(new BigInteger((String.valueOf(arr.length)))).intValue();
}
- 插入数据
public void insert(Info info) {
arr[hashCode(info.getKey())] = info;
}
*查询数据
public Info find(String key) {
return arr[hashCode(key)];
}
产生Hash冲突的原因
当存入的两个值的key,经过Hash计算后,等到的是相同的Hash值时,就会产生Hash冲突。
我们常见的“通话”,“重地”两个 值作为key计算后,得到的 Hash值就是相同的,便产生了Hash冲突。
无论设置的存储区域(n)有多大,当需要存储的数据大于 n 时,那么必然会存在哈希值相同的情况。这就是所谓的Hash冲突(又称为散列冲突)。
解决Hash冲突的办法
- 开放地址法:
当添加数据发生冲突时,寻找一个空位,并将数据插入
- 添加元素
public void insert(Info info) {
//获得关键字的Hash值
int hashCode = hashCode(info.getKey());
//如果当前的位置已经占用,并且值不为空
while (arr[hashCode] != null && arr[hashCode].getName() != null) {
//进行递加
++hashCode;
//循环
hashCode %= arr.length;
}
arr[hashCode] = info;
}
- 查找元素
public Info find(String key) {
int hashCode = hashCode(key);
while (arr[hashCode] != null) {
if (arr[hashCode].getKey().equals(key)) {
return arr[hashCode];
}
++hashCode;
hashCode %= arr.length;
}
return null;
}
*删除元素
public Info delete(String key) {
int hashCode = hashCode(key);
while (arr[hashCode] != null) {
if (arr[hashCode].getKey().equals(key)) {
Info info = arr[hashCode];
info.setName(null);
return info;
}
}
return null;
}
- 链表法:
每个位置对应一条链表,所有散列值相同的元素都放到相同位置对应的链表中
- 节点
public class Node {
/**
* 数据域
*/
public Info info;
/**
* 指针域
*/
public Node next;
public Node(Info info) {
this.info = info;
}
}
- 链表
public class LinkList {
/**
* 头结点指针
*/
private Node first;
public LinkList() {
first = null;
}
/**
* 在头结点后插入节点
*
* @param info
*/
public void insertFirst(Info info) {
Node node = new Node(info);
node.next = first;
first = node;
}
/**
* 删除节点
*
* @return
*/
public Node deleteFirst() {
Node tmp = first;
first = tmp.next;
return tmp;
}
/**
* 查找方法
*
* @param key
* @return
*/
public Node find(String key) {
Node current = first;
while (!key.equals(current.info.getKey())) {
if (current.next == null) {
return null;
}
current = first.next;
}
return current;
}
/**
* 删除节点
*
* @param key
* @return
*/
public Node delete(String key) {
Node current = first;
Node previous = first;
while (!key.equals(current.info.getKey())) {
if (current.next == null) {
return null;
}
previous = current;
current = current.next;
}
if (current == first) {
first = first.next;
} else {
previous.next = current.next;
}
return current;
}
}
- 初始化Hash Table
public class HashTable {
public LinkList[] arr;
public HashTable() {
arr = new LinkList[100];
}
public HashTable(int maxSize) {
arr = new LinkList[maxSize];
}
}
- 添加元素
public void insert(Info info) {
String key = info.getKey();
int hashCode = hashCode(key);
if (arr[hashCode] == null) {
arr[hashCode] = new LinkList();
}
arr[hashCode].insertFirst(info);
}
- 查找元素
public Info find(String key) {
int hashCode = hashCode(key);
return arr[hashCode].find(key).info;
}
- 删除元素
public Info delete(String key) {
int hashCode = hashCode(key);
return arr[hashCode].delete(key).info;
}
如有错误,欢迎指出!