看源码可以知道HashMap内部是由一个 Entry[] table组成
Entry的定义如下
static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; final int hash; }
key、value
一个hash值,next指向下一个entry对象(后续会说为什么有这个next存在);
HashMap的put方法实现
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; }
根据key对象的hashCode进行哈希,hash方法
static int hash(int h) { // This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); }
因此,不同key的hash值可能是相同的,所以需要存在next指针指向下一个对象,也就是说对于hash相同的对象,是以链表的形式存储的。
HashMap的实现结构图如下
在使用HashMap时,最好显示声明HashMap的容量(initialCapacity),以及负载因(loadFactor)
initialCapacity在HashMap初始化时确定数组的大小;
threhold=loadFactor*initialCapacity,确定数组何时扩充容量,扩充容量是现在数组大小的2倍;
initialCapacity 默认大小是16
loadFactor是0.75
也就是说如果你按照以下方式一个HashMap
Map<K,V> map=new HashMap<K,V>();
当put的元素数目大于12的时候,可能就会导致HashMap需要扩容。扩容函数
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); }
看上面代码,resize是新建一个大数组,把原有的数据的entry重新哈希放到新数组中,很是浪费资源。所以在使用HashMap时尽量指定初始化大小;
LinkedHashMap实现原理
LinkedHashMap继承在HashMap,其中对Entry<K,V>对象进行了扩展,定义如下:
private static class Entry<K,V> extends HashMap.Entry<K,V> { // These fields comprise the doubly linked list used for iteration. Entry<K,V> before, after; Entry(int hash, K key, V value, HashMap.Entry<K,V> next) { super(hash, key, value, next); } }在原有HashMap.Entry<K,V>基础上加入了before、after两个指针,那么LinkedHashMap就变成了一个带有双向链表的数组;因此在Iterator时,LinkedHashMap的速度要高于HashMap,而且在Map的容量越大时,区别更明显;