在前面的章節中,我們已經探討了 ArrayList 和 LinkedList,它們查找元素的效率均較低,需要逐個比較。本章將深入剖析 HashMap——一種查找效率極高(接近 O(1))的鍵值對存儲結構。我們將從 Map 接口定義、HashMap 使用示例、構造方法、核心實現原理(數組+鏈表+哈希)、擴容機制、關鍵方法源碼分析、特點總結等多個維度進行詳細闡述。
Map 存儲鍵值對,一個鍵映射一個值,鍵不可重複,重複 put 會覆蓋舊值。典型應用場景:
數組、List 可視為特殊的 Map:索引為鍵,元素為值。
public interface Map<K,V> {
// 基礎增刪改查
V put(K key, V value);
V get(Object key);
V remove(Object key);
// 狀態查詢
int size();
boolean isEmpty();
boolean containsKey(Object key);
boolean containsValue(Object value);
// 批量操作
void putAll(Map<? extends K, ? extends V> m);
void clear();
// 視圖方法(返回集合視圖,非拷貝,修改視圖影響原 Map)
Set<K> keySet();
Collection<V> values();
Set<Map.Entry<K,V>> entrySet();
// 內部接口:鍵值對實體
interface Entry<K,V> {
K getKey();
V getValue();
V setValue(V value);
boolean equals(Object o);
int hashCode();
}
// Object 繼承方法
boolean equals(Object o);
int hashCode();
}
put 返回舊值,無舊值返回 nullkeySet()/values()/entrySet() 返回的是視圖,非拷貝,操作視圖直接影響原 Mapnull 或 equals() 返回 trueimport java.util.*;
public class WordFrequency {
public static void main(String[] args) {
Random rnd = new Random();
Map<Integer, Integer> countMap = new HashMap<>();
// 生成 1000 個 0~3 的隨機數並統計頻次
for (int i = 0; i < 1000; i++) {
int num = rnd.nextInt(4);
Integer count = countMap.get(num);
if (count == null) {
countMap.put(num, 1);
} else {
countMap.put(num, count + 1);
}
}
// 遍歷輸出
for (Map.Entry<Integer, Integer> kv : countMap.entrySet()) {
System.out.println(kv.getKey() + "," + kv.getValue());
}
// 典型輸出:0,269 1,236 2,261 3,234
}
}
| 構造方法 | 適用場景 | 備註 |
|---|---|---|
HashMap() | 通用場景,默認容量 16,負載因子 0.75 | 最常用 |
HashMap(int initialCapacity) | 預估數據量大,避免頻繁擴容 | 建議設為預估大小/0.75 |
HashMap(int initialCapacity, float loadFactor) | 精細控制空間換時間權衡 | loadFactor 0.5~0.9 之間 |
HashMap(Map<? extends K,? extends V> m) | 從現有 Map 複製構建 | 淺拷貝鍵值對 |
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE; // 哈希桶數組
transient int size; // 實際鍵值對數量
int threshold; // 擴容閾值 = capacity * loadFactor
final float loadFactor; // 負載因子,默認 0.75
// 靜態內部類:鏈表節點
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next; // 指向下一個節點(單向鏈表)
int hash; // 緩存 key 的 hash 值,加速比較
Entry(int h, K k, V v, Entry<K,V> n) {
value = v; next = n; key = k; hash = h;
}
}

public V put(K key, V value) {
// 1. 首次插入時初始化 table
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
// 2. null key 特殊處理(存放在 table[0])
if (key == null) return putForNullKey(value);
// 3. 計算 hash 值(二次擾動,提高均勻性)
int hash = hash(key);
// 4. 計算桶索引:h & (length-1) 等價於 h % length(length 為 2 的冪)
int i = indexFor(hash, table.length);
// 5. 遍歷鏈表查找是否已存在相同 key
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value; // 覆蓋舊值
e.recordAccess(this);
return oldValue;
}
}
// 6. 不存在則新增節點(頭插法)
modCount++;
addEntry(hash, key, value, i);
return null;
}
// hash 擾動函數:基於 key.hashCode() 進行位運算,降低碰撞
final int hash(Object k) {
int h = 0;
h ^= k.hashCode();
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
// 索引計算:利用 2 的冪特性,位運算替代取模
static int indexFor(int h, int length) {
return h & (length - 1);
}
// 初始化 table
private void inflateTable(int toSize) {
int capacity = roundUpToPowerOf2(toSize); // 向上取最接近的 2 的冪
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
table = new Entry[capacity];
}
當 size >= threshold 且目標桶位非空時觸發擴容:
void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length); // 容量翻倍
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
void resize(int newCapacity) {
Entry[] oldTable = table;
Entry[] newTable = new Entry[newCapacity];
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
threshold = (int) Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while (null != e) {
Entry<K,V> next = e.next;
if (rehash) e.hash = (e.key == null) ? 0 : hash(e.key);
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i]; // 頭插法遷移(注意:JDK 7 為頭插,JDK 8 改為尾插避免死循環)
newTable[i] = e;
e = next;
}
}
}
執行以下代碼:
Map<String,Integer> countMap = new HashMap<>();
countMap.put("hello", 1); // hash=96207088, index=0
countMap.put("world", 3); // hash=111207038, index=15
countMap.put("position", 4); // hash=771782464, index=0 (碰撞,頭插法)



public V get(Object key) {
if (key == null) return getForNullKey();
Entry<K,V> entry = getEntry(key);
return (entry == null) ? null : entry.getValue();
}
final Entry<K,V> getEntry(Object key) {
if (size == 0) return null;
int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) {
return e;
}
}
return null;
}
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
public boolean containsValue(Object value) {
if (value == null) return containsNullValue();
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
for (Entry e = tab[i]; e != null; e = e.next)
if (value.equals(e.value)) return true;
return false; // 值查找需全表掃描,O(n)
}
public V remove(Object key) {
Entry<K,V> e = removeEntryForKey(key);
return (e == null ? null : e.value);
}
final Entry<K,V> removeEntryForKey(Object key) {
if (size == 0) return null;
int hash = (key == null) ? 0 : hash(key);
int i = indexFor(hash, table.length);
Entry<K,V> prev = table[i];
Entry<K,V> e = prev;
while (e != null) {
Entry<K,V> next = e.next;
Object k;
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) {
modCount++; size--;
if (prev == e) table[i] = next; // 刪除頭節點
else prev.next = next; // 刪除中間/尾節點
e.recordRemoval(this);
return e;
}
prev = e;
e = next;
}
return null;
}
| 特性 | 表現 | 說明 |
|---|---|---|
| 查找/插入/刪除效率 | O(1) 級別 | 哈希定位 + 短鏈表遍歷 |
| 順序性 | 無序 | 哈希值隨機分佈,不保證插入/迭代順序 |
| null 支持 | 支持 1 個 null key,多個 null value | null key 固定存 table[0] |
| 線程安全 | 非線程安全 | 多線程環境用 ConcurrentHashMap 或 Collections.synchronizedMap |
| 適用場景 | 高頻鍵值查找、不需要順序 | 如緩存、索引、計數器、配置映射 |
LinkedHashMap(雙向鏈表維護順序)TreeMap(紅黑樹,O(log n))ConcurrentHashMap(CAS + synchronized + 節點鎖)HashMap 體現了計算機科學中極其重要的一種思維:**用空間換時間,用哈希值將「與所有元素比較」轉化為「直接定位 + 少量比較」**。
int hash,再調用 equals(),大幅降低比較開銷equals() 相等的對象,hashCode() 必須相等(否則無法正確查找)本章提到了 Set 接口,下一章我們將探討其重要實現類 HashSet,並揭秘它如何基於 HashMap 實現「不重複集合」語義。
未完待續,關注微信公眾號「老馬說編程」,從入門到高級,深入淺出,一起探索 Java 編程與計算機技術本質。
