2021-03-25 17:14:57 +08:00
|
|
|
# HashMap in Java
|
|
|
|
|
|
2021-03-31 10:40:06 +08:00
|
|
|
java 中 hashmap的实现原理。
|
2021-03-25 17:14:57 +08:00
|
|
|
|
2021-04-06 21:57:30 +08:00
|
|
|
```
|
|
|
|
|
public class HashMap<K,V> extends AbstractMap<K,V>
|
|
|
|
|
implements Map<K,V>, Cloneable, Serializable {
|
|
|
|
|
|
|
|
|
|
static class Node<K,V> implements Map.Entry<K,V> {
|
|
|
|
|
final int hash;
|
|
|
|
|
final K key;
|
|
|
|
|
V value;
|
|
|
|
|
Node<K,V> next;
|
|
|
|
|
...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
|
|
|
|
|
TreeNode<K,V> parent; // red-black tree links
|
|
|
|
|
TreeNode<K,V> left;
|
|
|
|
|
TreeNode<K,V> right;
|
|
|
|
|
TreeNode<K,V> prev; // needed to unlink next upon deletion
|
|
|
|
|
boolean red;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public V put(K key, V value) {
|
|
|
|
|
return putVal(hash(key), key, value, false, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public V get(Object key) {
|
|
|
|
|
Node<K,V> e;
|
|
|
|
|
return (e = getNode(hash(key), key)) == null ? null : e.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 扩容
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|