Skip to content

Commit 0dea62d

Browse files
committed
[A] map: HashMap && ConcurrentHashMap && LinkedHashMap
1 parent eb98288 commit 0dea62d

File tree

5 files changed

+97
-4
lines changed

5 files changed

+97
-4
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
## 目录简介
1515

16-
<h1>Directory Tree</h1><p>
17-
<a href=".">.</a><br>
16+
<a href=".">.</a><br>
1817
├── <a href="./README.md">README.md</a><br>
1918
├── <a href="./algo/">algo</a><br>
2019
│ ├── <a href="./algo/leetcode-148-Sort-List(%E5%8D%95%E9%93%BE%E8%A1%A8%E6%8E%92%E5%BA%8F).md">leetcode-148-Sort-List(单链表排序).md</a><br>
@@ -55,10 +54,16 @@
5554
│ │ &nbsp;&nbsp;&nbsp; │ ├── <a href="./java/source-code/jdk/collection/list-linkedlist.md">list-linkedlist.md</a><br>
5655
│ │ &nbsp;&nbsp;&nbsp; │ ├── <a href="./java/source-code/jdk/collection/list-stack.md">list-stack.md</a><br>
5756
│ │ &nbsp;&nbsp;&nbsp; │ ├── <a href="./java/source-code/jdk/collection/list-vector.md">list-vector.md</a><br>
57+
│ │ &nbsp;&nbsp;&nbsp; │ ├── <a href="./java/source-code/jdk/collection/map-concurrenthashmap.md">map-concurrenthashmap.md</a><br>
5858
│ │ &nbsp;&nbsp;&nbsp; │ ├── <a href="./java/source-code/jdk/collection/map-enummap.md">map-enummap.md</a><br>
59-
│ │ &nbsp;&nbsp;&nbsp; │ └── <a href="./java/source-code/jdk/collection/map-identityhashmap.md">map-identityhashmap.md</a><br>
59+
│ │ &nbsp;&nbsp;&nbsp; │ ├── <a href="./java/source-code/jdk/collection/map-hashmap.md">map-hashmap.md</a><br>
60+
│ │ &nbsp;&nbsp;&nbsp; │ ├── <a href="./java/source-code/jdk/collection/map-identityhashmap.md">map-identityhashmap.md</a><br>
61+
│ │ &nbsp;&nbsp;&nbsp; │ ├── <a href="./java/source-code/jdk/collection/map-linkedhashmap.md">map-linkedhashmap.md</a><br>
62+
│ │ &nbsp;&nbsp;&nbsp; │ └── <a href="./java/source-code/jdk/collection/map-weakhashmap.md">map-weakhashmap.md</a><br>
6063
│ │ &nbsp;&nbsp;&nbsp; └── <a href="./java/source-code/jdk/lang/">lang</a><br>
61-
│ │ &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; └── <a href="./java/source-code/jdk/lang/integer.md">integer.md</a><br>
64+
│ │ &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ├── <a href="./java/source-code/jdk/lang/integer.md">integer.md</a><br>
65+
│ │ &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; └── <a href="./java/source-code/jdk/lang/ref/">ref</a><br>
66+
│ │ &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; └── <a href="./java/source-code/jdk/lang/ref/reference.md">reference.md</a><br>
6267
│ ├── <a href="./java/test/">test</a><br>
6368
│ │ └── <a href="./java/test/awaitility.zh.md">awaitility.zh.md</a><br>
6469
│ ├── <a href="./java/%E5%9B%BE%E8%A7%A3JavaCollectionFramework.md">图解JavaCollectionFramework.md</a><br>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `ConcurrentHashMap<K,V>`
2+
3+
看参考一篇吧,足够了。
4+
5+
## 参考
6+
7+
- [为并发而生的 ConcurrentHashMap(Java 8)](https://cloud.tencent.com/developer/article/1013643)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# `HashMap<K,V>`
2+
3+
## 设计
4+
5+
链地址法,链上条目超过8个转红黑树。红黑树小于 6 退化成链。
6+
7+
## 扩容策略
8+
9+
```java
10+
/**
11+
* The default initial capacity - MUST be a power of two.
12+
*/
13+
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
14+
15+
16+
/**
17+
* The bin count threshold for using a tree rather than list for a
18+
* bin. Bins are converted to trees when adding an element to a
19+
* bin with at least this many nodes. The value must be greater
20+
* than 2 and should be at least 8 to mesh with assumptions in
21+
* tree removal about conversion back to plain bins upon
22+
* shrinkage.
23+
*/
24+
static final int TREEIFY_THRESHOLD = 8;
25+
26+
/**
27+
* The bin count threshold for untreeifying a (split) bin during a
28+
* resize operation. Should be less than TREEIFY_THRESHOLD, and at
29+
* most 6 to mesh with shrinkage detection under removal.
30+
*/
31+
static final int UNTREEIFY_THRESHOLD = 6;
32+
33+
/**
34+
* The smallest table capacity for which bins may be treeified.
35+
* (Otherwise the table is resized if too many nodes in a bin.)
36+
* Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
37+
* between resizing and treeification thresholds.
38+
*/
39+
static final int MIN_TREEIFY_CAPACITY = 64;
40+
```
41+
42+
达到阈值直接扩容为原来两倍
43+
44+
```java
45+
/**
46+
* Initializes or doubles table size. If null, allocates in
47+
* accord with initial capacity target held in field threshold.
48+
* Otherwise, because we are using power-of-two expansion, the
49+
* elements from each bin must either stay at same index, or move
50+
* with a power of two offset in the new table.
51+
*
52+
* @return the table
53+
*/
54+
final Node<K,V>[] resize() {}
55+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `LinkedHashMap<K,V>`
2+
3+
继承自 HashMap
4+
5+
## 节点继承结构
6+
7+
![collection-hashmap-node-class](http://image.rainstorm.vip/blog/collection-hashmap-node-class.png)

res/collection-hashmap-node-class.pu

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@startuml collection-hashmap-node-class
2+
3+
4+
interface Map.Entry<K,V>
5+
class HashMap.Node<K,V>
6+
class HashMap.TreeNode<K,V>
7+
class LinkedListHashMap.Entry<K,V>
8+
9+
10+
Map.Entry <.. HashMap.Node
11+
HashMap.Node <-- LinkedListHashMap.Entry
12+
LinkedListHashMap.Entry <-- HashMap.TreeNode
13+
14+
15+
caption hashmap节点继承结构类图
16+
17+
right footer https://github.com/c-rainstorm/blog 转载请注明出处
18+
19+
@enduml

0 commit comments

Comments
 (0)