本文共 11465 字,大约阅读时间需要 38 分钟。
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;static final int MAXIMUM_CAPACITY = 1 << 30;static final float DEFAULT_LOAD_FACTOR = 0.75f;/** An empty table instance to share when the table is not inflated. */static final Entry [] EMPTY_TABLE = {}; // 懒初始化使用的空数组,// 一个默认阈值。高于该值,且以String作为Key时,使用备用哈希hash函数(sun.misc.Hashing.stringHash32),这个备用hash函数能减少String的hash冲突。// 此值是最大int型,表明hash表再也不能扩容了,继续put下去,hash冲突会越来越多。// 至于为什么是String型才有这种特殊待遇,因为String是最常见的Key的类型。Integer虽然也很常见,但是Integer有范围限制,并且它的hashCode设计得就非常好(就是自身的数值)。// 可以通过定义系统属性jdk.map.althashing.threshold来覆盖这个值。// 值为1时,总是会对String类型的key使用备用的hash函数;值为-1,则一定不使用备用hash函数。static final int ALTERNATIVE_HASHING_THRESHOLD_DEFAULT = Integer.MAX_VALUE;
/** The table, resized as necessary. Length MUST Always be a power of two. */transient Entry[] table = (Entry []) EMPTY_TABLE; // 懒初始化,因此给一个空数组 ,其实空数组也没什么用,用null也行transient int size;final float loadFactor;transient int modCount;// 通常的扩容阈值,另外在懒初始化中,真正初始化时会使用这个值作为第一次的容量int threshold;// 一个hash种子,用于参与hash函数中的运算,获得更好的hash值来减少hash冲突// 值为0时会禁止掉备用hash函数transient int hashSeed = 0;private transient Set > entrySet = null;// keySet values继承使用AbstractMap的父类的属性
// Entry就一个变化,hash值不再是final的static class Entryimplements Map.Entry { final K key; V value; Entry next; int hash; // 就这一个改变,hash不再是final的,扩容时可以重新计算hash值 // 后面的跟1.6的一样,不说了}/** holds values which can't be initialized until after VM is booted. */// 这个类就是用来持有 ALTERNATIVE_HASHING_THRESHOLD的,影响String在极端情况下的hash值的计算,不影响HashMap基本的实现private static class Holder { /** Table capacity above which to switch to use alternative hashing. */ static final int ALTERNATIVE_HASHING_THRESHOLD; static { String altThreshold = java.security.AccessController.doPrivileged( new sun.security.action.GetPropertyAction( "jdk.map.althashing.threshold")); int threshold; try { threshold = (null != altThreshold) ? Integer.parseInt(altThreshold) : ALTERNATIVE_HASHING_THRESHOLD_DEFAULT; // disable alternative hashing if -1 if (threshold == -1) { threshold = Integer.MAX_VALUE; } if (threshold < 0) { throw new IllegalArgumentException("value must be positive integer."); } } catch(IllegalArgumentException failed) { throw new Error("Illegal value for 'jdk.map.althashing.threshold'", failed); } ALTERNATIVE_HASHING_THRESHOLD = threshold; }}
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); this.loadFactor = loadFactor; threshold = initialCapacity; // 初始容量保存在threshold中,真正初始化后threshold才是阈值 init(); // 这个方法什么也没做 // 初始化后数组table是默认值空数组,没有真正进行初始化}public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR);}public HashMap() { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);}// loadFactor使用默认值0.75f,因为m是接口类型,可能没有loadFactor这个属性public HashMap(Map m) { this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1, DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR); inflateTable(threshold); putAllForCreate(m);}下面是真正的初始化的相关代码。
// 使用比较高大上的算法求不小于number的满足2^n的数private static int roundUpToPowerOf2(int number) { // assert number >= 0 : "number must be non-negative"; return number >= MAXIMUM_CAPACITY ? MAXIMUM_CAPACITY : (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;}// 真正初始化HashMapprivate void inflateTable(int toSize) { // Find a power of 2 >= toSize int capacity = roundUpToPowerOf2(toSize); threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1); table = new Entry[capacity]; initHashSeedAsNeeded(capacity);}// 初始化hash种子// hashseed相关的可以不用理解太多,它只影响hash值的生成,以及扩容时是否需要重新计算hash值(rehash),本身对HashMap的基本的实现没影响final boolean initHashSeedAsNeeded(int capacity) { boolean currentAltHashing = hashSeed != 0; boolean useAltHashing = sun.misc.VM.isBooted() && (capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD); boolean switching = currentAltHashing ^ useAltHashing; if (switching) { hashSeed = useAltHashing ? sun.misc.Hashing.randomHashSeed(this) : 0; } return switching;}
// 多了个hashSeed,以及对String的特别处理// null 依然视为hash = 0,总是放在index = 0的hash桶中final int hash(Object k) { int h = hashSeed; if (0 != h && k instanceof String) { return sun.misc.Hashing.stringHash32((String) k); } h ^= k.hashCode(); // 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);}static int indexFor(int h, int length) { // assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2"; return h & (length-1);}
// resize 修复了threshold过早地变为Integer.MAX_VALUE的问题,其余跟1.6一致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, initHashSeedAsNeeded(newCapacity)); table = newTable; threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1); // 上面这行代码,可以避免1.6可能发生的因为newCapacity * LoadFacotr大于Integer.MAX_VALUE,导致后续本来还可以扩容,但是无法进入resize方法的问题}// 多一个真正的rehash的判断,其余跟1.6的一致void transfer(Entry[] newTable, boolean rehash) { int newCapacity = newTable.length; for (Entrye : table) { while(null != e) { Entry next = e.next; if (rehash) { // 为true就要进行真正的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; } }}
public int size() { return size;}public boolean isEmpty() { return size == 0;}public V get(Object key) { if (key == null) return getForNullKey(); Entryentry = getEntry(key); return null == entry ? null : entry.getValue();}private V getForNullKey() { if (size == 0) { return null; } for (Entry e = table[0]; e != null; e = e.next) { if (e.key == null) return e.value; } return null;}public boolean containsKey(Object key) { return getEntry(key) != null;}final Entry getEntry(Object key) { if (size == 0) { return null; } int hash = (key == null) ? 0 : hash(key); for (Entry e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } return null;}public boolean containsValue(Object value) { if (value == null) return containsNullValue(); Entry[] tab = table; for (int i = 0; i < tab.length ; i++) for (Entry e = tab[i] ; e != null ; e = e.next) if (value.equals(e.value)) return true; return false;}private boolean containsNullValue() { Entry[] tab = table; for (int i = 0; i < tab.length ; i++) for (Entry e = tab[i] ; e != null ; e = e.next) if (e.value == null) return true; return false;}
public V put(K key, V value) { if (table == EMPTY_TABLE) { // 考虑还未真正初始化的情况 inflateTable(threshold); } if (key == null) return putForNullKey(value); int hash = hash(key); int i = indexFor(hash, table.length); for (Entrye = 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 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;}// 这个跟1.6有些区别// 1、走实用主义,扩容多了个条件。当添加的节点是hash桶的第一个节点时,一定不扩容,所以会出现size > threshold的情况。// 2、几步的操作顺序不一样。jdk1.6的是先把节点添加到链表中,再判断是否扩容;1.7这里是先判断是否扩容,扩容完再把节点添加到链表中。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);}void createEntry(int hash, K key, V value, int bucketIndex) { Entry e = table[bucketIndex]; table[bucketIndex] = new Entry<>(hash, key, value, e); size++;}public void putAll(Map m) { int numKeysToBeAdded = m.size(); if (numKeysToBeAdded == 0) return; if (table == EMPTY_TABLE) { // 考虑还未真正初始化的情况 inflateTable((int) Math.max(numKeysToBeAdded * loadFactor, threshold)); } // 跟1.6的一样,稍微保守些,多判断下 if (numKeysToBeAdded > threshold) { int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1); if (targetCapacity > MAXIMUM_CAPACITY) targetCapacity = MAXIMUM_CAPACITY; int newCapacity = table.length; while (newCapacity < targetCapacity) newCapacity <<= 1; if (newCapacity > table.length) resize(newCapacity); } for (Map.Entry e : m.entrySet()) put(e.getKey(), e.getValue());}public V remove(Object key) { Entry e = removeEntryForKey(key); return (e == null ? null : e.value);}final Entry removeEntryForKey(Object key) { if (size == 0) { // 考虑还未真正初始化的情况 return null; } int hash = (key == null) ? 0 : hash(key); int i = indexFor(hash, table.length); Entry prev = table[i]; Entry e = prev; while (e != null) { Entry next = e.next; Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) { modCount++; size--; if (prev == e) table[i] = next; else prev.next = next; e.recordRemoval(this); return e; } prev = e; e = next; } return e;}public void clear() { modCount++; Arrays.fill(table, null); // 就是循环赋值 size = 0;}
转载地址:http://zgcws.baihongyu.com/