欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-02 09:22:35
hashmap的基本结构 package com.liuyuhe; public class node { int hash; object key;...

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;
   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内容

@override
 public string tostring() {
 stringbuilder sb = new stringbuilder();
 sb.append("{");
 boolean isfirst=true;
 //遍历数组
 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; //位桶数组
 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(),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;
 //遍历数组
 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();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。