public class MyHashMap<K,V> {
Node<K,V>[] table;
class Node<K,V> {
int hash;
K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K, V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
}
//长度
int size;
//修改次数
int modCount;
//阈值
int threshold;
//负载因子
float loadFactor;
public MyHashMap() {
this.loadFactor = 0.75f;
}
public V put(K key, V value){
//hash
int hash = key.hashCode() ^ key.hashCode() >>> 16;
//index
int n = table.length;
int index = hash % n;
//table[index] == null -> new node
if(table[index] == null){
table[index] = new Node<>(hash, key, value, null);
}
//table[index].key == key -> table[index].value = value
else if(table[index].hash == hash && table[index].key == key){
table[index].value = value;
}
//table[index] instanceof TreeNode -> putTreeVal()
else if(table[index] instanceof TreeNode){
}
//foreach table[index].next
else {
//table[index].next.key == key -> table[index].next.value = value
//table[index].next == null -> table[index].next == new node
//binCount >= TREEIFY_THRESHOLD - 1 -> treeifyBin(table, hash);
}
modCount = modCount + 1;
size = size + 1;
if(size > threshold){
//resize()
}
return null;
}
public V get(K key){
//hash
int hash = key.hashCode() ^ key.hashCode() >>> 16;
//index
int n = table.length;
int index = hash % n;
//table[index] == null -> return null
if(table[index] == null){
return null;
}
//table[index].key == key -> return table[index].value
else if(table[index].hash == hash && table[index].key == key){
return table[index].value;
}
//table[index] instanceof TreeNode -> getTreeNode(key)
else if(table[index] instanceof TreeNode){
// return getTreeNode(key);
}
//foreach table[index].next
else {
//table[index].next.key == key -> return table[index].next.value
//table[index].next == null -> return null;
}
return null;
}
}