定义一个接口,对外暴露快速存取的方法。
注意MyMap接口内部定义了一个内部接口Entry。
/**
* 定义接口
*/
public interface Map<K,V> {
public V put(K k , V v);
public V get(K k);
public int size();
public interface Entry<K,V>{
public K getKey();
public V getValue();
}
}
public class MyHashMap<K,V> implements Map<K,V>{
//定义一个存储空间(数组)
private Entry<K,V>[] table = null;
//定义一个初始的容量
private static int defalutLenth = 16;
//定义一个初始的大小
private int size = 0;
//定义一个构造方法
public MyHashMap(){
//初始化table
table = new Entry[defalutLenth];
}
@Override
public V put(K k , V v){
//hash() 出来hash值
int index = hash(k);
//数组的长度 index值 即下标的位置
//通过这个位置找到我们的table对应的Entry对象
Entry entry = table[index];
//判断一下这个值存不存在
if(null == entry){
//如果为空,就直接填进去就可以了
table[index] = new Entry(k,v,null,index);
size++;
}else{
//如果存在了,用链表
table[index] = new Entry(k,v,entry,index);
}
return table[index].getValue();
}
private int hash(K k){
int index = k.hashCode()%(defalutLenth-1);
return Math.abs(index);
}
@Override
public V get(K k){
if(size==0){
return null;
}
//hash() 出来hash值
int index = hash(k);
//数组的长度 index值 即下标的位置
//通过这个位置找到我们的table对应的Entry对象
Entry<K,V> entry = getEntry(k ,index);
//拿到这个元素,判断一下
return entry==null ? null : entry.getValue();
}
private Entry getEntry(K k , int index){
for(Entry e=table[index];e!=null;e=e.next){
if(e.hash == index && (k==e.getKey() || k.equals(e.getKey()))){
return e;
}
}
return null;
}
@Override
public int size(){
return size;
}
class Entry<K,V> implements Map.Entry<K,V>{
private K k;
private V v;
private Entry<K,V> next;
private int hash;
//构造函数
public Entry(K k , V v , Entry<K,V> next, int hash){
this.k = k;
this.v = v;
this.next = next;
this.hash = hash;
}
@Override
public K getKey(){
return k;
}
@Override
public V getValue(){
return v;
}
}
}
别人的实现:https://blog.csdn.net/huangshulang1234/article/details/79713303