|
| 1 | +**更多 HashMap 与 ConcurrentHashMap 相关请查看[这里](https://crossoverjie.top/2018/07/23/java-senior/ConcurrentHashMap/)。** |
| 2 | + |
| 3 | +# HashMap 底层分析 |
| 4 | + |
| 5 | +> 以下基于 JDK1.7 分析。 |
| 6 | +
|
| 7 | + |
| 8 | + |
| 9 | +如图所示,HashMap 底层是基于数组和链表实现的。其中有两个重要的参数: |
| 10 | + |
| 11 | +- 容量 |
| 12 | +- 负载因子 |
| 13 | + |
| 14 | +容量的默认大小是 16,负载因子是 0.75,当 `HashMap` 的 `size > 16*0.75` 时就会发生扩容(容量和负载因子都可以自由调整)。 |
| 15 | + |
| 16 | +## put 方法 |
| 17 | +首先会将传入的 Key 做 `hash` 运算计算出 hashcode,然后根据数组长度取模计算出在数组中的 index 下标。 |
| 18 | + |
| 19 | +由于在计算中位运算比取模运算效率高的多,所以 HashMap 规定数组的长度为 `2^n` 。这样用 `2^n - 1` 做位运算与取模效果一致,并且效率还要高出许多。 |
| 20 | + |
| 21 | +由于数组的长度有限,所以难免会出现不同的 Key 通过运算得到的 index 相同,这种情况可以利用链表来解决,HashMap 会在 `table[index]`处形成链表,采用头插法将数据插入到链表中。 |
| 22 | + |
| 23 | +## get 方法 |
| 24 | + |
| 25 | +get 和 put 类似,也是将传入的 Key 计算出 index ,如果该位置上是一个链表就需要遍历整个链表,通过 `key.equals(k)` 来找到对应的元素。 |
| 26 | + |
| 27 | +## 遍历方式 |
| 28 | + |
| 29 | + |
| 30 | +```java |
| 31 | + Iterator<Map.Entry<String, Integer>> entryIterator = map.entrySet().iterator(); |
| 32 | + while (entryIterator.hasNext()) { |
| 33 | + Map.Entry<String, Integer> next = entryIterator.next(); |
| 34 | + System.out.println("key=" + next.getKey() + " value=" + next.getValue()); |
| 35 | + } |
| 36 | +``` |
| 37 | + |
| 38 | +```java |
| 39 | +Iterator<String> iterator = map.keySet().iterator(); |
| 40 | + while (iterator.hasNext()){ |
| 41 | + String key = iterator.next(); |
| 42 | + System.out.println("key=" + key + " value=" + map.get(key)); |
| 43 | + |
| 44 | + } |
| 45 | +``` |
| 46 | + |
| 47 | +```java |
| 48 | +map.forEach((key,value)->{ |
| 49 | + System.out.println("key=" + key + " value=" + value); |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +**强烈建议**使用第一种 EntrySet 进行遍历。 |
| 54 | + |
| 55 | +第一种可以把 key value 同时取出,第二种还得需要通过 key 取一次 value,效率较低, 第三种需要 `JDK1.8` 以上,通过外层遍历 table,内层遍历链表或红黑树。 |
| 56 | + |
| 57 | + |
| 58 | +## notice |
| 59 | + |
| 60 | +在并发环境下使用 `HashMap` 容易出现死循环。 |
| 61 | + |
| 62 | +并发场景发生扩容,调用 `resize()` 方法里的 `rehash()` 时,容易出现环形链表。这样当获取一个不存在的 `key` 时,计算出的 `index` 正好是环形链表的下标时就会出现死循环。 |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +> 所以 HashMap 只能在单线程中使用,并且尽量的预设容量,尽可能的减少扩容。 |
| 67 | +
|
| 68 | +在 `JDK1.8` 中对 `HashMap` 进行了优化: |
| 69 | +当 `hash` 碰撞之后写入链表的长度超过了阈值(默认为8),链表将会转换为**红黑树**。 |
| 70 | + |
| 71 | +假设 `hash` 冲突非常严重,一个数组后面接了很长的链表,此时重新的时间复杂度就是 `O(n)` 。 |
| 72 | + |
| 73 | +如果是红黑树,时间复杂度就是 `O(logn)` 。 |
| 74 | + |
| 75 | +大大提高了查询效率。 |
| 76 | + |
| 77 | +多线程场景下推荐使用 [ConcurrentHashMap](https://github.com/crossoverJie/Java-Interview/blob/master/MD/ConcurrentHashMap.md)。 |
0 commit comments