Java 數(shù)據(jù)結(jié)構哈希算法之哈希桶方式解決哈希沖突
一. 實現(xiàn)形式一(鍵值對只能為整數(shù))
我們可以先實現(xiàn)一個比較簡單的哈希表,使用java中解決哈希沖突的方法,即哈希桶(開散列)方式實現(xiàn),其中注意:
- 可以使用內(nèi)部類方式定義節(jié)點
- 負載因子默認為0.75
- 因為我們使用的是哈希桶方式解決哈希沖突,所以在我們擴容成功之后,原來桶中的數(shù)據(jù)得重新哈希計算出新的位置,不然就和原來桶中的數(shù)據(jù)的位置不一樣了
相關代碼如下
public class HashBucket { static class Node {//使用內(nèi)部類方式定義節(jié)點 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、確定下標 int index = key % this.array.length; //2、遍歷這個下標的鏈表 Node cur = array[index]; while (cur != null) { //更新val if(cur.key == key) { cur.val = val; return; } cur = cur.next; } //3、cur == null 當前數(shù)組下標的鏈表中沒有key Node node = new Node(key,val); node.next = array[index]; array[index] = node; this.usedSize++; //4、判斷當前有沒有超過負載因子 if(loadFactor() >= 0.75) {//負載因子我們認為0.75 //擴容 resize(); } } public int get(int key) {//取出數(shù)據(jù) //以什么方式存儲的 那就以什么方式取 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() {//計算負載因子 return this.usedSize*1.0 / this.array.length; } public void resize() {//擴容函數(shù) //自己創(chuàng)建新的2倍數(shù)組 Node[] newArray = new Node[2*this.array.length]; //遍歷原來的哈希桶 //最外層循環(huán) 控制數(shù)組下標 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ù)組里面的下標 int index = cur.key % newArray.length; //進行頭插法 cur.next = newArray[index]; newArray[index] = cur; cur = curNext; } } this.array = newArray; }
二. 實現(xiàn)方式二(改進版)
上面我們實現(xiàn)的哈希表中的鍵值對只能存放整型數(shù)據(jù),但若是比較復雜的類型,例如字符串,對象等等,此時就需要用到泛型了。其中注意:
- 同樣可以使用內(nèi)部類方式定義節(jié)點類型
- 使用泛型
- 將泛型轉(zhuǎn)換成整數(shù)時要用到
hashCode
方法 - 利用對象哈希值確定下標,為了防止哈希值太大,應該讓其%數(shù)組的長度
- 遍歷數(shù)組下標時,利用equals方法比較key是否相同
- 存放自定義的數(shù)據(jù)類型時,一定要重寫
hashcode
和equals方法
相關代碼如下
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ù)組的下標 int hash = key.hashCode(); int index = hash % this.array.length; Node<K,V> cur = array[index]; while (cur != null) { //equals 起的作用是遍歷當前數(shù)組下標的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; }
到此這篇關于Java 數(shù)據(jù)結(jié)構哈希算法之哈希桶方式解決哈希沖突的文章就介紹到這了,更多相關Java 哈希沖突內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
idea使用pagehelper實現(xiàn)后端分頁功能的步驟詳解
這篇文章主要介紹了idea使用pagehelper實現(xiàn)后端分頁功能的步驟,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09java天數(shù)計算函數(shù)(當前月天數(shù)、某月總天數(shù)及某月剩余天數(shù))4種方法實現(xiàn)代碼
日常開發(fā)中會遇到關于日期的計算,比如當月的天數(shù)、兩日期之間的天數(shù)、當月剩余天數(shù)等等,這篇文章主要給大家介紹了關于java天數(shù)計算函數(shù)(當前月天數(shù)、某月總天數(shù)及某月剩余天數(shù))4種方法實現(xiàn)的相關資料,需要的朋友可以參考下2023-10-10SpringMVC @RequestBody 為null問題的排查及解決
這篇文章主要介紹了SpringMVC @RequestBody 為null問題的排查及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10