HashMap基本上就是一个Key Value的存储方式,内部就是一个数组加单链表的结构。存数据时,通过计算key值hash值找到对应数组的下标,然后放置到链表表头位置,如果内部容量已经超过了阈值,则进行两倍扩容,扩容时间重新计算hash,将旧数组中的元素搬到新的数组元素。取数据时候,计算hash值找到对应的数组下标,遍历链表得到对应的值。
基本成员变量
Entry[] table ----> 键值对数组
size ----> 键值对数量
threshold ----> 阈值
loadFactor ----> 负载因子
modCount ----> 记录修改次数,迭代时与expectedModCount对比,如果不相等则抛出异常
构造函数
默认负载因子是0.75,初始化容量是16,默认阈值是16*0.75 = 12;
public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); table = new Entry[DEFAULT_INITIAL_CAPACITY]; init(); }
public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); }
public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity > MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal load factor: " + loadFactor); // Find a power of 2 >= initialCapacity //确保数组长度是2次幂(hash时计算下标) int capacity = 1; while (capacity < initialCapacity) capacity <<= 1; this.loadFactor = loadFactor; threshold = (int)(capacity * loadFactor); table = new Entry[capacity]; init(); }
主要函数
存放键值对: V put(K key, V value)
判断key值是否为空,如果为空,放入数组下标为0的地方
key值不为空,计算hash值,找到对应的数组下标
判断链表中是否包含对应的key值,已经包含则替换调原来的value,存入新的value值
否则,将键值对填入到对应的链表中,新元素添加到链表表头,如果当前元素数量超过阈值,则进行2倍扩容
public V put(K key, V value) { if (key == null) return putForNullKey(value); int hash = hash(key.hashCode()); int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, key, value, i); return null; }
private V putForNullKey(V value) { for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(0, null, value, 0); return null; }
static int indexFor(int h, int length) { //确保数组长度是2次幂 return h & (length-1); }
添加键值对到链表中,新元素添加到表头,如果当前元素数量超过阈值,则进行2倍扩容
void addEntry(int hash, K key, V value, int bucketIndex) { Entry<K,V> e = table[bucketIndex]; table[bucketIndex] = new Entry<K,V>(hash, key, value, e); if (size++ >= threshold) resize(2 * table.length); }
Entry(int h, K k, V v, Entry<K,V> n) { value = v; next = n; key = k; hash = h; }
数组扩容原理
如果元素容量已经达到最大容量(2的30次幂),则不继续扩容
将旧的元素转移到新的数组中,转移时候会把旧的链表反序
重新设置阈值
void resize(int newCapacity) { Entry[] oldTable = table; int oldCapacity = oldTable.length; if (oldCapacity == MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return; } Entry[] newTable = new Entry[newCapacity]; transfer(newTable); table = newTable; threshold = (int)(newCapacity * loadFactor); }
void transfer(Entry[] newTable) { Entry[] src = table; int newCapacity = newTable.length; for (int j = 0; j < src.length; j++) { Entry<K,V> e = src[j]; if (e != null) { src[j] = null; do { Entry<K,V> next = e.next; int i = indexFor(e.hash, newCapacity); e.next = newTable[i]; newTable[i] = e; e = next; } while (e != null); } } }
获取键值对 V get(Object key)
key 值为空,则从数组下标为0的地方获取
key值不为空,计算对应的hash,找到对应的数组下标,遍历链表
public V get(Object key) { if (key == null) return getForNullKey(); int hash = hash(key.hashCode()); for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) return e.value; } return null; }
private V getForNullKey() { for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) return e.value; } return null; }
清空数组元素 void clear
public void clear() { modCount++; Entry[] tab = table; for (int i = 0; i < tab.length; i++) tab[i] = null; size = 0; }