java实现自定义哈希表

1|0哈希表实现原理

哈希表底层是使用数组实现的,因为数组使用下标查找元素很快。所以实现哈希表的关键就是把某种数据类型通过计算变成数组的下标(这个计算就是hashCode()函数***

1|1怎么把一个字符串转化成整数下标呢?

  • 可以把每个字符的ASCII对应的数字相加作为下标,比如"abc"=(a-96)+(b-96)+(c-96),'a'的ASCII是97;这种方式的缺点就是哈希值很容易重复,比如aaa,abc,cab
  • 也可以使用幂的连乘,保证不同字符串算出来的哈希值不一样,这种方式的缺点是会算出来的哈希值会发生数值越界
  • 解决越界问题可以使用大数运算,java里的BitInt

2|0实现

2|1首先创建数据类型

package dataS.hash; import java.util.Objects; public class Info { //员工号 private String key; //员工值 private String value; public Info(String key, String value) { this.key = key; this.value = value; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "Info{" + "key='" + key + '\'' + ", value='" + value + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Info info = (Info) o; return Objects.equals(key, info.key) && Objects.equals(value, info.value); } }

2|2创建HashTable类

package dataS.hash; import java.math.BigInteger; public class HashTable { private Info[] arrays; /** * 默认构造方法,默认数组大小100 */ public HashTable() { this.arrays = new Info[100]; } /** * 指定大小 */ public HashTable(int maxsize){ this.arrays=new Info[maxsize]; } /** * 插入数据,直接把员工号作为数组索引 */ public void insert(Info info){ this.arrays[hashCode(info.getKey())]=info; } /** * 查找数据,直接把员工号作为数组索引 */ public Info find(String key){ return arrays[hashCode(key)]; } public int hashCode(String key){ return hash3(key); } public int hash1(String key){ //1.将字母转化成ASCII,然后相加 int hashvalue=0; for (int i = 0; i < key.length(); i++) { //a是97,其他字母减去97就是字母对应的数字 int letter=key.charAt(i)-96; hashvalue+= letter; } //取模可以压缩可选值,比如想把100个可选择压缩到0-9,对数组长度取模 return hashvalue%arrays.length; } public int hash2(String key){ //2.幂的连乘,这里可能hashvalue的值超过范围,使用long也不行,可以用bigint int hashvalue=0; int pow27=1; for (int i = 0; i < key.length(); i++) { //比如abc,1*27^0+2*27^1+2*27^2 int letter=key.charAt(i)-96; hashvalue+= letter*pow27; pow27*=27; } return hashvalue%arrays.length; } public int hash3(String key){ //3.用bigint BigInteger hashvalue=new BigInteger("0"); BigInteger pow27=new BigInteger("1"); for (int i = 0; i < key.length(); i++) { //比如abc,1*27^0+2*27^1+3*27^2 int letter=key.charAt(i)-96; //把letter用bigint包装起来 BigInteger bigLetter=new BigInteger(letter+""); hashvalue=hashvalue.add(bigLetter.multiply(pow27)); pow27=pow27.multiply(new BigInteger(27+"")); } return hashvalue.mod(new BigInteger(arrays.length+"")).intValue(); } }

2|3测试

package dataS.hash; public class HashTest { public static void main(String[] args) { HashTable hashTable=new HashTable(); hashTable.insert(new Info("a","111")); hashTable.insert(new Info("tc","222")); hashTable.insert(new Info("cba","333")); System.out.println(hashTable.find("a").getValue()); System.out.println(hashTable.find("tc").getValue()); System.out.println(hashTable.find("cba").getValue()); } }


发现tc把a的位置给占用了

3|0冲突解决

为什么会有冲突呢?因为我压缩了可选值(进行了取模运算),比如我想把1和101个元素放到大小为10的数组,对10取模后下标都是1,肯定会发生冲突

4|0开放地址法

把一号位置占用了,101就看2号位置有没有被占用,直到找到空位置,然后插入。主要101原本想插入的位置和最终插入位置一定是连续的,中间不会有空位置

4|1修改插入删除和查找方法

package dataS.hashTwo; import java.math.BigInteger; public class HashTable { private Info[] arrays; /** * 默认构造方法,实现哈希表的本质是哈希函数将不同类型的数据转化成数组下表 */ public HashTable() { this.arrays = new Info[100]; } /** * 指定大小 */ public HashTable(int maxsize){ this.arrays=new Info[maxsize]; } /** * 插入数据,直接把员工号作为数组索引 */ public void insert(Info info){ String key=info.getKey(); //关键字对应的哈希值,将要作为下标 int hashValue=hash3(key); //如果被占用,并且key对应的value也不为空(因为删除的时候是删除info对象里的value,而不是全部) while (arrays[hashValue]!=null&&arrays[hashValue].getValue()!=null){ //一直找到一个没被占用的 hashValue++; //比如99和599哈希值取模后都是99,99加1后数组会越界,但是前面还有空的位置 hashValue%=arrays.length; //直到整个数组都填满 } arrays[hashValue]=info; } /** * 查找数据,直接把员工号作为数组索引 */ public Info find(String key){ int hashValue=hash3(key); //从第一次的位置,到最终插入位置是连续的 while (arrays[hashValue]!=null){ //如果key值相等说明找到了 if(arrays[hashValue].getKey().equals(key)) return arrays[hashValue]; hashValue++; hashValue%=arrays.length; } return null; } public Info delete(String key){ int hashValue=hash3(key); //从第一次的位置,到最终插入位置是连续的 while (arrays[hashValue]!=null){ //如果key值相等说明找到了,将Info的value值空 if(arrays[hashValue].getKey().equals(key)){ Info info=arrays[hashValue]; arrays[hashValue].setValue(null); return info; } hashValue++; hashValue%=arrays.length; } return null; } public int hashCode(String key){ return hash3(key); } public int hash1(String key){ //1.将字母转化成ASCII,然后相加 int hashvalue=0; for (int i = 0; i < key.length(); i++) { //a是97,其他字母减去97就是字母对应的数字 int letter=key.charAt(i)-96; hashvalue+= letter; } //取模可以压缩可选值,比如想把100个可选择压缩到0-9,对数组长度取模 return hashvalue%arrays.length; } public int hash2(String key){ //2.幂的连乘,这里可能hashvalue的值超过范围,使用long也不行,可以用bigint int hashvalue=0; int pow27=1; for (int i = 0; i < key.length(); i++) { //比如abc,1*27^0+2*27^1+2*27^2 int letter=key.charAt(i)-96; hashvalue+= letter*pow27; pow27*=27; } return hashvalue%arrays.length; } public int hash3(String key){ //3.用bigint BigInteger hashvalue=new BigInteger("0"); BigInteger pow27=new BigInteger("1"); for (int i = 0; i < key.length(); i++) { //比如abc,1*27^0+2*27^1+3*27^2 int letter=key.charAt(i)-96; //把letter用bigint包装起来 BigInteger bigLetter=new BigInteger(letter+""); hashvalue=hashvalue.add(bigLetter.multiply(pow27)); pow27=pow27.multiply(new BigInteger(27+"")); } return hashvalue.mod(new BigInteger(arrays.length+"")).intValue(); } }

4|2测试



发现冲突问题解决了

5|0链地址法:

实现原理,将一个个链表作为数组的元素,当发生冲突就将冲突元素链接到对应的链表后面
不同的哈希值对应一个不同的链表,哈希值相同的串在一起

5|1链表结构,实现增删查功能

public class LinkedNode { public Info info; public LinkedNode next; public LinkedNode(Info info) { this.info = info; } }
package dataS.hashThree; public class Linked { public LinkedNode head; public Linked() { head = null; } //插入节点,在头结点之后插入. //重点是不要让节点丢失 public void insert(Info info) { LinkedNode node = new LinkedNode(info); if (head == null) { head = node; } else { node.next = head.next; // head.next=node;此处不应该这么写,会形成环 head.next = node; } } //在头结点之后删除一个元素 public LinkedNode delete() throws Exception { if(head.next==null){ head=null; return null; }else{ LinkedNode tmp = head.next; head.next = tmp.next; return tmp; } } //查找方法 public LinkedNode find(String key) { LinkedNode tmp = head; if(tmp==null) return null; while (!key.equals(tmp.info.getKey())){ if(tmp.next==null) return null; tmp=tmp.next; } return tmp; } //根据值来删除元素 public LinkedNode deleteByvalue(String key){ LinkedNode ans = null; LinkedNode pretmp = head; LinkedNode tmp =head.next; if(key.equals(head.info.getKey())){ ans=head; head=head.next; return ans; } while (tmp!=null){ if(key.equals(tmp.info.getKey())){ ans=tmp; pretmp.next=tmp.next; } pretmp=pretmp.next; tmp=tmp.next; } return ans; } }

5|2哈希表

package dataS.hashThree; import java.math.BigInteger; public class HashTable { private Linked[] arrays; /** * 默认构造方法,实现哈希表的本质是哈希函数将不同类型的数据转化成数组下表 */ public HashTable() { this.arrays = new Linked[100]; } /** * 指定大小 */ public HashTable(int maxsize){ this.arrays=new Linked[maxsize]; } /** * 插入数据,直接把员工号作为数组索引 */ public void insert(Info info){ String key=info.getKey(); //关键字对应的哈希值,将要作为下标 int hashValue=hash3(key); // if(arrays[hashValue]==null){ arrays[hashValue]=new Linked(); } //插入 arrays[hashValue].insert(info); } /** * 查找数据,直接把员工号作为数组索引 */ public Info find(String key){ int hashValue=hash3(key); //从第一次的位置,到最终插入位置是连续的 LinkedNode node = arrays[hashValue].find(key); if(node==null) return null; return node.info; } public Info delete(String key){ int hashValue=hash3(key); //从第一次的位置,到最终插入位置是连续的 LinkedNode node = arrays[hashValue].deleteByvalue(key); return node.info; } public int hashCode(String key){ return hash3(key); } public int hash1(String key){ //1.将字母转化成ASCII,然后相加 int hashvalue=0; for (int i = 0; i < key.length(); i++) { //a是97,其他字母减去97就是字母对应的数字 int letter=key.charAt(i)-96; hashvalue+= letter; } //取模可以压缩可选值,比如想把100个可选择压缩到0-9,对数组长度取模 return hashvalue%arrays.length; } public int hash2(String key){ //2.幂的连乘,这里可能hashvalue的值超过范围,使用long也不行,可以用bigint int hashvalue=0; int pow27=1; for (int i = 0; i < key.length(); i++) { //比如abc,1*27^0+2*27^1+2*27^2 int letter=key.charAt(i)-96; hashvalue+= letter*pow27; pow27*=27; } return hashvalue%arrays.length; } public int hash3(String key){ //3.用bigint BigInteger hashvalue=new BigInteger("0"); BigInteger pow27=new BigInteger("1"); for (int i = 0; i < key.length(); i++) { //比如abc,1*27^0+2*27^1+3*27^2 int letter=key.charAt(i)-96; //把letter用bigint包装起来 BigInteger bigLetter=new BigInteger(letter+""); hashvalue=hashvalue.add(bigLetter.multiply(pow27)); pow27=pow27.multiply(new BigInteger(27+"")); } return hashvalue.mod(new BigInteger(arrays.length+"")).intValue(); } public int hash4(String key){ //3.使用开放地址法解决冲突 //当冲突发生,查找空位置插入,而不再用哈希函数得到数组下标 return 1; } }

5|3测试

发现冲突解决了

6|0总结

哈希表的本质是数组,学会hashCode的实现方式,数据的压缩,掌握解决冲突的俩种办法
重点是链地址法,比开放地址法高效简洁


__EOF__

本文作者程序员小宇
本文链接https://www.cnblogs.com/treasury/p/12777712.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   程序员小宇  阅读(3413)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示