HashMap是Java集合框架中最常用的数据结构之一,它基于哈希表实现,具备快速的插入、删除和查找操作。本文将详细解析HashMap的底层实现,包括其数据结构、主要方法及其工作原理,并探讨一些常见问题和优化策略。
一、HashMap的数据结构
HashMap内部主要通过数组和链表(在Java 8之后引入了红黑树)来存储数据。其基本结构如下:
- 数组(table):存储哈希桶(bucket)的数组,数组中的每个元素是一个链表或红黑树的头节点。
- 链表:解决哈希冲突的主要方式,多个键值对可能存储在同一个桶中。
- 红黑树:当链表长度超过一定阈值(默认为8)时,链表转换为红黑树,以提升性能。
transient Node<K,V>[] table; // 存储桶的数组 transient int size; // 当前HashMap中的键值对数量 transient int modCount; // 结构修改次数 int threshold; // 阈值,table扩容的临界点 final float loadFactor; // 负载因子
二、构造函数
HashMap提供了多个构造函数,常用的有以下两个:
public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; // 默认负载因子0.75 } public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity > MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal load factor: " + loadFactor); this.loadFactor = loadFactor; this.threshold = tableSizeFor(initialCapacity); }
DEFAULT_LOAD_FACTOR
是默认负载因子,值为0.75。tableSizeFor
方法用于计算最近的2的幂次方的容量值。三、核心方法
- put方法
-
put
方法是HashMap中最重要的方法之一,用于插入键值对:public V put(K key, V value) { 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; if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; if ((p = tab[i = (n - 1) & hash]) == null) 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) { if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); if (binCount >= TREEIFY_THRESHOLD - 1) treeifyBin(tab, hash); break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } if (e != null) { V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
putVal
方法先计算键的哈希值,然后确定其在数组中的位置。如果该位置为空,直接插入;否则,通过链表或红黑树处理哈希冲突。 - get方法
-
get
方法用于根据键查找对应的值: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) { if (first.hash == hash && ((k = first.key) == key || (key != null && key.equals(k)))) return first; if ((e = first.next) != null) { if (first instanceof TreeNode) return ((TreeNode<K,V>)first).getTreeNode(hash, key); do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } while ((e = e.next) != null); } } return null; }
getNode
方法根据键的哈希值定位到数组中的位置,然后在对应的链表或红黑树中查找匹配的节点。 - resize方法
-
resize
方法用于扩容数组,当数组中的键值对数量超过阈值时触发:final Node<K,V>[] resize() { Node<K,V>[] oldTab = table; int oldCap = (oldTab == null) ? 0 : oldTab.length; int oldThr = threshold; int newCap, newThr = 0; if (oldCap > 0) { if (oldCap >= MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return oldTab; } else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) newThr = oldThr << 1; } else if (oldThr > 0) newCap = oldThr; else { newCap = DEFAULT_INITIAL_CAPACITY; newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } if (newThr == 0) { float ft = (float)newCap * loadFactor; newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE); } threshold = newThr; @SuppressWarnings({"rawtypes","unchecked"}) Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; table = newTab; if (oldTab != null) { for (int j = 0; j < oldCap; ++j) { Node<K,V> e; if ((e = oldTab[j]) != null) { oldTab[j] = null; if (e.next == null) newTab[e.hash & (newCap - 1)] = e; else if (e instanceof TreeNode) ((TreeNode<K,V>)e).split(this, newTab, j, oldCap); else { Node<K,V> loHead = null, loTail = null; Node<K,V> hiHead = null, hiTail = null; Node<K,V> next; do { next = e.next; if ((e.hash & oldCap) == 0) { if (loTail == null) loHead = e; else loTail.next = e; loTail = e; } else { if (hiTail == null) hiHead = e; else hiTail.next = e; hiTail = e; } } while ((e = next) != null); if (loTail != null) { loTail.next = null; newTab[j] = loHead; } if (hiTail != null) { hiTail.next = null; newTab[j + oldCap] = hiHead; } } } } } return newTab; }
resize
方法通过创建一个新的更大的数组,并将旧数组中的元素重新分配到新数组中。旧数组中的元素要么保持在原位置,要么移动到新位置(索引加上旧容量)。四、红黑树的引入
在Java 8中,引入了红黑树以优化链表的性能。当一个桶中的节点数量超过阈值(8个)时,链表将转换为红黑树:
final void treeifyBin(Node<K,V>[] tab, int hash) { int n, index; Node<K,V> e; if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) resize(); else