Java實(shí)現(xiàn)哈希表的基本功能
一、哈希表頭插法放入元素
/**
* user:ypc;
* date:2021-05-20;
* time: 11:05;
*/
public class HashBuck {
class Node {
public int key;
int value;
Node next;
Node(int key, int value) {
this.key = key;
this.value = value;
}
}
public int usedSize;
public Node[] array;
HashBuck() {
this.array = new Node[8];
this.usedSize = 0;
}
//JDk1.7及之前是頭插法
public void put1(int key, int value) {
int index = key % this.array.length;
Node node = new Node(key, value);
Node cur = array[index];
while (cur != null) {
if (cur.key == key) {
cur.value = value;
return;
}
cur = cur.next;
}
node.next = array[index];
array[index] = node;
this.usedSize++;
if (loadFactor() > 0.75) {
resize1();
}
}
public double loadFactor() {
return this.usedSize / this.array.length * 1.0;
}
}
二、哈希表尾插法放入元素
//JDK1.8是尾插法
public Node findLast(Node head) {
if (head == null) return head;
Node cur = head;
while (cur.next != null) {
cur = cur.next;
}
return cur;
}
public void put2(int key, int value) {
int index = key % this.array.length;
Node node = new Node(key, value);
Node cur = array[index];
while (cur != null) {
if (cur.key == key) {
cur.value = value;
return;
}
cur = cur.next;
}
Node last = findLast(array[index]);
if (last == null) {
array[index] = node;
this.usedSize++;
return;
}
last.next = node;
this.usedSize++;
if (loadFactor() > 0.75) {
resize2();
}
}
三、哈希表頭插、尾插擴(kuò)容
public void resize1() {
Node[] newArray = new Node[this.array.length * 2];
for (int i = 0; i < this.array.length; i++) {
Node cur = array[i];
while (cur != null) {
int index = cur.key % newArray.length;
Node curNext = cur.next;
cur.next = newArray[index];
newArray[index] = cur;
cur = curNext;
}
}
this.array = newArray;
}
//resize尾插
public void resize2() {
Node[] newArray = new Node[this.array.length * 2];
for (int i = 0; i < this.array.length; i++) {
Node cur = array[i];
while (cur != null) {
int index = cur.key % newArray.length;
Node curNext = cur.next;
Node last = findLast(newArray[index]);
if (last == null) {
newArray[index] = cur;
break;
}
last.next = cur;
cur = curNext;
}
}
this.array = newArray;
}
public Node findLast(Node head) {
if (head == null) return head;
Node cur = head;
while (cur.next != null) {
cur = cur.next;
}
return cur;
}
四、找到key對(duì)應(yīng)的value
public int get(int key) {
int index = key % this.array.length;
Node cur = this.array[index];
while (cur != null) {
if (cur.key == key) {
return cur.value;
}
cur = cur.next;
}
return -1;
}
五、運(yùn)行結(jié)果
class HashBuckTest {
public static void main(String[] args) {
HashBuck hashBuck = new HashBuck();
//頭插
hashBuck.put1(9,451);
hashBuck.put1(17,455);
//尾插
//hashBuck.put2(9,451);
//hashBuck.put2(17,455);
hashBuck.put1(2,45);
hashBuck.put1(3,14);
hashBuck.put1(4,52);
hashBuck.put1(4,89);
System.out.println(hashBuck.get(1));
System.out.println("+=================");
}
}
頭插

尾插

擴(kuò)容
class HashBuckTest {
public static void main(String[] args) {
HashBuck hashBuck = new HashBuck();
// hashBuck.put1(9, 451);
// hashBuck.put1(17, 455);
hashBuck.put1(1, 589);
hashBuck.put1(2, 45);
hashBuck.put1(3, 14);
hashBuck.put1(4, 52);
hashBuck.put1(4, 1);
hashBuck.put1(6, 829);
hashBuck.put1(7, 72);
hashBuck.put1(8, 8279);
hashBuck.put2(9,451);
hashBuck.put2(15,455);
hashBuck.put2(31,451);
System.out.println(hashBuck.get(7));
System.out.println(hashBuck.get(4));
System.out.println(hashBuck.get(15));
System.out.println(hashBuck.get(31));
}
}


get

六、哈希表的泛型實(shí)現(xiàn)
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;
public int usedSize;
public HashBuck2() {
this.array = (Node<K, V>[]) new Node[8];
}
public void put(K key, V val) {
int hash = key.hashCode();
int index = hash % array.length;
Node<K, V> cur = array[index];
while (cur != null) {
if (cur.key.equals(key)) {
cur.val = val;
return;
}
cur = cur.next;
}
Node<K, V> node = new Node<>(key, val);
node.next = array[index];
array[index] = node;
this.usedSize++;
if (loadFactor() > 0.75) {
resize();
}
}
public V get(K key) {
int hash = key.hashCode();
int index = hash % array.length;
Node<K, V> cur = array[index];
while (cur != null) {
if (cur.key.equals(key)) {
return cur.val;
}
cur = cur.next;
}
return null;
}
public void resize() {
Node[] newArray = new Node[this.array.length * 2];
for (int i = 0; i < this.array.length; i++) {
Node<K,V> cur = array[i];
while (cur != null) {
int hash = cur.key.hashCode();
int index = hash % array.length;
Node <K,V>curNext = cur.next;
cur.next = newArray[index];
newArray[index] = cur;
cur = curNext;
}
}
this.array = newArray;
}
public double loadFactor() {
return this.usedSize / this.array.length * 1.0;
}
}
/**
* user:ypc;
* date:2021-05-20;
* time: 15:25;
*/
class Student{
public int id;
Student(int id){
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
class HashBuck2Test{
public static void main(String[] args) {
HashBuck2<Student,Integer> hashBuck2 = new HashBuck2<>();
Student s1 = new Student(10001);
Student s2 = new Student(10001);
Student s3 = new Student(10003);
hashBuck2.put(s1,89);
hashBuck2.put(s1,60);
hashBuck2.put(s2,94);
hashBuck2.put(s3,100);
System.out.println(hashBuck2.get(s1));
System.out.println(hashBuck2.get(s2));
}
}
注意:
要用自定義類作為 HashMap 的 key 或者 HashSet 的值,必須覆寫 hashCode 和 equals 方 法,而且要做到 equals 相等的對(duì)象,hashCode 一定是一致的。
比如Student s1 和 s2 的id一樣,得到的卻是不同的value,所以要覆寫hashCode 和 equals 方 法,如果不覆寫,則使用的是Object類的hashCode 和 equals 方 法,比較的是地址。

重寫之后

七、為什么JDK1.7及之前使用頭插法而JDK1.8使用尾插法
hashmap用數(shù)組+鏈表。數(shù)組是固定長(zhǎng)度,鏈表太長(zhǎng)就需要擴(kuò)充數(shù)組長(zhǎng)度進(jìn)行rehash減少鏈表長(zhǎng)度。如果兩個(gè)線程同時(shí)觸發(fā)擴(kuò)容,在移動(dòng)節(jié)點(diǎn)時(shí)會(huì)導(dǎo)致一個(gè)鏈表中的2個(gè)節(jié)點(diǎn)相互引用,從而生成環(huán)鏈表
到此這篇關(guān)于Java實(shí)現(xiàn)哈希表的基本功能的文章就介紹到這了,更多相關(guān)哈希表基本功能實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于kafka實(shí)現(xiàn)Spring Cloud Bus消息總線
消息總線是一種通信工具,可以在機(jī)器之間互相傳輸消息、文件等,這篇文章主要介紹了如何利用kafka實(shí)現(xiàn)SpringCloud Bus消息總線,感興趣的可以學(xué)習(xí)一下2022-04-04
Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼
這篇文章主要介紹了 Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Java volatile四種內(nèi)存屏障的作用與生效機(jī)制原理詳解
內(nèi)存屏障是處理器提供的一種指令,用于控制指令執(zhí)行順序和內(nèi)存可見(jiàn)性,在Java中,volatile關(guān)鍵字就是通過(guò)插入內(nèi)存屏障來(lái)實(shí)現(xiàn)其內(nèi)存語(yǔ)義的,下面我將詳細(xì)解釋四種內(nèi)存屏障的含義和工作原理,感興趣的朋友一起看看吧2025-09-09
基于FeignException$InternalServerError的解決方案
這篇文章主要介紹了FeignException$InternalServerError的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Redis實(shí)現(xiàn)商品秒殺功能頁(yè)面流程
這篇文章主要介紹了Redis實(shí)現(xiàn)商品秒殺功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
Spring中HandlerMethod類源碼詳細(xì)解析
這篇文章主要介紹了Spring中HandlerMethod類源碼詳細(xì)解析,HandlerMethod類用于封裝控制器方法信息,包含類信息、方法Method對(duì)象、參數(shù)、注解等信息,具體的接口請(qǐng)求是可以根據(jù)封裝的信息調(diào)用具體的方法來(lái)執(zhí)行業(yè)務(wù)邏輯,需要的朋友可以參考下2023-11-11
IDEA將Maven項(xiàng)目中指定文件夾下的xml等文件編譯進(jìn)classes的方法
這篇文章主要介紹了IDEA將Maven項(xiàng)目中指定文件夾下的xml等文件編譯進(jìn)classes的方法,幫助大家更好的利用IDEA進(jìn)行Java的開(kāi)發(fā)學(xué)習(xí),感興趣的朋友可以了解下2021-01-01
java實(shí)現(xiàn)連接mysql數(shù)據(jù)庫(kù)單元測(cè)試查詢數(shù)據(jù)的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇java實(shí)現(xiàn)連接mysql數(shù)據(jù)庫(kù)單元測(cè)試查詢數(shù)據(jù)的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10

