目录
1.定义MyMap接口(可以理解为HashMap实现的Map接口)
1.定义MyMap接口(可以理解为HashMap实现的Map接口)
public interface MyMap<K, V> {
/**
* 定义put方法
*
* @param k
* @param v
* @return
*/
V put(K k, V v);
/**
* 定义get 方法
*
* @param k
* @return
*/
V get(K k);
/**
* 定义长度
*
* @return
*/
int size();
/**
* 删除一个元素
* @param k
* @return
*/
boolean remove(K k);
/**
* 定义规范
*
* @param <K>
* @param <V>
*/
interface Entry<K, V> {
K getKey();
V getValue();
}
}
2 实现接口及参数解释
public class MyHashMap<K, V> implements MyMap<K, V> {
//容器
private Entry<K, V>[] table = null;
private int mapLength = 16;
//计算临界值负载因子
private static final double CRITICAL_VALUE = 0.75;
//临界值 扩容用的
private int threshold = (int) Math.ceil(mapLength * CRITICAL_VALUE);
private int size = 0;
public MyHashMap() {
table = new Entry[mapLength];
}
public MyHashMap(int mapLength) {
this.mapLength = mapLength;
}
class Entry<K, V> implements MyMap.Entry<K, V> {
K key;
V value;
int index;
Entry<K, V> next;
public Entry(K key, V value, int index, Entry<K, V> next) {
this.key = key;
this.value = value;
this.index = index;
this.next = next;
}
@Override
public K getKey() {
return key;