If java7 and java8 of hashmap source code analysis,
1. view the blog:
https://www.cnblogs.com/jajian/p/10385063.html#autoid-0-0-0,
2. If no sense of character class, you can watch the video:
https://www.bilibili.com/video/av71408100?from=search&seid=805805466626935035
The following is a summary of self-learning notes
1.java7, HashMap configuration (array + chain)
2. "modulo" operation consumes relatively large, the hash value using the displacement mode of operation
static int indexFor(int h, int length) { return h & (length-1); }
3. hash collision (hashCode if two different objects of the same, this case is called hash collision), will produce the single-chain structure alone (if the hash value is generally in the same array will create a single chain store)
4.hashmap three parameters indicate the source
Capacity : the current capacity of the array, always keep 2 ^ n, the expansion can, after expansion to the current size of the array twice.
loadFactor : load factor, the default is 0.75 (load factor indicating the degree of fill Hash table of elements.).
threshold: expansion threshold, equal capacity * loadFactor.
The source code analysis
public V PUT (Key K, V value) { // when inserted into the first element, the need to initialize the array size IF (Table == EMPTY_TABLE) { inflateTable(threshold); } // if the key is null, may be interested in looking in, it will eventually put the entry table [0] in the IF (key == null ) return putForNullKey (value); // 1. seek key of hash value int = the hash the hash (key); // 2. find the corresponding array index int I = indexFor (the hash, table.length); // 3. click traversing the list at the corresponding index to see if there already exists duplicate key , // if any, direct coverage, PUT method returns the old value is over for (the Entry <K, V> Table E = [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 ++ ; // 4. no duplicate key, entry is added to this list, the details behind said the addEntry (the hash, Key, value, I); return null ; }
1. The array initialization (inflateTable)
Private void inflateTable ( int toSize) { // ensure that the array size must be a power of 2 n. // example initialized as: new HashMap (20), the process is an initial size of the array 32 int Capacity = roundUpToPowerOf2 (toSize); // calculate the expansion threshold: Capacity * loadFactor threshold = ( int ) Math.min (Capacity * loadFactor, +. 1 MAXIMUM_CAPACITY ); // be initialized array bar Table = new new the Entry [Capacity]; initHashSeedAsNeeded(capacity); //ignore }
2. Add the nodes to the list (the addEntry)
void the addEntry ( int the hash, K Key, V value, int bucketIndex) { // if the current HashMap size has reached a threshold value, and the new value to be inserted in the array location has elements, then to expansion IF ((size> = threshold ) && ( null =! the Table [bucketIndex])) { // expansion, would later introduce of a resize (2 * table.length); // expansion in the future, to recalculate the hash hash = ( null !? = Key) hash ( Key): 0 ; // new subscript after expansion is recalculated bucketIndex = indexFor (the hash, table.length); } // look down createEntry (hash, key, value, bucketIndex); } // This is very simple, in fact, the new value into the header of the list, then ++ size void createEntry ( int hash, Key K, V value, int bucketIndex) { Entry<K,V> e = table[bucketIndex]; table[bucketIndex] = new Entry<>(hash, key, value, e); size++; }
3. Expansion Array (resize)
void resize(int newCapacity) { Entry[] oldTable = table; int oldCapacity = oldTable.length; if (oldCapacity == MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return; } // new array the Entry [] = NewTable new new the Entry [newCapacity]; // the value of the original array migrate to a new larger array transfer (newTable, initHashSeedAsNeeded (newCapacity) ); table = newTable; threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1); }
4.get process analysis
The calculated hash value key.
Find the corresponding array subscript: hash & (length - 1) .
Traversing the list at the array position until the key is equal to find (or the equals ==) a.
public V GET (Object Key) { // said before, key is null, it will be put table [0], so long as the list traverse the table [0] will be at the IF (Key == null ) return getForNullKey (); // the entry <K, V> = entry getEntry (Key); return null == entry ? null : entry.getValue(); }
getEntry(key):
final Entry<K,V> getEntry(Object key) { if (size == 0) { return null; } int the hash = (Key == null ) 0? : the hash (Key); // determine the array index, and then traverse the list from the beginning, until it finds for (the Entry <K, V> E = Table [indexFor (the hash, Table .length)]; and ! = null ; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } return null; }
There are other questions (HashMap the hash function h & (length-1) Explanation) for the array index, may refer to:
https://blog.csdn.net/sd_csdn_scy/article/details/55510453