Java 數(shù)據(jù)結(jié)構(gòu)哈希算法之哈希桶方式解決哈希沖突
一. 實(shí)現(xiàn)形式一(鍵值對(duì)只能為整數(shù))
我們可以先實(shí)現(xiàn)一個(gè)比較簡(jiǎn)單的哈希表,使用java中解決哈希沖突的方法,即哈希桶(開散列)方式實(shí)現(xiàn),其中注意:
- 可以使用內(nèi)部類方式定義節(jié)點(diǎn)
- 負(fù)載因子默認(rèn)為0.75
- 因?yàn)槲覀兪褂玫氖枪M胺绞浇鉀Q哈希沖突,所以在我們擴(kuò)容成功之后,原來桶中的數(shù)據(jù)得重新哈希計(jì)算出新的位置,不然就和原來桶中的數(shù)據(jù)的位置不一樣了
相關(guān)代碼如下
public class HashBucket { static class Node {//使用內(nèi)部類方式定義節(jié)點(diǎn) public int key; public int val; public Node next; public Node(int key, int val) { this.key = key; this.val = val; } } private Node[] array; public int usedSize; public HashBucket() { this.array = new Node[10]; this.usedSize = 0; } public void put(int key,int val) {//存放數(shù)據(jù) //1、確定下標(biāo) int index = key % this.array.length; //2、遍歷這個(gè)下標(biāo)的鏈表 Node cur = array[index]; while (cur != null) { //更新val if(cur.key == key) { cur.val = val; return; } cur = cur.next; } //3、cur == null 當(dāng)前數(shù)組下標(biāo)的鏈表中沒有key Node node = new Node(key,val); node.next = array[index]; array[index] = node; this.usedSize++; //4、判斷當(dāng)前有沒有超過負(fù)載因子 if(loadFactor() >= 0.75) {//負(fù)載因子我們認(rèn)為0.75 //擴(kuò)容 resize(); } } public int get(int key) {//取出數(shù)據(jù) //以什么方式存儲(chǔ)的 那就以什么方式取 int index = key % this.array.length; Node cur = array[index]; while (cur != null) { if(cur.key == key) { return cur.val; } cur = cur.next; } return -1; } public double loadFactor() {//計(jì)算負(fù)載因子 return this.usedSize*1.0 / this.array.length; } public void resize() {//擴(kuò)容函數(shù) //自己創(chuàng)建新的2倍數(shù)組 Node[] newArray = new Node[2*this.array.length]; //遍歷原來的哈希桶 //最外層循環(huán) 控制數(shù)組下標(biāo) for (int i = 0; i < this.array.length; i++) { Node cur = array[i]; Node curNext = null; while (cur != null) { //記錄cur.next curNext = cur.next; //在新的數(shù)組里面的下標(biāo) int index = cur.key % newArray.length; //進(jìn)行頭插法 cur.next = newArray[index]; newArray[index] = cur; cur = curNext; } } this.array = newArray; }
二. 實(shí)現(xiàn)方式二(改進(jìn)版)
上面我們實(shí)現(xiàn)的哈希表中的鍵值對(duì)只能存放整型數(shù)據(jù),但若是比較復(fù)雜的類型,例如字符串,對(duì)象等等,此時(shí)就需要用到泛型了。其中注意:
- 同樣可以使用內(nèi)部類方式定義節(jié)點(diǎn)類型
- 使用泛型
- 將泛型轉(zhuǎn)換成整數(shù)時(shí)要用到
hashCode
方法 - 利用對(duì)象哈希值確定下標(biāo),為了防止哈希值太大,應(yīng)該讓其%數(shù)組的長(zhǎng)度
- 遍歷數(shù)組下標(biāo)時(shí),利用equals方法比較key是否相同
- 存放自定義的數(shù)據(jù)類型時(shí),一定要重寫
hashcode
和equals方法
相關(guān)代碼如下
class Person { public String id; public Person(String id) { this.id = id; } @Override public String toString() { return "Person{" + "id='" + id + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return Objects.equals(id, person.id); } @Override public int hashCode() { return Objects.hash(id); } } public class HashBuck2<K,V> { static class Node<K,V> { public K key; public V val; public Node<K,V> next; public Node(K key,V val) { this.key = key; this.val = val; } } public Node<K,V>[] array = (Node<K, V>[]) new Node[10]; public int usedSize; public void put(K key,V val) { //通過hashcode方法定位數(shù)組的下標(biāo) int hash = key.hashCode(); int index = hash % this.array.length; Node<K,V> cur = array[index]; while (cur != null) { //equals 起的作用是遍歷當(dāng)前數(shù)組下標(biāo)的key是否相同 if(cur.key.equals(key)) { cur.val = val; } cur = cur.next; } Node<K,V> node = new Node<>(key,val); node.next = array[index]; array[index] = node; this.usedSize++; } public V get(K key) { int hash = key.hashCode(); int index = hash % this.array.length; Node<K,V> cur= array[index]; while (cur != null) { if(cur.key.equals(key)) { return cur.val; } cur = cur.next; } return null; }
到此這篇關(guān)于Java 數(shù)據(jù)結(jié)構(gòu)哈希算法之哈希桶方式解決哈希沖突的文章就介紹到這了,更多相關(guān)Java 哈希沖突內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea使用pagehelper實(shí)現(xiàn)后端分頁功能的步驟詳解
這篇文章主要介紹了idea使用pagehelper實(shí)現(xiàn)后端分頁功能的步驟,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09java天數(shù)計(jì)算函數(shù)(當(dāng)前月天數(shù)、某月總天數(shù)及某月剩余天數(shù))4種方法實(shí)現(xiàn)代碼
日常開發(fā)中會(huì)遇到關(guān)于日期的計(jì)算,比如當(dāng)月的天數(shù)、兩日期之間的天數(shù)、當(dāng)月剩余天數(shù)等等,這篇文章主要給大家介紹了關(guān)于java天數(shù)計(jì)算函數(shù)(當(dāng)前月天數(shù)、某月總天數(shù)及某月剩余天數(shù))4種方法實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-10-10SpringBoot下如何實(shí)現(xiàn)支付寶接口的使用
這篇文章主要介紹了SpringBoot下如何實(shí)現(xiàn)支付寶接口的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Java 序列化詳解及簡(jiǎn)單實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了 Java 序列化詳解及簡(jiǎn)單實(shí)現(xiàn)實(shí)例的相關(guān)資料,使用序列化目的:以某種存儲(chǔ)形式使自定義對(duì)象持久化,將對(duì)象從一個(gè)地方傳遞到另一個(gè)地方,需要的朋友可以參考下2017-03-03SpringMVC @RequestBody 為null問題的排查及解決
這篇文章主要介紹了SpringMVC @RequestBody 為null問題的排查及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10SpringIOC框架的簡(jiǎn)單實(shí)現(xiàn)步驟
這篇文章主要介紹了SpringIOC框架簡(jiǎn)單實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用Spring,感興趣的朋友可以了解下2021-05-05