Java手写简易版HashMap的使用(存储+查找)

网友投稿 299 2022-12-16


Java手写简易版HashMap的使用(存储+查找)

HashMap的基本结构

package com.liuyuhe;

public class Node {

int hash;

Object key;

Object value;

Node next;

}

package com.liuyuhe;

public class MyHashMap {

Node[] table; //位桶数组

int size; //存放键值对的个数

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;

temhttp://p=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内容

@Override

public String toString() {

KfzDbR StringBuilder sb = new StringBuilder();

sb.append("{");

boolean isFirst=true;

//遍历数组

for(int i=0;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 {

int hash;

K key;

V value;

Node next;

}

package com.liuyuhe;

public class MyHashMap {

Node[] table; //位桶数组

int size; //存放键值对的个数

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(),tableKfzDbR.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;

//遍历数组

for(int i=0;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();

}

}

//遍历链表

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 {

int hash;

K key;

V value;

Node next;

}

package com.liuyuhe;

public class MyHashMap {

Node[] table; //位桶数组

int size; //存放键值对的个数

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(),tableKfzDbR.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;

//遍历数组

for(int i=0;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();

}

}

//遍历链表

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();

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring web集成rabbitmq代码实例
下一篇:Java处理异常2种机制关键字区别解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~