Java實(shí)現(xiàn)簡(jiǎn)易HashMap功能詳解
本文實(shí)例講述了Java實(shí)現(xiàn)簡(jiǎn)易HashMap功能。分享給大家供大家參考,具體如下:
創(chuàng)建節(jié)點(diǎn)類
節(jié)點(diǎn)類含有的屬性:鍵值對(duì)(value,key)以及指向下一節(jié)點(diǎn)的next;
這些屬性的get以及set方法
代碼如下:
/** * 節(jié)點(diǎn)類 * @author HP * */ public class Node { private Object value; private Object key; private Node next; /** * 空節(jié)點(diǎn) */ public Node() { } /** * 值為key value的節(jié)點(diǎn) * @param data */ public Node(Object key,Object value) { this.key = key; this.value = value; } //接下來就是一些數(shù)據(jù)和節(jié)點(diǎn)的set,get public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } public Object getKey() { return key; } public void setKey(String key) { this.key = key; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } }
實(shí)現(xiàn)MyHash
實(shí)現(xiàn)MyHash的基本操作:
實(shí)現(xiàn)哈希表的基本存取運(yùn)算
- 1.創(chuàng)建一個(gè)固定大小數(shù)組
- 2.將數(shù)組中的每個(gè)元素作為頭節(jié)點(diǎn)
- 存儲(chǔ)鍵值對(duì)
- 3.存數(shù):通過對(duì)key某種運(yùn)算,計(jì)算出該數(shù)的哈希碼,將該哈希碼與數(shù)組做某種運(yùn)算,得到該數(shù)在數(shù)組中的哪一個(gè)位置下的鏈表中
- 4.取數(shù):計(jì)算該數(shù)的哈希碼,然后同樣找到該數(shù)在數(shù)組中的位置,然后從該頭節(jié)點(diǎn)依次向下進(jìn)行比較得到該數(shù),不存在則返回null
HashMap的源代碼以及函數(shù)使用方法及返回值:
HashMap hash = new HashMap();
hash.keySet()
hash.hashCode() :返回int類型
hash.put(Object key, Object value)
hash.get(Object key)返回key值對(duì)應(yīng)的value
hash.remove(key) 返回對(duì)應(yīng)的value
hash.remove(key, value) 返回boolean是否remove成功
hash.size() :返回int類型的存儲(chǔ)的節(jié)點(diǎn)的個(gè)數(shù)
hash.containsKey(Object key) :boolean
hash.containsValue(value) :boolean
hash.values() :返回value集合
hash.clear();
hash.replace(key, oldValue, newValue) ???
hash.replace(key, value) 將key對(duì)應(yīng)的oldvalue換為傳入的參數(shù)value,返回oldvalue
hash.entrySet()
hash.isEmpty()
hash.equals(o):判斷兩個(gè)對(duì)象是否相等,看系統(tǒng)源代碼,可重寫
遍歷Iterator輸出的是所有節(jié)點(diǎn)對(duì)應(yīng)的value的值
存儲(chǔ)的東西越來越大,那么刪除插入操作越來越復(fù)雜,那么需要rehash(需要一個(gè)條件判斷是否需要rehash)
本次示例沒有編寫rehash函數(shù)。
MyHash代碼,注釋還比較詳細(xì),后邊還有測(cè)試代碼以及測(cè)試結(jié)果:
public class MyHash { //哈希數(shù)組的長(zhǎng)度初始化為8 private int size = 8; private int number = 0;//存儲(chǔ)的節(jié)點(diǎn)的個(gè)數(shù) //哈希數(shù)組 private ArrayList<LinkedList> array_head = new ArrayList<LinkedList>(size); //構(gòu)造方法 public MyHash() { for(int i=0;i<size;i++) { LinkedList mylist = new LinkedList();//哈希數(shù)組中初始化存儲(chǔ)的為空鏈表頭 array_head.add(mylist);//初始化的時(shí)候就將空節(jié)點(diǎn)頭添加到數(shù)組中去 } } /** * 根據(jù) 鍵值對(duì) 生成節(jié)點(diǎn) * 將節(jié)點(diǎn)放入哈希表中 * @param key 鍵 * @param value 值 */ public void put(Object key,Object value) { if(number/size == 10) { rehash(); } number++; Node new_node = new Node(key,value);//由傳入的參數(shù)生成新節(jié)點(diǎn) int code = hashcode(key.toString());//得到哈希碼 int position = locate(code);//得到該哈希碼所對(duì)應(yīng)的哈希數(shù)組中的位置 //找到該位置對(duì)應(yīng)的鏈表頭 LinkedList list_head = (LinkedList) array_head.get(position); //將節(jié)點(diǎn)放入哈希表中 list_head.add(new_node); } /** * */ private void rehash() { } /** * @param key * @param value * @return 返回鍵值對(duì)應(yīng)得節(jié)點(diǎn) */ public Object get(Object key) { int code = hashcode(key.toString());//得到哈希碼 int position = locate(code);//得到該哈希碼所對(duì)應(yīng)的哈希數(shù)組中的位置 //找到該位置對(duì)應(yīng)的鏈表 LinkedList list_head = (LinkedList) array_head.get(position); //從頭遍歷鏈表 ,找到與鍵key對(duì)應(yīng)的節(jié)點(diǎn)的value值進(jìn)行輸出 for(int i=0;i<list_head.size();i++) { //首先拿到頭節(jié)點(diǎn) Node head = (Node) list_head.get(i); Node node = head; while(node!=null) { //如果 鍵 相等 if(node.getKey().equals(key)) { // System.out.println("node.getValue() :"+node.getValue()); return node.getValue(); } node = node.getNext(); } } return null;//否則返回空 } /** * 移除鍵為key的節(jié)點(diǎn) * @param key * @return 返回刪除節(jié)點(diǎn)的key對(duì)應(yīng)的value */ public Object remove(Object key) { number--; int code = hashcode(key.toString());//得到哈希碼 int position = locate(code);//得到該哈希碼所對(duì)應(yīng)的哈希數(shù)組中的位置 //找到該位置對(duì)應(yīng)的鏈表 LinkedList list_head = (LinkedList) array_head.get(position); //從頭遍歷鏈表 ,找到與鍵key對(duì)應(yīng)的節(jié)點(diǎn)的value值進(jìn)行輸出 for(int i=0;i<list_head.size();i++) { //首先拿到頭節(jié)點(diǎn) Node head = (Node) list_head.get(i); Node node = head; while(node!=null) { //如果 鍵 相等 if(node.getKey().equals(key)) { Object value = node.getValue(); list_head.remove(node); return value; } node = node.getNext(); } } return null;//否則返回空 } public Object replace(Object key,Object newvalue) { System.out.println("replace"); int code = hashcode(key.toString());//得到哈希碼 int position = locate(code);//得到該哈希碼所對(duì)應(yīng)的哈希數(shù)組中的位置 //找到該位置對(duì)應(yīng)的鏈表 LinkedList list_head = (LinkedList) array_head.get(position); //從頭遍歷鏈表 ,找到與鍵key對(duì)應(yīng)的節(jié)點(diǎn)的value值進(jìn)行輸出 for(int i=0;i<list_head.size();i++) { //首先拿到頭節(jié)點(diǎn) Node head = (Node) list_head.get(i); Node node = head; while(node!=null) { //如果 鍵 相等 if(node.getKey().equals(key)) { Object oldvalue = node.getValue(); node.setValue(newvalue); return oldvalue; } node = node.getNext(); } } return null;//否則返回空 } /** * @param key * @return 哈希表中含有該key,返回true */ public boolean containsKey(Object key) { int code = hashcode(key.toString());//得到哈希碼 int position = locate(code);//得到該哈希碼所對(duì)應(yīng)的哈希數(shù)組中的位置 //找到該位置對(duì)應(yīng)的鏈表 LinkedList list_head = (LinkedList) array_head.get(position); //從頭遍歷鏈表 ,找到與鍵key對(duì)應(yīng)的節(jié)點(diǎn)的value值進(jìn)行輸出 for(int i=0;i<list_head.size();i++) { //首先拿到頭節(jié)點(diǎn) Node head = (Node) list_head.get(i); Node node = head; while(node!=null) { //如果 鍵 相等 if(node.getKey().equals(key)) { return true; } node = node.getNext(); } } return false;//否則返回空 } public Object containsValue(Object value) { //找到該位置對(duì)應(yīng)的鏈表 for(int k=0;k<size;k++) { LinkedList list_head = (LinkedList) array_head.get(k); //從頭遍歷鏈表 ,找到與鍵key對(duì)應(yīng)的節(jié)點(diǎn)的value值進(jìn)行輸出 for(int i=0;i<list_head.size();i++) { //首先拿到頭節(jié)點(diǎn) Node head = (Node) list_head.get(i); Node node = head; while(node!=null) { //如果 鍵 相等 if(node.getValue().equals(value)) { return true; } node = node.getNext(); } } } return false;//否則返回空 } /** * 打印哈希表 */ public void show() { System.out.println("打印哈希表"); for(int i=0;i<size;i++) { LinkedList list_head = array_head.get(i);//得到鏈表頭 System.out.println("鏈表 :"+(i+1)); for(int j=0;j<list_head.size();j++) { Node head = (Node) list_head.get(j);//取出每個(gè)節(jié)點(diǎn) Node node = head; while(node!=null) { //打印出每個(gè)節(jié)點(diǎn)得鍵值對(duì) System.out.print("節(jié)點(diǎn)"+(j+1)+" :("+node.getKey()+":"+node.getValue()+")"+"-->"); node = node.getNext(); } } System.out.println(); } System.out.println(); } /** * * @return 返回存儲(chǔ)的節(jié)點(diǎn)的個(gè)數(shù) */ public int size() { return number; } /** * 清除哈希表中的所有元素 */ public void clear() { for(int i=0;i<size;i++) { LinkedList list_head = array_head.get(i);//得到鏈表頭 list_head.clear(); } number = 0; } /** * 計(jì)算字符串的哈希碼 * ASCII碼相加 * @param s * @return */ public int hashcode(String s) { int k = 0; for(int i=0;i<s.length();i++) { k += s.charAt(i); } return k; } /** * 得到哈希碼對(duì)應(yīng)在數(shù)組中的位置 * @param k * @return */ public int locate(int k) { int m = k%size; return m; } }
測(cè)試代碼及結(jié)果
public class Test { public static void main(String[] args) { MyHash myhash = new MyHash(); myhash.put("abc", 123); myhash.put("b", 2); myhash.put("c", 3); myhash.put("a", 1); myhash.put("d", 4); myhash.put("e", 5); myhash.put("f", 6); myhash.put("g", 7); myhash.put("h", 8); myhash.put("i", 9); myhash.put("j", 10); myhash.put("k", 11); myhash.put("l", 12); myhash.put("m", 13); System.out.println("myhash.get(\"g\") :"+myhash.get("g")); System.out.println("myhash.size() :"+myhash.size()); System.out.println("myhash.replace(\"m\", 20) :"+myhash.replace("m", 20)); System.out.println("myhash.containsValue(5) :"+myhash.containsValue(5)); System.out.println("myhash.containsKey(\"g\") :"+myhash.containsKey("g")); System.out.println("myhash.remove(\"j\") :"+myhash.remove("j")); System.out.println("myhash.show()"); myhash.show(); myhash.clear(); System.out.println("myhash.clear()"); System.out.println("myhash.size() :"+myhash.size()); } }
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- java使用HashMap實(shí)現(xiàn)斗地主(有序版)
- 解決Java Redis刪除HashMap中的key踩到的坑
- java中JSONObject轉(zhuǎn)換為HashMap(方法+main方法調(diào)用實(shí)例)
- Java 對(duì)HashMap進(jìn)行排序的三種常見方法
- Java HashMap源碼及并發(fā)環(huán)境常見問題解決
- 關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析
- JAVA--HashMap熱門面試題
- Java5種遍歷HashMap數(shù)據(jù)的寫法
- Java手寫簡(jiǎn)易版HashMap的使用(存儲(chǔ)+查找)
- 詳解 Java HashMap 實(shí)現(xiàn)原理
相關(guān)文章
通過Spring Boot + Mybatis + Redis快速搭建現(xiàn)代化Web項(xiàng)目
本篇文章介紹了如何通過Spring Boot、Mybatis以及Redis快速搭建一個(gè)現(xiàn)代化的Web項(xiàng)目,并且同時(shí)介紹了如何在Spring Boot下優(yōu)雅地書寫單元測(cè)試來保證我們的代碼質(zhì)量。具體內(nèi)容詳情大家通過本文學(xué)習(xí)下吧2017-12-12詳解SpringBoot配置devtools實(shí)現(xiàn)熱部署
本篇文章主要介紹了詳解SpringBoot配置devtools實(shí)現(xiàn)熱部署 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05深入理解JVM之Java對(duì)象的創(chuàng)建、內(nèi)存布局、訪問定位詳解
這篇文章主要介紹了深入理解JVM之Java對(duì)象的創(chuàng)建、內(nèi)存布局、訪問定位,結(jié)合實(shí)例形式詳細(xì)分析了Java對(duì)象的創(chuàng)建、內(nèi)存布局、訪問定位相關(guān)概念、原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-09-09SpringBoot實(shí)現(xiàn)在webapp下直接訪問html,jsp
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)在webapp下直接訪問html,jsp問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Java SpringBoot 使用攔截器作為權(quán)限控制的實(shí)現(xiàn)方法
這篇文章主要介紹了Java SpringBoot 使用攔截器作為權(quán)限控制的實(shí)現(xiàn),文中通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10