HashMap
>>>: Move right
^: Bitwise XOR OR
&: Logic and
Final V putval (int hash, K key, V value, Boolean onlyifabsent, & nbsp; Boolean evict) { node<k,v>[] tab; Node<k,v> p; int N, i; if (tab = table) = = NULL | | (n = tab.length) = = 0)//First initialization tab n = (tab = Resize () ) .length; if (p = tab[i = (n-1) & hash]) = = = NULL)//determines the storage location based on the hash value of the key   ; Tab[i] = NewNode (hash, key, value, NULL); Else { node<k ,v> e; K k; if (P.hash = = Hash && ((k = p.key) = = Key | | (Key! = null && key.equals (k)))) e = p; else if (P instanceof TreeNode) e = ((treenode<k,v>) p). Puttreeval (This, tab, hash, key, value); else { for (int bincount = 0;; ++bincount) { & nbsp; if ( (e = p.next) = = null) { P.next = NewNode (hash, key, value, NULL); if ( Bincount >= treeify_threshold-1)//-1 for 1st & nbsp; treeifyBin (tab, Hash); break; } if (E.hash = = Hash && ( K = e.key) = = Key | | (Key! = null && key.equals (k)))) break; p = e; } } if (E! = null) {//existing mapping for key & nbsp; V oldValue = e.value; if (!onlyifabsent | | oldValue = = NULL) E.value = value; afternodeaccess (e); return oldvalue; } } ++modcount; if (++size > Threshold) Resize (); Afternodeinsertion (evict); return null; }
each time you put an element into HashMap, a node object is new, and the position is determined by calculating the hashcode value of the key. If two elements have the same hashcode, they will be placed on the same index. The problem arises, how do you put it? Originally it was stored in the form of a list (LinkedList) (logically).
The hash of key is worthy of calculation method
Static final int hash (Object key) { int h; return (key = = null)? 0: (H = key.hashcode ()) ^ (h >>> 16); Move right first, then bitwise XOR. }
Java Collection-hashmap