hashmap的基本原理:
首先建立一个所有元素都是链表的数组(链表的root node),当添加一个元素时,首先计算该元素key值的hash,然后通过哈希值和数组的长度计算出该元素需要放入的位置,当同一位置有多个元素时,元素之间就形成链表。
1,构造器;
该简易hashmap使用默认的Capacity和Factor,并且有两种Entry构造器使得hashmap可以被传入key和value同为null的元素。
public class HashMap<K, V> {
private Entry<K, V>[] table;
private static final Integer Capacity = 16;
private Integer size = 0;
private static final float Factor = 0.75f;
public HashMap() {
this.table = new Entry[Capacity];
}
class Entry<K, V> {
private K k;
private V v;
private Entry next;
public Entry(Entry next) {
this.k = null;
this.v = null;
this.next = next;
}
public Entry(K key, V value, En