Java手寫簡易版HashMap的使用(存儲+查找)
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; //存放鍵值對的個數(shù) public MyHashMap() { table=new Node[16]; } }
put()方法存儲鍵值對
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()方法查找鍵值對
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; //存放鍵值對的個數(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(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡單介紹Java網(wǎng)絡(luò)編程中的HTTP請求
這篇文章主要介紹了簡單介紹Java網(wǎng)絡(luò)編程中的HTTP請求,需要的朋友可以參考下2015-09-09Spring?Boot中的過濾器攔截器監(jiān)聽器使用技巧匯總
本文將介紹在Spring?Boot應(yīng)用程序中使用過濾器、攔截器和監(jiān)聽器的使用技巧,我們將討論它們之間的區(qū)別,以及何時使用它們,我們還將提供代碼示例,以幫助您在自己的應(yīng)用程序中使用它們2023-12-12java實(shí)現(xiàn)將結(jié)果集封裝到List中的方法
這篇文章主要介紹了java實(shí)現(xiàn)將結(jié)果集封裝到List中的方法,涉及java數(shù)據(jù)庫查詢及結(jié)果集轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-07-07Java instanceof和getClass()區(qū)別實(shí)例解析
這篇文章主要介紹了Java instanceof和getClass()區(qū)別實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07Java遞歸算法經(jīng)典實(shí)例(經(jīng)典兔子問題)
本文主要對經(jīng)典的兔子案例分析,來進(jìn)一步更好的理解和學(xué)習(xí)java遞歸算法,具有很好的參考價值,需要的朋友一起來看下吧2016-12-12詳細(xì)總結(jié)Java創(chuàng)建文件夾的方法及優(yōu)缺點(diǎn)
很多小伙伴都不知道如何用Java創(chuàng)建文件夾,今天給大家整理了這篇文章,文中有非常詳細(xì)的方法介紹及方法的優(yōu)缺點(diǎn),對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05