手工实现HashMap1_基本结构_put存储键值对
节点类
public class Node2 {
int hash;
Object key;
Object value;
Node2 next;
}
基本结构_put方法
public class SxtHashMap1 {
Node2[] table;//位桶数组 bucket array
int size;//存放键对个数
public SxtHashMap1() {
table = new Node2[16];//长度一般为2的整数次幂
}
public void put(Object key,Object value) {
Node2 newNode = new Node2();
newNode.hash = myHash(key.hashCode(),table.length);
newNode.key = key;
newNode.value = value;
Node2 temp = table[newNode.hash];
if(temp==null) {
table[newNode.hash] = newNode;
}else {
}
}
public static int myHash(int v,int length) {
return v&(length-1);
}
public static void main(String[] args) {
SxtHashMap1 map1 = new SxtHashMap1();
map1.put(1001, "阿狸");
map1.put(1002, "阿卡丽");
map1.put(1003, "瑞雯");
System.out.println(map1);
}
}
采用断点方式查看是否存储成功
效果如图