1 data structure of HASHMAP
HashMap is actually an array of linked lists, the outermost is an array, and the elements of the array are linked lists.
2 HashMap's Put method:
The source code is as follows:
<span style= "font-size:10px;" > Public v put (K key, V value) { //1 If key is null put to key null position
if (key = null) return Putfornullkey (value);
//2 calculation key hash value int hash = hash (Key.hashcode ());
//3 get index position int i = indexfor (hash, table.length); //4 traversing the list of index locations, overwriting and returning old values if they exist, and if not, create a new location insert for (ENTRY<K,V&G T e = Table[i]; e!= null; E = e.next) { Object K; //4.1 key location has been The old value is given the new value and return the old value if (E.hash = = Hash && (k = e.key) = = Key | | key.equ ALS (k)) { V oldValue = e.value &NBSP ; E.value = value;
e.recordaccess (this); Retu
RN OldValue; { } //4.2 call a If key is new
Ddentry inserts new key and value modcount++;
addentry (hash, key, value, I);
return null; }</span>
3 source code is as follows HashMap get method:
The source code is as follows:
<span style= "font-size:10px;" > Public V get (Object key) {
//1 if key is null to return the value of the key null directly if
(key = null) returns
Getfornullkey ();
//2 calculates the hash value of the key object
int hash = hash (Key.hashcode ());
for (entry<k,v> e = table[indexfor (hash, table.length)];//Find the index e!= null by calculating the hash of the key
;
E = e.next) {//Find traversal list Object K in index
;
Find key position by equals comparison if key is found and returns value if
(E.hash = Hash && ((k = e.key) = = Key | | key.equals (k)) return
E.value;
}
return null;
} </span>
Analysis of 4 Concurrenthashmap:
Mainly to divide the segment
Resources:
Java Improvement article (two or three)-----HASHMAP
The Concurrenthashmap of source analysis