标签:
static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; int hash; Entry(int h, K k, V v, Entry<K,V> n) { value = v; next = n; key = k; hash = h; } }Entry类中保存了key、value、key的hash值和下一个Entry的引用(拉链法解决hash冲突)。
public V put(K key, V value) { if (key == null) return putForNullKey(value);//对key为空的值会被放在数组的下表为0的位置上,hash值也为0 int hash = hash(key); 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; }put的方法处理过程是先找这个key对应的hash是否存在,如果存在就判断key是否相同,如果key相同就用新的value替换旧的value。
void addEntry(int hash, K key, V value, int bucketIndex) { if ((size >= threshold) && (null != table[bucketIndex])) { resize(2 * table.length); hash = (null != key) ? hash(key) : 0; bucketIndex = indexFor(hash, table.length); } createEntry(hash, key, value, bucketIndex);//在指定的下标添加元素 }threshold是阈值的意思,这个值有两个参数决定:capacity(容量,默认16,以后所有的取值都是2的n次方), loadFactor(负载系数,默认0.75,简单来数就是空间利用率),threshold=capacity*loadFactor,例如默认情况下capacity=16,loadFactor=0.75,所以threshold=12,当HashMap中的元素个数大于等于12时,就有可能扩容,变为原来容量的两倍,用resize方法调整。
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]; boolean oldAltHashing = useAltHashing; useAltHashing |= sun.misc.VM.isBooted() && (newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD); boolean rehash = oldAltHashing ^ useAltHashing; transfer(newTable, rehash);//转移方法 table = newTable; threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1); }
void transfer(Entry[] newTable, boolean rehash) { int newCapacity = newTable.length; for (Entry<K,V> e : table) { while(null != e) { Entry<K,V> next = e.next; if (rehash) { e.hash = null == e.key ? 0 : hash(e.key); } int i = indexFor(e.hash, newCapacity); e.next = newTable[i]; newTable[i] = e; e = next; } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/w171203757/article/details/47734275