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ò)容成功之后,原來(lái)桶中的數(shù)據(jù)得重新哈希計(jì)算出新的位置,不然就和原來(lái)桶中的數(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)的鏈表中沒(méi)有key
Node node = new Node(key,val);
node.next = array[index];
array[index] = node;
this.usedSize++;
//4、判斷當(dāng)前有沒(méi)有超過(guò)負(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];
//遍歷原來(lái)的哈希桶
//最外層循環(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) {
//通過(guò)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)文章
Java的SpringMVC中控制器返回XML數(shù)據(jù)問(wèn)題
這篇文章主要介紹了Java的SpringMVC中控制器返回XML數(shù)據(jù)問(wèn)題,控制器是處理HTTP請(qǐng)求的組件,它們接收來(lái)自客戶端的請(qǐng)求,并將其轉(zhuǎn)換為適當(dāng)?shù)捻憫?yīng),這些響應(yīng)可以是動(dòng)態(tài)生成的?HTML?頁(yè)面,也可以是JSON或XML格式的數(shù)據(jù),需要的朋友可以參考下2023-07-07
探究Java常量本質(zhì)及三種常量池(小結(jié))
這篇文章主要介紹了探究Java常量本質(zhì)及三種常量池(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
mybatis插入數(shù)據(jù)后如何返回新增數(shù)據(jù)的id值
當(dāng)往mysql數(shù)據(jù)庫(kù)插入一條數(shù)據(jù)時(shí),有時(shí)候需要知道剛插入的信息,下面這篇文章主要給大家介紹了關(guān)于mybatis插入數(shù)據(jù)后如何返回新增數(shù)據(jù)id值的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
SpringBoot使用Spring Security實(shí)現(xiàn)登錄注銷功能
這篇文章主要介紹了SpringBoot使用Spring Security實(shí)現(xiàn)登錄注銷功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-09-09
springboot接收excel數(shù)據(jù)文件去重方式
文章主要介紹了如何在Spring?Boot中實(shí)現(xiàn)文件上傳并入庫(kù)的功能,包括讀取Excel文件、生成Entity對(duì)象、使用MergeInto語(yǔ)句進(jìn)行數(shù)據(jù)庫(kù)操作以及注意事項(xiàng)2024-12-12
Java基礎(chǔ)學(xué)習(xí)之關(guān)鍵字和變量數(shù)據(jù)類型的那些事
變量就是系統(tǒng)為程序分配的一塊內(nèi)存單元,用來(lái)存儲(chǔ)各種類型的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)學(xué)習(xí)之關(guān)鍵字和變量數(shù)據(jù)類型的那些事,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Mybatis不啟動(dòng)項(xiàng)目直接測(cè)試Mapper的實(shí)現(xiàn)方法
在項(xiàng)目開發(fā)中,測(cè)試單個(gè)Mybatis Mapper方法通常需要啟動(dòng)整個(gè)SpringBoot項(xiàng)目,消耗大量時(shí)間,本文介紹通過(guò)Main方法和Mybatis配置類,快速測(cè)試Mapper功能,無(wú)需啟動(dòng)整個(gè)項(xiàng)目,這方法使用AnnotationConfigApplicationContext容器2024-09-09
SpringMVC對(duì)日期類型的轉(zhuǎn)換示例
本篇文章主要介紹了SpringMVC對(duì)日期類型的轉(zhuǎn)換示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

