数据结构HashMap
作为一个大一新生自己写的HashMap
因为HashMap是Java比较重要的一个集合,通过我对数据结构的学习,也为了跟好的了解JavaHashMap源码,通过链表+数组自己做了一个简易的HashMap,可能有很多问题希望不要喷,毕竟我终是一个大一新生
public class HashListMap{
private static HashListMap hashListMap=null;
public static synchronized HashListMap hashListMap(int SizeNode){
if(hashListMap==null){
hashListMap=new HashListMap(SizeNode);
}
return hashListMap;
}
public class Node{
String key;
String value;
Node next;
public Node(String key,String value){
this.key=key;
this.value=value;
}
}
private int SizeNode=0;
//传入你要设置的链表长度
public HashListMap(int SizeNode){
this.SizeNode=SizeNode;
head=new Node[SizeNode];
tail=new Node[SizeNode];
}
Node[] head;
Node[] tail;
//计算hash值
public int hash(String key){
return key==null?0:key.hashCode()&(SizeNode-1);
}
public void insert(String key,String Value){
Node node=new Node(key,Value);
int hash=this.hash(key);
if(head[hash]==null&&tail[hash]==null){
tail[hash]=node;
head[hash]=tail[hash];
}else{
tail[hash].next=node;
tail[hash]=node;
}
}
public String Get(String key){
int hash=this.hash(key);
Node cur=head[hash];
while (cur!=null&&cur.key!=key){
cur=cur.next;
}
return cur==null?null:cur.value;
}
public void GetNode(){
for(int i=0;i<SizeNode;i++){
Node cur=head[i];
while (cur!=null){
System.out.println(cur.key);
System.out.println(cur.value);
cur=cur.next;
}
}
}
}