您好,登錄后才能下訂單哦!
這篇文章主要介紹了怎么用Java哈希桶方式解決哈希沖突的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇怎么用Java哈希桶方式解決哈希沖突文章都會(huì)有所收獲,下面我們一起來看看吧。
我們可以先實(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)的哈希表中的鍵值對(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哈希桶方式解決哈希沖突”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“怎么用Java哈希桶方式解決哈希沖突”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。