Java 集合系列 10 之 HashMap 详细介绍 (源码解析) 和使用示例
Java 集合系列 10 之 HashMap 详细介绍 (源码解析) 和使用示例
注:文末有第二部分,对哈希原理等做了进一步阐述
概要
这一章,我们对 HashMap 进行学习。
我们先对 HashMap 有个整体认识,然后再学习它的源码,最后再通过实例来学会使用 HashMap。
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3310835.html
第 1 部分 HashMap 介绍
HashMap 简介
HashMap 是一个散列表,它存储的内容是键值对 (key-value) 映射。
HashMap 继承于 AbstractMap,实现了 Map、Cloneable、java.io.Serializable 接口。
HashMap 的实现不是同步的,这意味着它不是线程安全的。它的 key、value 都可以为 null。此外,HashMap 中的映射不是有序的。
HashMap 的实例有两个参数影响其性能:“初始容量” 和 “加载因子”。容量 是哈希表中桶的数量,初始容量 只是哈希表在创建时的容量。加载因子 是哈希表在其容量自动增加之前可以达到多满的一种尺度。当哈希表中的条目数超出了加载因子与当前容量的乘积时,则要对该哈希表进行 rehash 操作(即重建内部数据结构),从而哈希表将具有大约两倍的桶数。
通常,默认加载因子是 0.75, 这是在时间和空间成本上寻求一种折衷。加载因子过高虽然减少了空间开销,但同时也增加了查询成本(在大多数 HashMap 类的操作中,包括 get 和 put 操作,都反映了这一点)。在设置初始容量时应该考虑到映射中所需的条目数及其加载因子,以便最大限度地减少 rehash 操作次数。如果初始容量大于最大条目数除以加载因子,则不会发生 rehash 操作。
HashMap 的构造函数
HashMap 共有 4 个构造函数, 如下:
[](javascript:void(0)😉
// 默认构造函数。
HashMap()
// 指定“容量大小”的构造函数
HashMap(int capacity)
// 指定“容量大小”和“加载因子”的构造函数
HashMap(int capacity, float loadFactor)
// 包含“子Map”的构造函数
HashMap(Map<? extends K, ? extends V> map)
[](javascript:void(0)😉
HashMap 的 API
[](javascript:void(0)😉
void clear()
Object clone()
boolean containsKey(Object key)
boolean containsValue(Object value)
Set<Entry<K, V>> entrySet()
V get(Object key)
boolean isEmpty()
Set<K> keySet()
V put(K key, V value)
void putAll(Map<? extends K, ? extends V> map)
V remove(Object key)
int size()
Collection<V> values()
[](javascript:void(0)😉
第 2 部分 HashMap 数据结构
HashMap 的继承关系
[](javascript:void(0)😉
java.lang.Object
↳ java.util.AbstractMap<K, V>
↳ java.util.HashMap<K, V>
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable { }
[](javascript:void(0)😉
HashMap 与 Map 关系如下图:
从图中可以看出:
(01) HashMap 继承于 AbstractMap 类,实现了 Map 接口。Map 是 "key-value 键值对" 接口,AbstractMap 实现了 "键值对" 的通用函数接口。
(02) HashMap 是通过 "拉链法" 实现的哈希表。它包括几个重要的成员变量:table, size, threshold, loadFactor, modCount。
table 是一个 Entry[] 数组类型,而 Entry 实际上就是一个单向链表。哈希表的 "key-value 键值对" 都是存储在 Entry 数组中的。
size 是 HashMap 的大小,它是 HashMap 保存的键值对的数量。
threshold 是 HashMap 的阈值,用于判断是否需要调整 HashMap 的容量。threshold 的值 ="容量 * 加载因子",当 HashMap 中存储数据的数量达到 threshold 时,就需要将 HashMap 的容量加倍。
loadFactor 就是加载因子。
modCount 是用来实现 fail-fast 机制的。
第 3 部分 HashMap 源码解析 (基于 JDK1.6.0_45)
为了更了解 HashMap 的原理,下面对 HashMap 源码代码作出分析。
在阅读源码时,建议参考后面的说明来建立对 HashMap 的整体认识,这样更容易理解 HashMap。
1 package java.util;
2 import java.io.*;
4 public class HashMap<K,V>
5 extends AbstractMap<K,V>
6 implements Map<K,V>, Cloneable, Serializable
7 {
9 // 默认的初始容量是16,必须是2的幂。
10 static final int DEFAULT_INITIAL_CAPACITY = 16;
12 // 最大容量(必须是2的幂且小于2的30次方,传入容量过大将被这个值替换)
13 static final int MAXIMUM_CAPACITY = 1 << 30;
15 // 默认加载因子
16 static final float DEFAULT_LOAD_FACTOR = 0.75f;
18 // 存储数据的Entry数组,长度是2的幂。
19 // HashMap是采用拉链法实现的,每一个Entry本质上是一个单向链表
20 transient Entry[] table;
22 // HashMap的大小,它是HashMap保存的键值对的数量
23 transient int size;
25 // HashMap的阈值,用于判断是否需要调整HashMap的容量(threshold = 容量*加载因子)
26 int threshold;
28 // 加载因子实际大小
29 final float loadFactor;
31 // HashMap被改变的次数
32 transient volatile int modCount;
34 // 指定“容量大小”和“加载因子”的构造函数
35 public HashMap(int initialCapacity, float loadFactor) {
36 if (initialCapacity < 0)
37 throw new IllegalArgumentException("Illegal initial capacity: " +
38 initialCapacity);
39 // HashMap的最大容量只能是MAXIMUM_CAPACITY
40 if (initialCapacity > MAXIMUM_CAPACITY)
41 initialCapacity = MAXIMUM_CAPACITY;
42 if (loadFactor <= 0 || Float.isNaN(loadFactor))
43 throw new IllegalArgumentException("Illegal load factor: " +
44 loadFactor);
46 // 找出“大于initialCapacity”的最小的2的幂
47 int capacity = 1;
48 while (capacity < initialCapacity)
49 capacity <<= 1;
51 // 设置“加载因子”
52 this.loadFactor = loadFactor;
53 // 设置“HashMap阈值”,当HashMap中存储数据的数量达到threshold时,就需要将HashMap的容量加倍。
54 threshold = (int)(capacity * loadFactor);
55 // 创建Entry数组,用来保存数据
56 table = new Entry[capacity];
57 init();
58 }
61 // 指定“容量大小”的构造函数
62 public HashMap(int initialCapacity) {
63 this(initialCapacity, DEFAULT_LOAD_FACTOR);
64 }
66 // 默认构造函数。
67 public HashMap() {
68 // 设置“加载因子”
69 this.loadFactor = DEFAULT_LOAD_FACTOR;
70 // 设置“HashMap阈值”,当HashMap中存储数据的数量达到threshold时,就需要将HashMap的容量加倍。
71 threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
72 // 创建Entry数组,用来保存数据
73 table = new Entry[DEFAULT_INITIAL_CAPACITY];
74 init();
75 }
77 // 包含“子Map”的构造函数
78 public HashMap(Map<? extends K, ? extends V> m) {
79 this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
80 DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
81 // 将m中的全部元素逐个添加到HashMap中
82 putAllForCreate(m);
83 }
//传入的也是对象的hashCode值,这里是二次哈希
85 static int hash(int h) {
86 h ^= (h >>> 20) ^ (h >>> 12);
87 return h ^ (h >>> 7) ^ (h >>> 4);
88 }
90 // 返回索引值
91 // h & (length-1)保证返回值的小于length
92 static int indexFor(int h, int length) {
93 return h & (length-1);
94 }
96 public int size() {
97 return size;
98 }
100 public boolean isEmpty() {
101 return size == 0;
102 }
104 // 获取key对应的value
105 public V get(Object key) {
106 if (key == null)
107 return getForNullKey();
108 // 获取key的hash值
109 int hash = hash(key.hashCode());
110 // 在“该hash值对应的链表”上查找“键值等于key”的元素
111 for (Entry<K,V> e = table[indexFor(hash, table.length)];
112 e != null;
113 e = e.next) {
114 Object k;
115 if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
116 return e.value;
117 }
118 return null;
119 }
121 // 获取“key为null”的元素的值
122 // HashMap将“key为null”的元素存储在table[0]位置!
123 private V getForNullKey() {
124 for (Entry<K,V> e = table[0]; e != null; e = e.next) {
125 if (e.key == null)
126 return e.value;
127 }
128 return null;
129 }
131 // HashMap是否包含key
132 public boolean containsKey(Object key) {
133 return getEntry(key) != null;
134 }
136 // 返回“键为key”的键值对
137 final Entry<K,V> getEntry(Object key) {
138 // 获取哈希值
139 // HashMap将“key为null”的元素存储在table[0]位置,“key不为null”的则调用hash()计算哈希值
140 int hash = (key == null) ? 0 : hash(key.hashCode());
141 // 在“该hash值对应的链表”上查找“键值等于key”的元素
142 for (Entry<K,V> e = table[indexFor(hash, table.length)];
143 e != null;
144 e = e.next) {
145 Object k;
146 if (e.hash == hash &&
147 ((k = e.key) == key || (key != null && key.equals(k))))
148 return e;
149 }
150 return null;
151 }
153 // 将“key-value”添加到HashMap中
154 public V put(K key, V value) {
155 // 若“key为null”,则将该键值对添加到table[0]中。
156 if (key == null)
157 return putForNullKey(value);
158 // 若“key不为null”,则计算该key的哈希值,然后将其添加到该哈希值对应的链表中。
159 int hash = hash(key.hashCode());
160 int i = indexFor(hash, table.length);
161 for (Entry<K,V> e = table[i]; e != null; e = e.next) {
162 Object k;
163 // 若“该key”对应的键值对已经存在,则用新的value取代旧的value。然后退出!
164 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
165 V oldValue = e.value;
166 e.value = value;
167 e.recordAccess(this);
168 return oldValue;
169 }
170 }
172 // 若“该key”对应的键值对不存在,则将“key-value”添加到table中
173 modCount++;
174 addEntry(hash, key, value, i);
175 return null;
176 }
178 // putForNullKey()的作用是将“key为null”键值对添加到table[0]位置
179 private V putForNullKey(V value) {
180 for (Entry<K,V> e = table[0]; e != null; e = e.next) {
181 if (e.key == null) {
182 V oldValue = e.value;
183 e.value = value;
184 e.recordAccess(this);
185 return oldValue;
186 }
187 }
188 // 这里的完全不会被执行到!
189 modCount++;
190 addEntry(0, null, value, 0);
191 return null;
192 }
194 // 创建HashMap对应的“添加方法”,
195 // 它和put()不同。putForCreate()是内部方法,它被构造函数等调用,用来创建HashMap
196 // 而put()是对外提供的往HashMap中添加元素的方法。
197 private void putForCreate(K key, V value) {
198 int hash = (key == null) ? 0 : hash(key.hashCode());
199 int i = indexFor(hash, table.length);
201 // 若该HashMap表中存在“键值等于key”的元素,则替换该元素的value值
202 for (Entry<K,V> e = table[i]; e != null; e = e.next) {
203 Object k;
204 if (e.hash == hash &&
205 ((k = e.key) == key || (key != null && key.equals(k)))) {
206 e.value = value;
207 return;
208 }
209 }
211 // 若该HashMap表中不存在“键值等于key”的元素,则将该key-value添加到HashMap中
212 createEntry(hash, key, value, i);
213 }
215 // 将“m”中的全部元素都添加到HashMap中。
216 // 该方法被内部的构造HashMap的方法所调用。
217 private void putAllForCreate(Map<? extends K, ? extends V> m) {
218 // 利用迭代器将元素逐个添加到HashMap中
219 for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
220 Map.Entry<? extends K, ? extends V> e = i.next();
221 putForCreate(e.getKey(), e.getValue());
222 }
223 }
225 // 重新调整HashMap的大小,newCapacity是调整后的单位
226 void resize(int newCapacity) {
227 Entry[] oldTable = table;
228 int oldCapacity = oldTable.length;
229 if (oldCapacity == MAXIMUM_CAPACITY) {
230 threshold = Integer.MAX_VALUE;
231 return;
232 }
234 // 新建一个HashMap,将“旧HashMap”的全部元素添加到“新HashMap”中,
235 // 然后,将“新HashMap”赋值给“旧HashMap”。
236 Entry[] newTable = new Entry[newCapacity];
237 transfer(newTable);
238 table = newTable;
239 threshold = (int)(newCapacity * loadFactor);
240 }
242 // 将HashMap中的全部元素都添加到newTable中
243 void transfer(Entry[] newTable) {
244 Entry[] src = table;
245 int newCapacity = newTable.length;
246 for (int j = 0; j < src.length; j++) {
247 Entry<K,V> e = src[j];
248 if (e != null) {
249 src[j] = null;
250 do {
251 Entry<K,V> next = e.next;
252 int i = indexFor(e.hash, newCapacity);
253 e.next = newTable[i];
254 newTable[i] = e;
255 e = next;
256 } while (e != null);
257 }
258 }
259 }
261 // 将"m"的全部元素都添加到HashMap中
262 public void putAll(Map<? extends K, ? extends V> m) {
263 // 有效性判断
264 int numKeysToBeAdded = m.size();
265 if (numKeysToBeAdded == 0)
266 return;
268 // 计算容量是否足够,
269 // 若“当前实际容量 < 需要的容量”,则将容量x2。
270 if (numKeysToBeAdded > threshold) {
271 int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
272 if (targetCapacity > MAXIMUM_CAPACITY)
273 targetCapacity = MAXIMUM_CAPACITY;
274 int newCapacity = table.length;
275 while (newCapacity < targetCapacity)
276 newCapacity <<= 1;
277 if (newCapacity > table.length)
278 resize(newCapacity);
279 }
281 // 通过迭代器,将“m”中的元素逐个添加到HashMap中。
282 for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
283 Map.Entry<? extends K, ? extends V> e = i.next();
284 put(e.getKey(), e.getValue());
285 }
286 }
288 // 删除“键为key”元素
289 public V remove(Object key) {
290 Entry<K,V> e = removeEntryForKey(key);
291 return (e == null ? null : e.value);
292 }
294 // 删除“键为key”的元素
295 final Entry<K,V> removeEntryForKey(Object key) {
296 // 获取哈希值。若key为null,则哈希值为0;否则调用hash()进行计算
297 int hash = (key == null) ? 0 : hash(key.hashCode());
298 int i = indexFor(hash, table.length);
299 Entry<K,V> prev = table[i];
300 Entry<K,V> e = prev;
302 // 删除链表中“键为key”的元素
303 // 本质是“删除单向链表中的节点”
304 while (e != null) {
305 Entry<K,V> next = e.next;
306 Object k;
307 if (e.hash == hash &&
308 ((k = e.key) == key || (key != null && key.equals(k)))) {
309 modCount++;
310 size--;
311 if (prev == e)
312 table[i] = next;
313 else
314 prev.next = next;
315 e.recordRemoval(this);
316 return e;
317 }
318 prev = e;
319 e = next;
320 }
322 return e;
323 }
325 // 删除“键值对”
326 final Entry<K,V> removeMapping(Object o) {
327 if (!(o instanceof Map.Entry))
328 return null;
330 Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
331 Object key = entry.getKey();
332 int hash = (key == null) ? 0 : hash(key.hashCode());
333 int i = indexFor(hash, table.length);
334 Entry<K,V> prev = table[i];
335 Entry<K,V> e = prev;
337 // 删除链表中的“键值对e”
338 // 本质是“删除单向链表中的节点”
339 while (e != null) {
340 Entry<K,V> next = e.next;
341 if (e.hash == hash && e.equals(entry)) {
342 modCount++;
343 size--;
344 if (prev == e)
345 table[i] = next;
346 else
347 prev.next = next;
348 e.recordRemoval(this);
349 return e;
350 }
351 prev = e;
352 e = next;
353 }
355 return e;
356 }
358 // 清空HashMap,将所有的元素设为null
359 public void clear() {
360 modCount++;
361 Entry[] tab = table;
362 for (int i = 0; i < tab.length; i++)
363 tab[i] = null;
364 size = 0;
365 }
367 // 是否包含“值为value”的元素
368 public boolean containsValue(Object value) {
369 // 若“value为null”,则调用containsNullValue()查找
370 if (value == null)
371 return containsNullValue();
373 // 若“value不为null”,则查找HashMap中是否有值为value的节点。
374 Entry[] tab = table;
375 for (int i = 0; i < tab.length ; i++)
376 for (Entry e = tab[i] ; e != null ; e = e.next)
377 if (value.equals(e.value))
378 return true;
379 return false;
380 }
382 // 是否包含null值
383 private boolean containsNullValue() {
384 Entry[] tab = table;
385 for (int i = 0; i < tab.length ; i++)
386 for (Entry e = tab[i] ; e != null ; e = e.next)
387 if (e.value == null)
388 return true;
389 return false;
390 }
392 // 克隆一个HashMap,并返回Object对象
393 public Object clone() {
394 HashMap<K,V> result = null;
395 try {
396 result = (HashMap<K,V>)super.clone();
397 } catch (CloneNotSupportedException e) {
398 // assert false;
399 }
400 result.table = new Entry[table.length];
401 result.entrySet = null;
402 result.modCount = 0;
403 result.size = 0;
404 result.init();
405 // 调用putAllForCreate()将全部元素添加到HashMap中
406 result.putAllForCreate(this);
408 return result;
409 }
411 // Entry是单向链表。
412 // 它是 “HashMap链式存储法”对应的链表。
413 // 它实现了Map.Entry 接口,即实现getKey(), getValue(), setValue(V value), equals(Object o), hashCode()这些函数
414 static class Entry<K,V> implements Map.Entry<K,V> {
415 final K key;
416 V value;
417 // 指向下一个节点
418 Entry<K,V> next;
419 final int hash;
421 // 构造函数。
422 // 输入参数包括"哈希值(h)", "键(k)", "值(v)", "下一节点(n)"
423 Entry(int h, K k, V v, Entry<K,V> n) {
424 value = v;
425 next = n;
426 key = k;
427 hash = h;
428 }
430 public final K getKey() {
431 return key;
432 }
434 public final V getValue() {
435 return value;
436 }
438 public final V setValue(V newValue) {
439 V oldValue = value;
440 value = newValue;
441 return oldValue;
442 }
444 // 判断两个Entry是否相等
445 // 若两个Entry的“key”和“value”都相等,则返回true。
446 // 否则,返回false
447 public final boolean equals(Object o) {
448 if (!(o instanceof Map.Entry))
449 return false;
450 Map.Entry e = (Map.Entry)o;
451 Object k1 = getKey();
452 Object k2 = e.getKey();
453 if (k1 == k2 || (k1 != null && k1.equals(k2))) {
454 Object v1 = getValue();
455 Object v2 = e.getValue();
456 if (v1 == v2 || (v1 != null && v1.equals(v2)))
457 return true;
458 }
459 return false;
460 }
462 // 实现hashCode()
463 public final int hashCode() {
464 return (key==null ? 0 : key.hashCode()) ^
465 (value==null ? 0 : value.hashCode());
466 }
468 public final String toString() {
469 return getKey() + "=" + getValue();
470 }
472 // 当向HashMap中添加元素时,绘调用recordAccess()。
473 // 这里不做任何处理
474 void recordAccess(HashMap<K,V> m) {
475 }
477 // 当从HashMap中删除元素时,绘调用recordRemoval()。
478 // 这里不做任何处理
479 void recordRemoval(HashMap<K,V> m) {
480 }
481 }
483 // 新增Entry。将“key-value”插入指定位置,bucketIndex是位置索引。
484 void addEntry(int hash, K key, V value, int bucketIndex) {
485 // 保存“bucketIndex”位置的值到“e”中
486 Entry<K,V> e = table[bucketIndex];
487 // 设置“bucketIndex”位置的元素为“新Entry”,
488 // 设置“e”为“新Entry的下一个节点”
489 table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
490 // 若HashMap的实际大小 不小于 “阈值”,则调整HashMap的大小
491 if (size++ >= threshold)
492 resize(2 * table.length);
493 }
495 // 创建Entry。将“key-value”插入指定位置,bucketIndex是位置索引。
496 // 它和addEntry的区别是:
497 // (01) addEntry()一般用在 新增Entry可能导致“HashMap的实际容量”超过“阈值”的情况下。
498 // 例如,我们新建一个HashMap,然后不断通过put()向HashMap中添加元素;
499 // put()是通过addEntry()新增Entry的。
500 // 在这种情况下,我们不知道何时“HashMap的实际容量”会超过“阈值”;
501 // 因此,需要调用addEntry()
502 // (02) createEntry() 一般用在 新增Entry不会导致“HashMap的实际容量”超过“阈值”的情况下。
503 // 例如,我们调用HashMap“带有Map”的构造函数,它绘将Map的全部元素添加到HashMap中;
504 // 但在添加之前,我们已经计算好“HashMap的容量和阈值”。也就是,可以确定“即使将Map中
505 // 的全部元素添加到HashMap中,都不会超过HashMap的阈值”。
506 // 此时,调用createEntry()即可。
507 void createEntry(int hash, K key, V value, int bucketIndex) {
508 // 保存“bucketIndex”位置的值到“e”中
509 Entry<K,V> e = table[bucketIndex];
510 // 设置“bucketIndex”位置的元素为“新Entry”,
511 // 设置“e”为“新Entry的下一个节点”
512 table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
513 size++;
514 }
516 // HashIterator是HashMap迭代器的抽象出来的父类,实现了公共了函数。
517 // 它包含“key迭代器(KeyIterator)”、“Value迭代器(ValueIterator)”和“Entry迭代器(EntryIterator)”3个子类。
518 private abstract class HashIterator<E> implements Iterator<E> {
519 // 下一个元素
520 Entry<K,V> next;
521 // expectedModCount用于实现fast-fail机制。
522 int expectedModCount;
523 // 当前索引
524 int index;
525 // 当前元素
526 Entry<K,V> current;
528 HashIterator() {
529 expectedModCount = modCount;
530 if (size > 0) { // advance to first entry
531 Entry[] t = table;
532 // 将next指向table中第一个不为null的元素。
533 // 这里利用了index的初始值为0,从0开始依次向后遍历,直到找到不为null的元素就退出循环。
534 while (index < t.length && (next = t[index++]) == null)
535 ;
536 }
537 }
539 public final boolean hasNext() {
540 return next != null;
541 }
543 // 获取下一个元素
544 final Entry<K,V> nextEntry() {
545 if (modCount != expectedModCount)
546 throw new ConcurrentModificationException();
547 Entry<K,V> e = next;
548 if (e == null)
549 throw new NoSuchElementException();
551 // 注意!!!
552 // 一个Entry就是一个单向链表
553 // 若该Entry的下一个节点不为空,就将next指向下一个节点;
554 // 否则,将next指向下一个链表(也是下一个Entry)的不为null的节点。
555 if ((next = e.next) == null) {
556 Entry[] t = table;
557 while (index < t.length && (next = t[index++]) == null)
558 ;
559 }
560 current = e;
561 return e;
562 }
564 // 删除当前元素
565 public void remove() {
566 if (current == null)
567 throw new IllegalStateException();
568 if (modCount != expectedModCount)
569 throw new ConcurrentModificationException();
570 Object k = current.key;
571 current = null;
572 HashMap.this.removeEntryForKey(k);
573 expectedModCount = modCount;
574 }
576 }
578 // value的迭代器
579 private final class ValueIterator extends HashIterator<V> {
580 public V next() {
581 return nextEntry().value;
582 }
583 }
585 // key的迭代器
586 private final class KeyIterator extends HashIterator<K> {
587 public K next() {
588 return nextEntry().getKey();
589 }
590 }
592 // Entry的迭代器
593 private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
594 public Map.Entry<K,V> next() {
595 return nextEntry();
596 }
597 }
599 // 返回一个“key迭代器”
600 Iterator<K> newKeyIterator() {
601 return new KeyIterator();
602 }
603 // 返回一个“value迭代器”
604 Iterator<V> newValueIterator() {
605 return new ValueIterator();
606 }
607 // 返回一个“entry迭代器”
608 Iterator<Map.Entry<K,V>> newEntryIterator() {
609 return new EntryIterator();
610 }
612 // HashMap的Entry对应的集合
613 private transient Set<Map.Entry<K,V>> entrySet = null;
615 // 返回“key的集合”,实际上返回一个“KeySet对象”
616 public Set<K> keySet() {
617 Set<K> ks = keySet;
618 return (ks != null ? ks : (keySet = new KeySet()));
619 }
621 // Key对应的集合
622 // KeySet继承于AbstractSet,说明该集合中没有重复的Key。
623 private final class KeySet extends AbstractSet<K> {
624 public Iterator<K> iterator() {
625 return newKeyIterator();
626 }
627 public int size() {
628 return size;
629 }
630 public boolean contains(Object o) {
631 return containsKey(o);
632 }
633 public boolean remove(Object o) {
634 return HashMap.this.removeEntryForKey(o) != null;
635 }
636 public void clear() {
637 HashMap.this.clear();
638 }
639 }
641 // 返回“value集合”,实际上返回的是一个Values对象
642 public Collection<V> values() {
643 Collection<V> vs = values;
644 return (vs != null ? vs : (values = new Values()));
645 }
647 // “value集合”
648 // Values继承于AbstractCollection,不同于“KeySet继承于AbstractSet”,
649 // Values中的元素能够重复。因为不同的key可以指向相同的value。
650 private final class Values extends AbstractCollection<V> {
651 public Iterator<V> iterator() {
652 return newValueIterator();
653 }
654 public int size() {
655 return size;
656 }
657 public boolean contains(Object o) {
658 return containsValue(o);
659 }
660 public void clear() {
661 HashMap.this.clear();
662 }
663 }
665 // 返回“HashMap的Entry集合”
666 public Set<Map.Entry<K,V>> entrySet() {
667 return entrySet0();
668 }
670 // 返回“HashMap的Entry集合”,它实际是返回一个EntrySet对象
671 private Set<Map.Entry<K,V>> entrySet0() {
672 Set<Map.Entry<K,V>> es = entrySet;
673 return es != null ? es : (entrySet = new EntrySet());
674 }
676 // EntrySet对应的集合
677 // EntrySet继承于AbstractSet,说明该集合中没有重复的EntrySet。
678 private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
679 public Iterator<Map.Entry<K,V>> iterator() {
680 return newEntryIterator();
681 }
682 public boolean contains(Object o) {
683 if (!(o instanceof Map.Entry))
684 return false;
685 Map.Entry<K,V> e = (Map.Entry<K,V>) o;
686 Entry<K,V> candidate = getEntry(e.getKey());
687 return candidate != null && candidate.equals(e);
688 }
689 public boolean remove(Object o) {
690 return removeMapping(o) != null;
691 }
692 public int size() {
693 return size;
694 }
695 public void clear() {
696 HashMap.this.clear();
697 }
698 }
700 // java.io.Serializable的写入函数
701 // 将HashMap的“总的容量,实际容量,所有的Entry”都写入到输出流中
702 private void writeObject(java.io.ObjectOutputStream s)
703 throws IOException
704 {
705 Iterator<Map.Entry<K,V>> i =
706 (size > 0) ? entrySet0().iterator() : null;
708 // Write out the threshold, loadfactor, and any hidden stuff
709 s.defaultWriteObject();
711 // Write out number of buckets
712 s.writeInt(table.length);
714 // Write out size (number of Mappings)
715 s.writeInt(size);
717 // Write out keys and values (alternating)
718 if (i != null) {
719 while (i.hasNext()) {
720 Map.Entry<K,V> e = i.next();
721 s.writeObject(e.getKey());
722 s.writeObject(e.getValue());
723 }
724 }
725 }
728 private static final long serialVersionUID = 362498820763181265L;
730 // java.io.Serializable的读取函数:根据写入方式读出
731 // 将HashMap的“总的容量,实际容量,所有的Entry”依次读出
732 private void readObject(java.io.ObjectInputStream s)
733 throws IOException, ClassNotFoundException
734 {
735 // Read in the threshold, loadfactor, and any hidden stuff
736 s.defaultReadObject();
738 // Read in number of buckets and allocate the bucket array;
739 int numBuckets = s.readInt();
740 table = new Entry[numBuckets];
742 init(); // Give subclass a chance to do its thing.
744 // Read in size (number of Mappings)
745 int size = s.readInt();
747 // Read the keys and values, and put the mappings in the HashMap
748 for (int i=0; i<size; i++) {
749 K key = (K) s.readObject();
750 V value = (V) s.readObject();
751 putForCreate(key, value);
752 }
753 }
755 // 返回“HashMap总的容量”
756 int capacity() { return table.length; }
757 // 返回“HashMap的加载因子”
758 float loadFactor() { return loadFactor; }
759 }
View Code
说明:
在详细介绍 HashMap 的代码之前,我们需要了解:HashMap 就是一个散列表,它是通过 “拉链法” 解决哈希冲突的。
还需要再补充说明的一点是影响 HashMap 性能的有两个参数:初始容量 (initialCapacity) 和加载因子 (loadFactor)。容量 是哈希表中桶的数量,初始容量只是哈希表在创建时的容量。加载因子 是哈希表在其容量自动增加之前可以达到多满的一种尺度。当哈希表中的条目数超出了加载因子与当前容量的乘积时,则要对该哈希表进行 rehash 操作(即重建内部数据结构),从而哈希表将具有大约两倍的桶数。
第 3.1 部分 HashMap 的 “拉链法” 相关内容
3.1.1 HashMap 数据存储数组
transient Entry[] table;
HashMap 中的 key-value 都是存储在 Entry 数组中的。
3.1.2 数据节点 Entry 的数据结构
1 static class Entry<K,V> implements Map.Entry<K,V> {
2 final K key;
3 V value;
4 // 指向下一个节点
5 Entry<K,V> next;
6 final int hash;
8 // 构造函数。
9 // 输入参数包括"哈希值(h)", "键(k)", "值(v)", "下一节点(n)"
10 Entry(int h, K k, V v, Entry<K,V> n) {
11 value = v;
12 next = n;
13 key = k;
14 hash = h;
15 }
17 public final K getKey() {
18 return key;
19 }
21 public final V getValue() {
22 return value;
23 }
25 public final V setValue(V newValue) {
26 V oldValue = value;
27 value = newValue;
28 return oldValue;
29 }
31 // 判断两个Entry是否相等
32 // 若两个Entry的“key”和“value”都相等,则返回true。
33 // 否则,返回false
34 public final boolean equals(Object o) {
35 if (!(o instanceof Map.Entry))
36 return false;
37 Map.Entry e = (Map.Entry)o;
38 Object k1 = getKey();
39 Object k2 = e.getKey();
40 if (k1 == k2 || (k1 != null && k1.equals(k2))) {
41 Object v1 = getValue();
42 Object v2 = e.getValue();
43 if (v1 == v2 || (v1 != null && v1.equals(v2)))
44 return true;
45 }
46 return false;
47 }
49 // 实现hashCode()
50 public final int hashCode() {
51 return (key==null ? 0 : key.hashCode()) ^
52 (value==null ? 0 : value.hashCode());
53 }
55 public final String toString() {
56 return getKey() + "=" + getValue();
57 }
59 // 当向HashMap中添加元素时,绘调用recordAccess()。
60 // 这里不做任何处理
61 void recordAccess(HashMap<K,V> m) {
62 }
64 // 当从HashMap中删除元素时,绘调用recordRemoval()。
65 // 这里不做任何处理
66 void recordRemoval(HashMap<K,V> m) {
67 }
68 }
从中,我们可以看出 Entry 实际上就是一个单向链表。这也是为什么我们说 HashMap 是通过拉链法解决哈希冲突的。
Entry 实现了 Map.Entry 接口,即实现 getKey(), getValue(), setValue(V value), equals(Object o), hashCode() 这些函数。这些都是基本的读取 / 修改 key、value 值的函数。
第 3.2 部分 HashMap 的构造函数
HashMap 共包括 4 个构造函数
1 // 默认构造函数。
2 public HashMap() {
3 // 设置“加载因子”
4 this.loadFactor = DEFAULT_LOAD_FACTOR;
5 // 设置“HashMap阈值”,当HashMap中存储数据的数量达到threshold时,就需要将HashMap的容量加倍。
6 threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
7 // 创建Entry数组,用来保存数据
8 table = new Entry[DEFAULT_INITIAL_CAPACITY];
9 init();
10 }
12 // 指定“容量大小”和“加载因子”的构造函数
13 public HashMap(int initialCapacity, float loadFactor) {
14 if (initialCapacity < 0)
15 throw new IllegalArgumentException("Illegal initial capacity: " +
16 initialCapacity);
17 // HashMap的最大容量只能是MAXIMUM_CAPACITY
18 if (initialCapacity > MAXIMUM_CAPACITY)
19 initialCapacity = MAXIMUM_CAPACITY;
20 if (loadFactor <= 0 || Float.isNaN(loadFactor))
21 throw new IllegalArgumentException("Illegal load factor: " +
22 loadFactor);
24 // Find a power of 2 >= initialCapacity
25 int capacity = 1;
26 while (capacity < initialCapacity)
27 capacity <<= 1;
29 // 设置“加载因子”
30 this.loadFactor = loadFactor;
31 // 设置“HashMap阈值”,当HashMap中存储数据的数量达到threshold时,就需要将HashMap的容量加倍。
32 threshold = (int)(capacity * loadFactor);
33 // 创建Entry数组,用来保存数据
34 table = new Entry[capacity];
35 init();
36 }
38 // 指定“容量大小”的构造函数
39 public HashMap(int initialCapacity) {
40 this(initialCapacity, DEFAULT_LOAD_FACTOR);
41 }
43 // 包含“子Map”的构造函数
44 public HashMap(Map<? extends K, ? extends V> m) {
45 this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
46 DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
47 // 将m中的全部元素逐个添加到HashMap中
48 putAllForCreate(m);
49 }
第 3.3 部分 HashMap 的主要对外接口
3.3.1 clear()
clear() 的作用是清空 HashMap。它是通过将所有的元素设为 null 来实现的。
[](javascript:void(0)😉
1 public void clear() {
2 modCount++;
3 Entry[] tab = table;
4 for (int i = 0; i < tab.length; i++)
5 tab[i] = null;
6 size = 0;
7 }
[](javascript:void(0)😉
View Code
3.3.2 containsKey()
containsKey() 的作用是判断 HashMap 是否包含 key。
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
containsKey() 首先通过 getEntry(key) 获取 key 对应的 Entry,然后判断该 Entry 是否为 null。
getEntry() 的源码如下:
[](javascript:void(0)😉
1 final Entry<K,V> getEntry(Object key) {
2 // 获取哈希值
3 // HashMap将“key为null”的元素存储在table[0]位置,“key不为null”的则调用hash()计算哈希值
//
4 int hash = (key == null) ? 0 : hash(key.hashCode());
5 // 在“该hash值对应的链表”上查找“键值等于key”的元素
6 for (Entry<K,V> e = table[indexFor(hash, table.length)];
7 e != null;
8 e = e.next) {
9 Object k;
10 if (e.hash == hash &&
11 ((k = e.key) == key || (key != null && key.equals(k))))
12 return e;
13 }
14 return null;
15 }
[](javascript:void(0)😉
View Code
getEntry() 的作用就是返回 “键为 key” 的键值对,它的实现源码中已经进行了说明。
这里需要强调的是:HashMap 将 “key 为 null” 的元素都放在 table 的位置 0 处,即 table[0] 中;“key 不为 null” 的放在 table 的其余位置!
3.3.3 containsValue()
containsValue() 的作用是判断 HashMap 是否包含 “值为 value” 的元素。
1 public boolean containsValue(Object value) {
2 // 若“value为null”,则调用containsNullValue()查找
3 if (value == null)
4 return containsNullValue();
6 // 若“value不为null”,则查找HashMap中是否有值为value的节点。
7 Entry[] tab = table;
8 for (int i = 0; i < tab.length ; i++)
9 for (Entry e = tab[i] ; e != null ; e = e.next)
10 if (value.equals(e.value))
11 return true;
12 return false;
13 }
View Code
从中,我们可以看出 containsNullValue() 分为两步进行处理:第一,若 “value 为 null”,则调用 containsNullValue()。第二,若 “value 不为 null”,则查找 HashMap 中是否有值为 value 的节点。
containsNullValue() 的作用判断 HashMap 中是否包含 “值为 null” 的元素。
[](javascript:void(0)😉
1 private boolean containsNullValue() {
2 Entry[] tab = table;
3 for (int i = 0; i < tab.length ; i++)
4 for (Entry e = tab[i] ; e != null ; e = e.next)
5 if (e.value == null)
6 return true;
7 return false;
8 }
3.3.4 entrySet()、values()、keySet()
它们 3 个的原理类似,这里以 entrySet() 为例来说明。
entrySet() 的作用是返回 “HashMap 中所有 Entry 的集合”,它是一个集合。实现代码如下:
1 // 返回“HashMap的Entry集合”
2 public Set<Map.Entry<K,V>> entrySet() {
3 return entrySet0();
4 }
6 // 返回“HashMap的Entry集合”,它实际是返回一个EntrySet对象
7 private Set<Map.Entry<K,V>> entrySet0() {
8 Set<Map.Entry<K,V>> es = entrySet;
9 return es != null ? es : (entrySet = new EntrySet());
10 }
12 // EntrySet对应的集合
13 // EntrySet继承于AbstractSet,说明该集合中没有重复的EntrySet。
14 private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
15 public Iterator<Map.Entry<K,V>> iterator() {
16 return newEntryIterator();
17 }
18 public boolean contains(Object o) {
19 if (!(o instanceof Map.Entry))
20 return false;
21 Map.Entry<K,V> e = (Map.Entry<K,V>) o;
22 Entry<K,V> candidate = getEntry(e.getKey());
23 return candidate != null && candidate.equals(e);
24 }
25 public boolean remove(Object o) {
26 return removeMapping(o) != null;
27 }
28 public int size() {
29 return size;
30 }
31 public void clear() {
32 HashMap.this.clear();
33 }
34 }
HashMap 是通过拉链法实现的散列表。表现在 HashMap 包括许多的 Entry,而每一个 Entry 本质上又是一个单向链表。那么 HashMap 遍历 key-value 键值对的时候,是如何逐个去遍历的呢?
下面我们就看看 HashMap 是如何通过 entrySet() 遍历的。
entrySet() 实际上是通过 newEntryIterator() 实现的。 下面我们看看它的代码:
1 // 返回一个“entry迭代器”
2 Iterator<Map.Entry<K,V>> newEntryIterator() {
3 return new EntryIterator();
4 }
6 // Entry的迭代器
7 private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
8 public Map.Entry<K,V> next() {
9 return nextEntry();
10 }
11 }
13 // HashIterator是HashMap迭代器的抽象出来的父类,实现了公共了函数。
14 // 它包含“key迭代器(KeyIterator)”、“Value迭代器(ValueIterator)”和“Entry迭代器(EntryIterator)”3个子类。
15 private abstract class HashIterator<E> implements Iterator<E> {
16 // 下一个元素
17 Entry<K,V> next;
18 // expectedModCount用于实现fast-fail机制。
19 int expectedModCount;
20 // 当前索引
21 int index;
22 // 当前元素
23 Entry<K,V> current;
25 HashIterator() {
26 expectedModCount = modCount;
27 if (size > 0) { // advance to first entry
28 Entry[] t = table;
29 // 将next指向table中第一个不为null的元素。
30 // 这里利用了index的初始值为0,从0开始依次向后遍历,直到找到不为null的元素就退出循环。
31 while (index < t.length && (next = t[index++]) == null)
32 ;
33 }
34 }
36 public final boolean hasNext() {
37 return next != null;
38 }
40 // 获取下一个元素
41 final Entry<K,V> nextEntry() {
42 if (modCount != expectedModCount)
43 throw new ConcurrentModificationException();
44 Entry<K,V> e = next;
45 if (e == null)
46 throw new NoSuchElementException();
48 // 注意!!!
49 // 一个Entry就是一个单向链表
50 // 若该Entry的下一个节点不为空,就将next指向下一个节点;
51 // 否则,将next指向下一个链表(也是下一个Entry)的不为null的节点。
52 if ((next = e.next) == null) {
53 Entry[] t = table;
54 while (index < t.length && (next = t[index++]) == null)
55 ;
56 }
57 current = e;
58 return e;
59 }
61 // 删除当前元素
62 public void remove() {
63 if (current == null)
64 throw new IllegalStateException();
65 if (modCount != expectedModCount)
66 throw new ConcurrentModificationException();
67 Object k = current.key;
68 current = null;
69 HashMap.this.removeEntryForKey(k);
70 expectedModCount = modCount;
71 }
73 }
当我们通过 entrySet() 获取到的 Iterator 的 next() 方法去遍历 HashMap 时,实际上调用的是 nextEntry() 。而 nextEntry() 的实现方式,先遍历 Entry(根据 Entry 在 table 中的序号,从小到大的遍历);然后对每个 Entry(即每个单向链表),逐个遍历。
3.3.5 get()
get() 的作用是获取 key 对应的 value,它的实现代码如下:
1 public V get(Object key) {
2 if (key == null)
3 return getForNullKey();
4 // 获取key的hash值
5 int hash = hash(key.hashCode());
6 // 在“该hash值对应的链表”上查找“键值等于key”的元素
7 for (Entry<K,V> e = table[indexFor(hash, table.length)];
8 e != null;
9 e = e.next) {
10 Object k;
11 if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
12 return e.value;
13 }
14 return null;
15 }
从这个代码中就能看出,hashmap不需要是完全没有哈希冲突的,有冲突的话每次就在几个冲突的entry中比较就好了,如下图所示
3.3.6 put()
put() 的作用是对外提供接口,让 HashMap 对象可以通过 put()将 “key-value” 添加到 HashMap 中。
1 public V put(K key, V value) {
2 // 若“key为null”,则将该键值对添加到table[0]中。
3 if (key == null)
4 return putForNullKey(value);
5 // 若“key不为null”,则计算该key的哈希值,然后将其添加到该哈希值对应的链表中。
6 int hash = hash(key.hashCode());
7 int i = indexFor(hash, table.length);
8 for (Entry<K,V> e = table[i]; e != null; e = e.next) {
9 Object k;
10 // 若“该key”对应的键值对已经存在,则用新的value取代旧的value。然后退出!
11 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
12 V oldValue = e.value;
13 e.value = value;
14 e.recordAccess(this);
15 return oldValue;
16 }
17 }
19 // 若“该key”对应的键值对不存在,则将“key-value”添加到table中
20 modCount++;
21 addEntry(hash, key, value, i);
22 return null;
23 }
View Code
若要添加到 HashMap 中的键值对对应的 key 已经存在 HashMap 中,则找到该键值对;然后新的 value 取代旧的 value,并退出!
若要添加到 HashMap 中的键值对对应的 key 不在 HashMap 中,则将其添加到该哈希值对应的链表中,并调用 addEntry()。
下面看看 addEntry() 的代码:
1 void addEntry(int hash, K key, V value, int bucketIndex) {
2 // 保存“bucketIndex”位置的值到“e”中
3 Entry<K,V> e = table[bucketIndex];
4 // 设置“bucketIndex”位置的元素为“新Entry”,
5 // 设置“e”为“新Entry的下一个节点”
6 table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
7 // 若HashMap的实际大小 不小于 “阈值”,则调整HashMap的大小
8 if (size++ >= threshold)
9 resize(2 * table.length);
10 }
View Code
addEntry() 的作用是新增 Entry。将 “key-value” 插入指定位置,bucketIndex 是位置索引。
说到 addEntry(),就不得不说另一个函数 createEntry()。createEntry() 的代码如下:
1 void createEntry(int hash, K key, V value, int bucketIndex) {
2 // 保存“bucketIndex”位置的值到“e”中
3 Entry<K,V> e = table[bucketIndex];
4 // 设置“bucketIndex”位置的元素为“新Entry”,
5 // 设置“e”为“新Entry的下一个节点”
6 table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
7 size++;
8 }
View Code
它们的作用都是将 key、value 添加到 HashMap 中。而且,比较 addEntry() 和 createEntry() 的代码,我们发现 addEntry() 多了两句:
if (size++ >= threshold)
resize(2 * table.length);
那它们的区别到底是什么呢?
阅读代码,我们可以发现,它们的使用情景不同。
(01) addEntry() 一般用在 新增 Entry 可能导致 “HashMap 的实际容量” 超过“阈值” 的情况下。
例如,我们新建一个 HashMap,然后不断通过 put() 向 HashMap 中添加元素;put() 是通过 addEntry() 新增 Entry 的。
在这种情况下,我们不知道何时 “HashMap 的实际容量” 会超过“阈值”;
因此,需要调用 addEntry()
(02) createEntry() 一般用在 新增 Entry 不会导致 “HashMap 的实际容量” 超过“阈值” 的情况下。
例如,我们调用 HashMap“带有 Map” 的构造函数,它绘将 Map 的全部元素添加到 HashMap 中;
但在添加之前,我们已经计算好 “HashMap 的容量和阈值”。也就是,可以确定 “即使将 Map 中的全部元素添加到 HashMap 中,都不会超过 HashMap 的阈值”。
此时,调用 createEntry() 即可。
3.3.7 putAll()
putAll() 的作用是将 "m" 的全部元素都添加到 HashMap 中,它的代码如下:
1 public void putAll(Map<? extends K, ? extends V> m) {
2 // 有效性判断
3 int numKeysToBeAdded = m.size();
4 if (numKeysToBeAdded == 0)
5 return;
7 // 计算容量是否足够,
8 // 若“当前实际容量 < 需要的容量”,则将容量x2。
9 if (numKeysToBeAdded > threshold) {
10 int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
11 if (targetCapacity > MAXIMUM_CAPACITY)
12 targetCapacity = MAXIMUM_CAPACITY;
13 int newCapacity = table.length;
14 while (newCapacity < targetCapacity)
15 newCapacity <<= 1;
16 if (newCapacity > table.length)
17 resize(newCapacity);
18 }
20 // 通过迭代器,将“m”中的元素逐个添加到HashMap中。
21 for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
22 Map.Entry<? extends K, ? extends V> e = i.next();
23 put(e.getKey(), e.getValue());
24 }
25 }
View Code
3.3.8 remove()
remove() 的作用是删除 “键为 key” 元素
1 public V remove(Object key) {
2 Entry<K,V> e = removeEntryForKey(key);
3 return (e == null ? null : e.value);
4 }
7 // 删除“键为key”的元素
8 final Entry<K,V> removeEntryForKey(Object key) {
9 // 获取哈希值。若key为null,则哈希值为0;否则调用hash()进行计算
10 int hash = (key == null) ? 0 : hash(key.hashCode());
11 int i = indexFor(hash, table.length);
12 Entry<K,V> prev = table[i];
13 Entry<K,V> e = prev;
15 // 删除链表中“键为key”的元素
16 // 本质是“删除单向链表中的节点”
17 while (e != null) {
18 Entry<K,V> next = e.next;
19 Object k;
20 if (e.hash == hash &&
21 ((k = e.key) == key || (key != null && key.equals(k)))) {
22 modCount++;
23 size--;
24 if (prev == e)
25 table[i] = next;
26 else
27 prev.next = next;
28 e.recordRemoval(this);
29 return e;
30 }
31 prev = e;
32 e = next;
33 }
35 return e;
36 }
View Code
第 3.4 部分 HashMap 实现的 Cloneable 接口
HashMap 实现了 Cloneable 接口,即实现了 clone() 方法。
clone() 方法的作用很简单,就是克隆一个 HashMap 对象并返回。
1 // 克隆一个HashMap,并返回Object对象
2 public Object clone() {
3 HashMap<K,V> result = null;
4 try {
5 result = (HashMap<K,V>)super.clone();
6 } catch (CloneNotSupportedException e) {
7 // assert false;
8 }
9 result.table = new Entry[table.length];
10 result.entrySet = null;
11 result.modCount = 0;
12 result.size = 0;
13 result.init();
14 // 调用putAllForCreate()将全部元素添加到HashMap中
15 result.putAllForCreate(this);
17 return result;
18 }
View Code
第 3.5 部分 HashMap 实现的 Serializable 接口
HashMap 实现 java.io.Serializable,分别实现了串行读取、写入功能。
串行写入函数是 writeObject(),它的作用是将 HashMap 的 “总的容量,实际容量,所有的 Entry” 都写入到输出流中。
而串行读取函数是 readObject(),它的作用是将 HashMap 的 “总的容量,实际容量,所有的 Entry” 依次读出
1 // java.io.Serializable的写入函数
2 // 将HashMap的“总的容量,实际容量,所有的Entry”都写入到输出流中
3 private void writeObject(java.io.ObjectOutputStream s)
4 throws IOException
5 {
6 Iterator<Map.Entry<K,V>> i =
7 (size > 0) ? entrySet0().iterator() : null;
9 // Write out the threshold, loadfactor, and any hidden stuff
10 s.defaultWriteObject();
12 // Write out number of buckets
13 s.writeInt(table.length);
15 // Write out size (number of Mappings)
16 s.writeInt(size);
18 // Write out keys and values (alternating)
19 if (i != null) {
20 while (i.hasNext()) {
21 Map.Entry<K,V> e = i.next();
22 s.writeObject(e.getKey());
23 s.writeObject(e.getValue());
24 }
25 }
26 }
28 // java.io.Serializable的读取函数:根据写入方式读出
29 // 将HashMap的“总的容量,实际容量,所有的Entry”依次读出
30 private void readObject(java.io.ObjectInputStream s)
31 throws IOException, ClassNotFoundException
32 {
33 // Read in the threshold, loadfactor, and any hidden stuff
34 s.defaultReadObject();
36 // Read in number of buckets and allocate the bucket array;
37 int numBuckets = s.readInt();
38 table = new Entry[numBuckets];
40 init(); // Give subclass a chance to do its thing.
42 // Read in size (number of Mappings)
43 int size = s.readInt();
45 // Read the keys and values, and put the mappings in the HashMap
46 for (int i=0; i<size; i++) {
47 K key = (K) s.readObject();
48 V value = (V) s.readObject();
49 putForCreate(key, value);
50 }
51 }
第 4 部分 HashMap 遍历方式
4.1 遍历 HashMap 的键值对
第一步:根据 entrySet()获取 HashMap 的 “键值对” 的 Set 集合。
第二步:通过 Iterator 迭代器遍历 “第一步” 得到的集合。
// 假设map是HashMap对象
// map中的key是String类型,value是Integer类型
Integer integ = null;
Iterator iter = map.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
// 获取key
key = (String)entry.getKey();
// 获取value
integ = (Integer)entry.getValue();
}
4.2 遍历 HashMap 的键
第一步:根据 keySet()获取 HashMap 的 “键” 的 Set 集合。
第二步:通过 Iterator 迭代器遍历 “第一步” 得到的集合。
// 假设map是HashMap对象
// map中的key是String类型,value是Integer类型
String key = null;
Integer integ = null;
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
// 获取key
key = (String)iter.next();
// 根据key,获取value
integ = (Integer)map.get(key);
}
4.3 遍历 HashMap 的值
第一步:根据 value()获取 HashMap 的 “值” 的集合。
第二步:通过 Iterator 迭代器遍历 “第一步” 得到的集合。
// 假设map是HashMap对象
// map中的key是String类型,value是Integer类型
Integer value = null;
Collection c = map.values();
Iterator iter= c.iterator();
while (iter.hasNext()) {
value = (Integer)iter.next();
}
遍历测试程序如下:
1 import java.util.Map;
2 import java.util.Random;
3 import java.util.Iterator;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Map.Entry;
7 import java.util.Collection;
9 /*
10 * @desc 遍历HashMap的测试程序。
11 * (01) 通过entrySet()去遍历key、value,参考实现函数:
12 * iteratorHashMapByEntryset()
13 * (02) 通过keySet()去遍历key、value,参考实现函数:
14 * iteratorHashMapByKeyset()
15 * (03) 通过values()去遍历value,参考实现函数:
16 * iteratorHashMapJustValues()
17 *
18 * @author skywang
19 */
20 public class HashMapIteratorTest {
22 public static void main(String[] args) {
23 int val = 0;
24 String key = null;
25 Integer value = null;
26 Random r = new Random();
27 HashMap map = new HashMap();
29 for (int i=0; i<12; i++) {
30 // 随机获取一个[0,100)之间的数字
31 val = r.nextInt(100);
33 key = String.valueOf(val);
34 value = r.nextInt(5);
35 // 添加到HashMap中
36 map.put(key, value);
37 System.out.println(" key:"+key+" value:"+value);
38 }
39 // 通过entrySet()遍历HashMap的key-value
40 iteratorHashMapByEntryset(map) ;
42 // 通过keySet()遍历HashMap的key-value
43 iteratorHashMapByKeyset(map) ;
45 // 单单遍历HashMap的value
46 iteratorHashMapJustValues(map);
47 }
49 /*
50 * 通过entry set遍历HashMap
51 * 效率高!
52 */
53 private static void iteratorHashMapByEntryset(HashMap map) {
54 if (map == null)
55 return ;
57 System.out.println("\niterator HashMap By entryset");
58 String key = null;
59 Integer integ = null;
60 Iterator iter = map.entrySet().iterator();
61 while(iter.hasNext()) {
62 Map.Entry entry = (Map.Entry)iter.next();
64 key = (String)entry.getKey();
65 integ = (Integer)entry.getValue();
66 System.out.println(key+" -- "+integ.intValue());
67 }
68 }
70 /*
71 * 通过keyset来遍历HashMap
72 * 效率低!
73 */
74 private static void iteratorHashMapByKeyset(HashMap map) {
75 if (map == null)
76 return ;
78 System.out.println("\niterator HashMap By keyset");
79 String key = null;
80 Integer integ = null;
81 Iterator iter = map.keySet().iterator();
82 while (iter.hasNext()) {
83 key = (String)iter.next();
84 integ = (Integer)map.get(key);
85 System.out.println(key+" -- "+integ.intValue());
86 }
87 }
90 /*
91 * 遍历HashMap的values
92 */
93 private static void iteratorHashMapJustValues(HashMap map) {
94 if (map == null)
95 return ;
97 Collection c = map.values();
98 Iterator iter= c.iterator();
99 while (iter.hasNext()) {
100 System.out.println(iter.next());
101 }
102 }
103 }
第 5 部分 HashMap 示例
下面通过一个实例学习如何使用 HashMap
1 import java.util.Map;
2 import java.util.Random;
3 import java.util.Iterator;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Map.Entry;
7 import java.util.Collection;
9 /*
10 * @desc HashMap测试程序
11 *
12 * @author skywang
13 */
14 public class HashMapTest {
16 public static void main(String[] args) {
17 testHashMapAPIs();
18 }
20 private static void testHashMapAPIs() {
21 // 初始化随机种子
22 Random r = new Random();
23 // 新建HashMap
24 HashMap map = new HashMap();
25 // 添加操作
26 map.put("one", r.nextInt(10));
27 map.put("two", r.nextInt(10));
28 map.put("three", r.nextInt(10));
30 // 打印出map
31 System.out.println("map:"+map );
33 // 通过Iterator遍历key-value
34 Iterator iter = map.entrySet().iterator();
35 while(iter.hasNext()) {
36 Map.Entry entry = (Map.Entry)iter.next();
37 System.out.println("next : "+ entry.getKey() +" - "+entry.getValue());
38 }
40 // HashMap的键值对个数
41 System.out.println("size:"+map.size());
43 // containsKey(Object key) :是否包含键key
44 System.out.println("contains key two : "+map.containsKey("two"));
45 System.out.println("contains key five : "+map.containsKey("five"));
47 // containsValue(Object value) :是否包含值value
48 System.out.println("contains value 0 : "+map.containsValue(new Integer(0)));
50 // remove(Object key) : 删除键key对应的键值对
51 map.remove("three");
53 System.out.println("map:"+map );
55 // clear() : 清空HashMap
56 map.clear();
58 // isEmpty() : HashMap是否为空
59 System.out.println((map.isEmpty()?"map is empty":"map is not empty") );
60 }
61 }
map:{two=7, one=9, three=6}
next : two - 7
next : one - 9
next : three - 6
size:3
contains key two : true
contains key five : false
contains value 0 : false
map:{two=7, one=9}
map is empty
HashMap 的整体结构如下
简单来说,HashMap 由数组 + 链表组成的,数组是 HashMap 的主体,链表则是主要为了解决哈希冲突而存在的,如果定位到的数组位置不含链表(当前 entry 的 next 指向 null), 那么对于查找,添加等操作很快,仅需一次寻址即可;如果定位到的数组包含链表,对于添加操作,其时间复杂度为 O(n),首先遍历链表,存在即覆盖,否则新增;对于查找操作来讲,仍需遍历链表,然后通过 key 对象的 equals 方法逐一比对查找。所以,性能考虑,HashMap 中的链表出现越少,性能才会越好。
其他几个重要字段
[](javascript:void(0)😉
//实际存储的key-value键值对的个数
transient int size;
//阈值,当table == {}时,该值为初始容量(初始容量默认为16);当table被填充了,也就是为table分配内存空间后,threshold一般为 capacity*loadFactory。HashMap在进行扩容时需要参考threshold,后面会详细谈到
int threshold;
//负载因子,代表了table的填充度有多少,默认是0.75
final float loadFactor;
//用于快速失败,由于HashMap非线程安全,在对HashMap进行迭代时,如果期间其他线程的参与导致HashMap的结构发生变化了(比如put,remove等操作),需要抛出异常ConcurrentModificationException
transient int modCount;
[](javascript:void(0)😉
HashMap 有 4 个构造器,其他构造器如果用户没有传入 initialCapacity 和 loadFactor 这两个参数,会使用默认值
initialCapacity 默认为 16,loadFactory 默认为 0.75
我们看下其中一个
[](javascript:void(0)😉
public HashMap(int initialCapacity, float loadFactor) {
//此处对传入的初始容量进行校验,最大不能超过MAXIMUM_CAPACITY = 1<<30(230)
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;
threshold = initialCapacity;
init();//init方法在HashMap中没有实际实现,不过在其子类如 linkedHashMap中就会有对应实现
}
[](javascript:void(0)😉
从上面这段代码我们可以看出,在常规构造器中,没有为数组 table 分配内存空间(有一个入参为指定 Map 的构造器例外),而是在执行 put 操作的时候才真正构建 table 数组
OK, 接下来我们来看看 put 操作的实现吧
[](javascript:void(0)😉
public V put(K key, V value) {
//如果table数组为空数组{},进行数组填充(为table分配实际内存空间),入参为threshold,此时threshold为initialCapacity 默认是1<<4(24=16)
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
//如果key为null,存储位置为table[0]或table[0]的冲突链上
if (key == null)
return putForNullKey(value);
int hash = hash(key);//对key的hashcode进一步计算,确保散列均匀
int i = indexFor(hash, table.length);//获取在table中的实际位置
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
//如果该对应数据已存在,执行覆盖操作。用新value替换旧value,并返回旧value
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;
}
}
modCount++;//保证并发访问时,若HashMap内部结构发生变化,快速响应失败
addEntry(hash, key, value, i);//新增一个entry
return null;
}
[](javascript:void(0)😉
先来看看 inflateTable 这个方法
[](javascript:void(0)😉
private void inflateTable(int toSize) {
int capacity = roundUpToPowerOf2(toSize);//capacity一定是2的次幂
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);//此处为threshold赋值,取capacity*loadFactor和MAXIMUM_CAPACITY+1的最小值,capaticy一定不会超过MAXIMUM_CAPACITY,除非loadFactor大于1
table = new Entry[capacity];
initHashSeedAsNeeded(capacity);
}
[](javascript:void(0)😉
inflateTable 这个方法用于为主干数组 table 在内存中分配存储空间,通过 roundUpToPowerOf2(toSize) 可以确保 capacity 为大于或等于 toSize 的最接近 toSize 的二次幂,比如 toSize=13, 则 capacity=16;to_size=16,capacity=16;to_size=17,capacity=32.
[](javascript:void(0)😉
private static int roundUpToPowerOf2(int number) {
// assert number >= 0 : "number must be non-negative";
return number >= MAXIMUM_CAPACITY
? MAXIMUM_CAPACITY
: (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;
}
[](javascript:void(0)😉
roundUpToPowerOf2 中的这段处理使得数组长度一定为 2 的次幂,Integer.highestOneBit 是用来获取最左边的 bit(其他 bit 位为 0)所代表的数值.
hash 函数
[](javascript:void(0)😉
//这是一个神奇的函数,用了很多的异或,移位等运算,对key的hashcode进一步进行计算以及二进制位的调整等来保证最终获取的存储位置尽量分布均匀
final int hash(Object k) {
int h = hashSeed;
if (0 != h && k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode();
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
[](javascript:void(0)😉
以上 hash 函数计算出的值,通过 indexFor 进一步处理来获取实际的存储位置
[](javascript:void(0)😉
/**
* 返回数组下标
*/
static int indexFor(int h, int length) {
return h & (length-1);
}
[](javascript:void(0)😉
h&(length-1)保证获取的 index 一定在数组范围内,举个例子,默认容量 16,length-1=15,h=18, 转换成二进制计算为
1 0 0 1 0
& 0 1 1 1 1
__________________
0 0 0 1 0 = 2
最终计算出的 index=2。有些版本的对于此处的计算会使用 取模运算,也能保证 index 一定在数组范围内,不过位运算对计算机来说,性能更高一些(HashMap 中有大量位运算)
所以最终存储位置的确定流程是这样的:
再来看看 addEntry 的实现:
[](javascript:void(0)😉
void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);//当size超过临界阈值threshold,并且即将发生哈希冲突时进行扩容
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
[](javascript:void(0)😉
通过以上代码能够得知,当发生哈希冲突并且 size 大于阈值的时候,需要进行数组扩容,扩容时,需要新建一个长度为之前数组 2 倍的新的数组,然后将当前的 Entry 数组中的元素全部传输过去,扩容后的新数组长度为之前的 2 倍,所以扩容相对来说是个耗资源的操作。
三、为何 HashMap 的数组长度一定是 2 的次幂?
我们来继续看上面提到的 resize 方法
[](javascript:void(0)😉
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
Entry[] newTable = new Entry[newCapacity];
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
[](javascript:void(0)😉
如果数组进行扩容,数组长度发生变化,而存储位置 index = h&(length-1),index 也可能会发生变化,需要重新计算 index,我们先来看看 transfer 这个方法
[](javascript:void(0)😉
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
//for循环中的代码,逐个遍历链表,重新计算索引位置,将老数组数据复制到新数组中去(数组不存储实际数据,所以仅仅是拷贝引用而已)
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
//将当前entry的next链指向新的索引位置,newTable[i]有可能为空,有可能也是个entry链,如果是entry链,直接在链表头部插入。
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
[](javascript:void(0)😉
这个方法将老数组中的数据逐个链表地遍历,扔到新的扩容后的数组中,我们的数组索引位置的计算是通过 对 key 值的 hashcode 进行 hash 扰乱运算后,再通过和 length-1 进行位运算得到最终数组索引位置。
hashMap 的数组长度一定保持 2 的次幂,比如 16 的二进制表示为 10000,那么 length-1 就是 15,二进制为 01111,同理扩容后的数组长度为 32,二进制表示为 100000,length-1 为 31,二进制表示为 011111。从下图可以我们也能看到这样会保证低位全为 1,而扩容后只有一位差异,也就是多出了最左位的 1,这样在通过 h&(length-1) 的时候,只要 h 对应的最左边的那一个差异位为 0,就能保证得到的新的数组索引和老数组索引一致 (大大减少了之前已经散列良好的老数组的数据位置重新调换),个人理解。
还有,数组长度保持 2 的次幂,length-1 的低位都为 1,会使得获得的数组索引 index 更加均匀,比如:
我们看到,上面的 & 运算,高位是不会对结果产生影响的(hash 函数采用各种位运算可能也是为了使得低位更加散列),我们只关注低位 bit,如果length-1的低位全部为 1,那么对于 h 低位部分来说,任何一位的变化都会对结果产生影响,也就是说,要得到 index=21 这个存储位置,h 的低位只有这一种组合,从而减少了哈希碰撞的可能性。这也是数组长度设计为必须为 2 的次幂的原因。
如果不是 2 的次幂,也就是低位不是全为 1 此时,要使得 index=21,h 的低位部分不再具有唯一性了,哈希冲突的几率会变的更大,同时,index 对应的这个 bit 位无论如何不会等于 1 了,而对应的那些数组位置也就被白白浪费了。
get 方法
[](javascript:void(0)😉
public V get(Object key) {
//如果key为null,则直接去table[0]处去检索即可。
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
[](javascript:void(0)😉
get 方法通过 key 值返回对应 value,如果 key 为 null,直接去 table[0] 处检索。我们再看一下 getEntry 这个方法
[](javascript:void(0)😉
final Entry<K,V> getEntry(Object key) {
if (size == 0) {
return null;
}
//通过key的hashcode值计算hash值
int hash = (key == null) ? 0 : hash(key);
//indexFor (hash&length-1) 获取最终数组索引,然后遍历链表,通过equals方法比对找出对应记录
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;
}
[](javascript:void(0)😉
可以看出,get 方法的实现相对简单,key(hashcode)-->hash-->indexFor--> 最终索引位置,找到对应位置 table[i],再查看是否有链表,遍历链表,通过 key 的 equals 方法比对查找对应的记录。要注意的是,有人觉得上面在定位到数组位置之后然后遍历链表的时候,e.hash == hash 这个判断没必要,仅通过 equals 判断就可以。其实不然,试想一下,如果传入的 key 对象重写了 equals 方法却没有重写 hashCode,而恰巧此对象定位到这个数组位置,如果仅仅用 equals 判断可能是相等的,但其 hashCode 和当前对象不一致,这种情况,根据 Object 的 hashCode 的约定,不能返回当前对象,而应该返回 null,后面的例子会做出进一步解释。
四、重写 equals 方法需同时重写 hashCode 方法
关于 HashMap 的源码分析就介绍到这儿了,最后我们再聊聊老生常谈的一个问题,各种资料上都会提到,“重写 equals 时也要同时覆盖 hashcode”,我们举个小例子来看看,如果重写了 equals 而不重写 hashcode 会发生什么样的问题
[](javascript:void(0)😉
/**
* Created by chengxiao on 2016/11/15.
*/
public class MyTest {
private static class Person{
int idCard;
String name;
public Person(int idCard, String name) {
this.idCard = idCard;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()){
return false;
}
Person person = (Person) o;
//两个对象是否等值,通过idCard来确定
return this.idCard == person.idCard;
}
}
public static void main(String []args){
HashMap<Person,String> map = new HashMap<Person, String>();
Person person = new Person(1234,"乔峰");
//put到hashmap中去
map.put(person,"天龙八部");
//get取出,从逻辑上讲应该能输出“天龙八部”
System.out.println("结果:"+map.get(new Person(1234,"萧峰")));
}
}
[](javascript:void(0)😉
实际输出结果:
结果:null
如果我们已经对 HashMap 的原理有了一定了解,这个结果就不难理解了。尽管我们在进行 get 和 put 操作的时候,使用的 key 从逻辑上讲是等值的(通过 equals 比较是相等的),但由于没有重写 hashCode 方法,所以 put 操作时,key(hashcode1)-->hash-->indexFor--> 最终索引位置 ,而通过 key 取出 value 的时候 key(hashcode1)-->hash-->indexFor--> 最终索引位置,由于 hashcode1 不等于 hashcode2,导致没有定位到一个数组位置而返回逻辑上错误的值 null(也有可能碰巧定位到一个数组位置,但是也会判断其 entry 的 hash 值是否相等,上面 get 方法中有提到。)
所以,在重写 equals 的方法的时候,必须注意重写 hashCode 方法,同时还要保证通过 equals 判断相等的两个对象,调用 hashCode 方法要返回同样的整数值。而如果 equals 判断不相等的两个对象,其 hashCode 可以相同(只不过会发生哈希冲突,应尽量避免)。