/**
* 自定义 map 升级版
* 1.提高查询的效率
*
* @author 大护法
*
*/
public class Demo02 {
LinkedList[] arr = new LinkedList[999];
int size;
//插入一个 key ,value值
public void put(Object key, Object value){
SxtEnty e = new SxtEnty(key, value);
int a = key.hashCode()/arr.length;
//如果之前没有这个hash
if(arr[a] == null){
//申请一个LinkedList的空间
LinkedList list = new LinkedList();
arr[a] = list;
list.add(e);
}else{
LinkedList list = arr[a];
for(int i=0; i<list.size(); i++){
SxtEnty e2 = (SxtEnty)list.get(i);
if(e.key.equals(key)){
e2.value = value; //键值的覆盖
}
}
arr[a].add(e);
}
}
//返回key所对应的value
public Object get (Object key){
int a = key.hashCode()/arr.length;
if(arr[a] != null){
LinkedList list = arr[a];
for(int i=0; i<list.size(); i++){
SxtEnty e = (SxtEnty)list.get(i);
if(e.key.equals(key)){
return e.value;
}
}
}
return null;
}
public static void main(String[] args) {
Map map = new HashMap();
map.put("1", "qwweqe");
map.put("2", "asdasd");
map.containsKey("2");
}
}