Java手寫簡(jiǎn)易版HashMap的使用(存儲(chǔ)+查找)
HashMap的基本結(jié)構(gòu)
package com.liuyuhe;
public class Node {
int hash;
Object key;
Object value;
Node next;
}
package com.liuyuhe;
public class MyHashMap {
Node[] table; //位桶數(shù)組
int size; //存放鍵值對(duì)的個(gè)數(shù)
public MyHashMap() {
table=new Node[16];
}
}
put()方法存儲(chǔ)鍵值對(duì)
public void put(Object key,Object value) {
Node newNode = new Node();
newNode.hash=myHash(key.hashCode(),table.length);
newNode.key=key;
newNode.value=value;
newNode.next=null;
Node temp = table[newNode.hash];
Node iterLast=null;
if(temp==null) {
table[newNode.hash]=newNode;
}else {
while(temp!=null) {
if(temp.key.equals(key)) {
temp.value=value;
return;
}else {
iterLast=temp;
temp=temp.next;
}
}
iterLast.next=newNode;
}
++size;
}
public int myHash(int v,int length) {
System.out.println("hash in myHash: "+(v&(length-1)));
return v&(length-1);
}
重寫toString()方法打印Map內(nèi)容
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
boolean isFirst=true;
//遍歷數(shù)組
for(int i=0;i<table.length;++i) {
//遍歷鏈表
Node temp = table[i];
while(temp!=null) {
if(isFirst) {
isFirst=false;
sb.append(temp.key+":"+temp.value);
}else {
sb.append(","+temp.key+":"+temp.value);
}
temp=temp.next;
}
}
sb.append("}");
return sb.toString();
}
get()方法查找鍵值對(duì)
public Object get(Object key) {
int hash=myHash(key.hashCode(),table.length);
Object value=null;
if(table[hash]!=null) {
Node temp=table[hash];
while(temp!=null) {
if(temp.key.equals(key)) {
value=temp.value;
break;
}else {
temp=temp.next;
}
}
}
return value;
}
增加泛型(完整代碼)
package com.liuyuhe;
public class Node<K,V> {
int hash;
K key;
V value;
Node next;
}
package com.liuyuhe;
public class MyHashMap<K,V> {
Node[] table; //位桶數(shù)組
int size; //存放鍵值對(duì)的個(gè)數(shù)
public MyHashMap() {
table=new Node[16];
}
public void put(K key,V value) {
Node newNode = new Node();
newNode.hash=myHash(key.hashCode(),table.length);
newNode.key=key;
newNode.value=value;
newNode.next=null;
Node temp = table[newNode.hash];
Node iterLast=null;
if(temp==null) {
table[newNode.hash]=newNode;
}else {
while(temp!=null) {
if(temp.key.equals(key)) {
temp.value=value;
return;
}else {
iterLast=temp;
temp=temp.next;
}
}
iterLast.next=newNode;
}
++size;
}
@SuppressWarnings("unchecked")
public V get(K key) {
int hash=myHash(key.hashCode(),table.length);
V value=null;
if(table[hash]!=null) {
Node temp=table[hash];
while(temp!=null) {
if(temp.key.equals(key)) {
value=(V)temp.value;
break;
}else {
temp=temp.next;
}
}
}
return value;
}
public int myHash(int v,int length) {
System.out.println("hash in myHash: "+(v&(length-1)));
return v&(length-1);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
boolean isFirst=true;
//遍歷數(shù)組
for(int i=0;i<table.length;++i) {
//遍歷鏈表
Node temp = table[i];
while(temp!=null) {
if(isFirst) {
isFirst=false;
sb.append(temp.key+":"+temp.value);
}else {
sb.append(","+temp.key+":"+temp.value);
}
temp=temp.next;
}
}
sb.append("}");
return sb.toString();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
現(xiàn)代高效的java構(gòu)建工具gradle的快速入門
和Maven一樣,Gradle只是提供了構(gòu)建項(xiàng)目的一個(gè)框架,真正起作用的是Plugin,本文主要介紹了gradle入門,文中通過(guò)示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下2021-11-11
Mybatis和Mybatis-Plus時(shí)間范圍查詢方式
這篇文章主要介紹了Mybatis和Mybatis-Plus時(shí)間范圍查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(53)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-08-08
Apache?Arrow?Parquet存儲(chǔ)與使用
這篇文章主要為大家介紹了Apache?Arrow?Parquet存儲(chǔ)與使用原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Spring Shell 命令行實(shí)現(xiàn)交互式Shell應(yīng)用開發(fā)
本文主要介紹了Spring Shell 命令行實(shí)現(xiàn)交互式Shell應(yīng)用開發(fā),能夠幫助開發(fā)者快速構(gòu)建功能豐富的命令行應(yīng)用程序,具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04
一篇文章帶你學(xué)習(xí)JAVA MyBatis底層原理
近來(lái)想寫一個(gè)mybatis的分頁(yè)插件,但是在寫插件之前肯定要了解一下mybatis具體的工作原理吧,本文就詳細(xì)總結(jié)了MyBatis工作原理,,需要的朋友可以參考下2021-09-09
Java如何實(shí)現(xiàn)一個(gè)簡(jiǎn)化版的Stream框架
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)化版的 Stream 實(shí)現(xiàn),展示了如何通過(guò)延遲執(zhí)行來(lái)處理數(shù)據(jù)流,感興趣的小伙伴可以跟隨小編一學(xué)習(xí)一下2024-10-10

