欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java手寫簡易版HashMap的使用(存儲+查找)

 更新時間:2020年01月21日 10:20:46   作者:虐貓人薛定諤i  
這篇文章主要介紹了Java手寫簡易版HashMap的使用(存儲+查找),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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實(shí)現(xiàn)斗地主發(fā)牌過程

    用java實(shí)現(xiàn)斗地主發(fā)牌過程

    這篇文章主要為大家詳細(xì)介紹了用java實(shí)現(xiàn)斗地主發(fā)牌過程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 簡單介紹Java網(wǎng)絡(luò)編程中的HTTP請求

    簡單介紹Java網(wǎng)絡(luò)編程中的HTTP請求

    這篇文章主要介紹了簡單介紹Java網(wǎng)絡(luò)編程中的HTTP請求,需要的朋友可以參考下
    2015-09-09
  • Spring體系的各種啟動流程詳解

    Spring體系的各種啟動流程詳解

    這篇文章主要給大家介紹了關(guān)于Spring體系的各種啟動流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Spring?Boot中的過濾器攔截器監(jiān)聽器使用技巧匯總

    Spring?Boot中的過濾器攔截器監(jiān)聽器使用技巧匯總

    本文將介紹在Spring?Boot應(yīng)用程序中使用過濾器、攔截器和監(jiān)聽器的使用技巧,我們將討論它們之間的區(qū)別,以及何時使用它們,我們還將提供代碼示例,以幫助您在自己的應(yīng)用程序中使用它們
    2023-12-12
  • Springboot打包部署修改配置文件的方法

    Springboot打包部署修改配置文件的方法

    這篇文章主要介紹了Springboot打包部署修改配置文件的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 詳解Java中的內(nèi)存屏障

    詳解Java中的內(nèi)存屏障

    這篇文章主要介紹了Java中的內(nèi)存屏障的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-05-05
  • java實(shí)現(xiàn)將結(jié)果集封裝到List中的方法

    java實(shí)現(xiàn)將結(jié)果集封裝到List中的方法

    這篇文章主要介紹了java實(shí)現(xiàn)將結(jié)果集封裝到List中的方法,涉及java數(shù)據(jù)庫查詢及結(jié)果集轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • Java instanceof和getClass()區(qū)別實(shí)例解析

    Java instanceof和getClass()區(qū)別實(shí)例解析

    這篇文章主要介紹了Java instanceof和getClass()區(qū)別實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • Java遞歸算法經(jīng)典實(shí)例(經(jīng)典兔子問題)

    Java遞歸算法經(jīng)典實(shí)例(經(jīng)典兔子問題)

    本文主要對經(jīng)典的兔子案例分析,來進(jìn)一步更好的理解和學(xué)習(xí)java遞歸算法,具有很好的參考價值,需要的朋友一起來看下吧
    2016-12-12
  • 詳細(xì)總結(jié)Java創(chuàng)建文件夾的方法及優(yōu)缺點(diǎn)

    詳細(xì)總結(jié)Java創(chuàng)建文件夾的方法及優(yōu)缺點(diǎn)

    很多小伙伴都不知道如何用Java創(chuàng)建文件夾,今天給大家整理了這篇文章,文中有非常詳細(xì)的方法介紹及方法的優(yōu)缺點(diǎn),對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05

最新評論