diff --git a/docs/notes/Java 容器.md b/docs/notes/Java 容器.md index 61fe2c1f..3417c8a1 100644 --- a/docs/notes/Java 容器.md +++ b/docs/notes/Java 容器.md @@ -659,9 +659,9 @@ static int indexFor(int h, int length) { ### 5. 扩容-基本原理 -设 HashMap 的 table 长度为 M,需要存储的键值对数量为 N,如果哈希函数满足均匀性的要求,那么每条链表的长度大约为 N/M,因此平均查找次数的复杂度为 O(N/M)。 +设 HashMap 的 table 长度为 M,需要存储的键值对数量为 N,如果哈希函数满足均匀性的要求,那么每条链表的长度大约为 N/M,因此查找的复杂度为 O(N/M)。 -为了让查找的成本降低,应该尽可能使得 N/M 尽可能小,因此需要保证 M 尽可能大,也就是说 table 要尽可能大。HashMap 采用动态扩容来根据当前的 N 值来调整 M 值,使得空间效率和时间效率都能得到保证。 +为了让查找的成本降低,应该使 N/M 尽可能小,因此需要保证 M 尽可能大,也就是说 table 要尽可能大。HashMap 采用动态扩容来根据当前的 N 值来调整 M 值,使得空间效率和时间效率都能得到保证。 和扩容相关的参数主要有:capacity、size、threshold 和 load_factor。 @@ -670,7 +670,7 @@ static int indexFor(int h, int length) { | capacity | table 的容量大小,默认为 16。需要注意的是 capacity 必须保证为 2 的 n 次方。| | size | 键值对数量。 | | threshold | size 的临界值,当 size 大于等于 threshold 就必须进行扩容操作。 | -| loadFactor | 装载因子,table 能够使用的比例,threshold = (int)(newCapacity * loadFactor)。| +| loadFactor | 装载因子,table 能够使用的比例,threshold = (int)(capacity* loadFactor)。 | ```java static final int DEFAULT_INITIAL_CAPACITY = 16; @@ -800,6 +800,8 @@ static final int tableSizeFor(int cap) { ### 1. 存储结构 +

+ ```java static final class HashEntry { final int hash; @@ -843,8 +845,6 @@ final Segment[] segments; static final int DEFAULT_CONCURRENCY_LEVEL = 16; ``` -

- ### 2. size 操作 每个 Segment 维护了一个 count 变量来统计该 Segment 中的键值对个数。 diff --git a/notes/Java 容器.md b/notes/Java 容器.md index 61fe2c1f..3417c8a1 100644 --- a/notes/Java 容器.md +++ b/notes/Java 容器.md @@ -659,9 +659,9 @@ static int indexFor(int h, int length) { ### 5. 扩容-基本原理 -设 HashMap 的 table 长度为 M,需要存储的键值对数量为 N,如果哈希函数满足均匀性的要求,那么每条链表的长度大约为 N/M,因此平均查找次数的复杂度为 O(N/M)。 +设 HashMap 的 table 长度为 M,需要存储的键值对数量为 N,如果哈希函数满足均匀性的要求,那么每条链表的长度大约为 N/M,因此查找的复杂度为 O(N/M)。 -为了让查找的成本降低,应该尽可能使得 N/M 尽可能小,因此需要保证 M 尽可能大,也就是说 table 要尽可能大。HashMap 采用动态扩容来根据当前的 N 值来调整 M 值,使得空间效率和时间效率都能得到保证。 +为了让查找的成本降低,应该使 N/M 尽可能小,因此需要保证 M 尽可能大,也就是说 table 要尽可能大。HashMap 采用动态扩容来根据当前的 N 值来调整 M 值,使得空间效率和时间效率都能得到保证。 和扩容相关的参数主要有:capacity、size、threshold 和 load_factor。 @@ -670,7 +670,7 @@ static int indexFor(int h, int length) { | capacity | table 的容量大小,默认为 16。需要注意的是 capacity 必须保证为 2 的 n 次方。| | size | 键值对数量。 | | threshold | size 的临界值,当 size 大于等于 threshold 就必须进行扩容操作。 | -| loadFactor | 装载因子,table 能够使用的比例,threshold = (int)(newCapacity * loadFactor)。| +| loadFactor | 装载因子,table 能够使用的比例,threshold = (int)(capacity* loadFactor)。 | ```java static final int DEFAULT_INITIAL_CAPACITY = 16; @@ -800,6 +800,8 @@ static final int tableSizeFor(int cap) { ### 1. 存储结构 +

+ ```java static final class HashEntry { final int hash; @@ -843,8 +845,6 @@ final Segment[] segments; static final int DEFAULT_CONCURRENCY_LEVEL = 16; ``` -

- ### 2. size 操作 每个 Segment 维护了一个 count 变量来统计该 Segment 中的键值对个数。 diff --git a/notes/pics/image-20191209001024618.png b/notes/pics/image-20191209001024618.png new file mode 100644 index 00000000..9e470149 Binary files /dev/null and b/notes/pics/image-20191209001024618.png differ diff --git a/notes/pics/image-20191209001038024.png b/notes/pics/image-20191209001038024.png new file mode 100644 index 00000000..9e470149 Binary files /dev/null and b/notes/pics/image-20191209001038024.png differ