PUT:
1. First hash the hashCode() of the key, and then calculate the index.
2. If there is no collision, put it directly in the bucket.
3. If there is a collision, there are buckets in the form of linked lists.
4. If the list is too long due to collision, convert the list to a red black tree.
5. If the node already exists, replace the old value (to ensure the uniqueness of the key).
6. If the bucket is full (more than load factor*current capacity), resize it.
public V put(K key, V value) { // hash the hashCode() of the key return putVal(hash(key), key, value, false, true); } final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; // Create if tab is empty if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; // Calculate index and handle null if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); else { Node<K,V> e; K k; // Node existence if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; // The chain is a tree. else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); // The chain is a list else { for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } // Write in if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; // load factor*current capacity, resize exceeded if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
GET:
1. The first node in the bucket, directly hit;
2. If there is a conflict, use key.equals(k) to find the corresponding entry
3. If it is a tree, search through key.equals(k) in the tree, O(logn);
4. If it is a linked list, search through key.equals(k) in the linked list, O(n).
public V get(Object key) { Node<K,V> e; return (e = getNode(hash(key), key)) == null ? null : e.value; } final Node<K,V> getNode(int hash, Object key) { Node<K,V>[] tab; Node<K,V> first, e; int n; K k; if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) { // Direct hit if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) return first; // Missed hit if ((e = first.next) != null) { // get in trees if (first instanceof TreeNode) return ((TreeNode<K,V>)first).getTreeNode(hash, key); // get in the linked list do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } while ((e = e.next) != null); } } return null; }