Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
jdk
提交
ee1852ec
J
jdk
项目概览
檀越@新空间
/
jdk
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
ee1852ec
编写于
4月 03, 2023
作者:
檀越@新空间
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:resize源码解读
上级
b3c1eef2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
101 addition
and
77 deletion
+101
-77
util/HashMap.java
util/HashMap.java
+101
-77
未找到文件。
util/HashMap.java
浏览文件 @
ee1852ec
...
@@ -229,36 +229,35 @@ public class HashMap<K, V> extends AbstractMap<K, V>
...
@@ -229,36 +229,35 @@ public class HashMap<K, V> extends AbstractMap<K, V>
*/
*/
/**
/**
*
The default initial capacity - MUST be a power of two.
*
默认的初始容量-必须是二的幂。
*/
*/
static
final
int
DEFAULT_INITIAL_CAPACITY
=
1
<<
4
;
// aka 16
static
final
int
DEFAULT_INITIAL_CAPACITY
=
1
<<
4
;
/**
/**
* The maximum capacity, used if a higher value is implicitly specified
* HashMap的容量极限,为2的30次幂;
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
*/
static
final
int
MAXIMUM_CAPACITY
=
1
<<
30
;
static
final
int
MAXIMUM_CAPACITY
=
1
<<
30
;
/**
/**
* The load factor used when none specified in constructor.
* 负载因子,来决定容量的大小以及是否扩容
* <p>
* 负载因子在初始化Map的时候被赋值,要么被用户传的参数赋值,要么被默认的0.75f给赋值;
* <p>
* 默认的负载因子我们之前说过:0.75f,当然也可以自己设置,但 0.75 是最均衡的设置,没有特殊要求不要修改该值,
* <p>
* 负载因子过小,说明Node元素比较少,理论上能减少 hash 冲突,
* <p>
* 负载因子过大,可以节约空间。
*/
*/
static
final
float
DEFAULT_LOAD_FACTOR
=
0.75f
;
static
final
float
DEFAULT_LOAD_FACTOR
=
0.75f
;
/**
/**
* The bin count threshold for using a tree rather than list for a
* 链表变为红黑树时链表的长度阈值
* bin. Bins are converted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
*/
*/
static
final
int
TREEIFY_THRESHOLD
=
8
;
static
final
int
TREEIFY_THRESHOLD
=
8
;
/**
/**
* The bin count threshold for untreeifying a (split) bin during a
* 取消树化
* resize operation. Should be less than TREEIFY_THRESHOLD, and at
* most 6 to mesh with shrinkage detection under removal.
*/
*/
static
final
int
UNTREEIFY_THRESHOLD
=
6
;
static
final
int
UNTREEIFY_THRESHOLD
=
6
;
...
@@ -348,7 +347,7 @@ public class HashMap<K, V> extends AbstractMap<K, V>
...
@@ -348,7 +347,7 @@ public class HashMap<K, V> extends AbstractMap<K, V>
*/
*/
static
final
int
hash
(
Object
key
)
{
static
final
int
hash
(
Object
key
)
{
int
h
;
int
h
;
return
(
key
==
null
)
?
0
:
(
h
=
key
.
hashCode
())
^
(
h
>>>
16
);
return
(
key
==
null
)
?
0
:
(
h
=
key
.
hashCode
())
^
(
h
>>>
16
);
//扰动函数计算hash值
}
}
/**
/**
...
@@ -388,7 +387,11 @@ public class HashMap<K, V> extends AbstractMap<K, V>
...
@@ -388,7 +387,11 @@ public class HashMap<K, V> extends AbstractMap<K, V>
}
}
/**
/**
* Returns a power of two size for the given target capacity.
* 返回给定目标容量的二次方大小。用于查找距离容量initialCapacity最近的2次幂值
* 任何一个int 数字,都能找到离他最近的 2 的幂次方数字(且比他大)。
* cap是5~7,那么该方法的返回结果就是8;
* cap是9~15,那么该方法的返回结果就是16;
* cap是17~31,那么该方法的返回结果就是32;
*/
*/
static
final
int
tableSizeFor
(
int
cap
)
{
static
final
int
tableSizeFor
(
int
cap
)
{
int
n
=
cap
-
1
;
int
n
=
cap
-
1
;
...
@@ -403,61 +406,40 @@ public class HashMap<K, V> extends AbstractMap<K, V>
...
@@ -403,61 +406,40 @@ public class HashMap<K, V> extends AbstractMap<K, V>
/* ---------------- Fields -------------- */
/* ---------------- Fields -------------- */
/**
/**
* The table, initialized on first use, and resized as
* 该表在首次使用时初始化,并根据需要调整大小。当被分配时,长度总是二的幂。
* necessary. When allocated, length is always a power of two.
* 可以将这个table理解为是一个entry数组;每一个Node即entry,本质都是一个单向链表
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
*/
transient
Node
<
K
,
V
>[]
table
;
transient
Node
<
K
,
V
>[]
table
;
/**
/**
* Holds cached entrySet(). Note that AbstractMap fields are used
* 保留缓存的entrySet
* for keySet() and values().
*/
*/
transient
Set
<
Map
.
Entry
<
K
,
V
>>
entrySet
;
transient
Set
<
Map
.
Entry
<
K
,
V
>>
entrySet
;
/**
/**
*
The number of key-value mappings contained in this map.
*
HashMap的实际元素数量
*/
*/
transient
int
size
;
transient
int
size
;
/**
/**
* The number of times this HashMap has been structurally modified
* HashMap已在结构上修改的次数
* Structural modifications are those that change the number of mappings in
* the HashMap or otherwise modify its internal structure (e.g.,
* rehash). This field is used to make iterators on Collection-views of
* the HashMap fail-fast. (See ConcurrentModificationException).
*/
*/
transient
int
modCount
;
transient
int
modCount
;
/**
/**
* The next size value at which to resize (capacity * load factor).
* 下一次HashMap扩容的阀值大小,如果尚未扩容,则该字段保存初始entry数组的容量,或用零表示
*
* @serial
*/
*/
// (The javadoc description is true upon serialization.
// Additionally, if the table array has not been allocated, this
// field holds the initial array capacity, or zero signifying
// DEFAULT_INITIAL_CAPACITY.)
int
threshold
;
int
threshold
;
/**
/**
* The load factor for the hash table.
* 存储负载因子的常量,初始化的时候将默认的负载因子赋值给它;
*
* @serial
*/
*/
final
float
loadFactor
;
final
float
loadFactor
;
/* ---------------- Public operations -------------- */
/* ---------------- Public operations -------------- */
/**
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* 构造函数 初始容量和负载因子
* capacity and load factor.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
*/
public
HashMap
(
int
initialCapacity
,
float
loadFactor
)
{
public
HashMap
(
int
initialCapacity
,
float
loadFactor
)
{
if
(
initialCapacity
<
0
)
if
(
initialCapacity
<
0
)
...
@@ -635,7 +617,7 @@ public class HashMap<K, V> extends AbstractMap<K, V>
...
@@ -635,7 +617,7 @@ public class HashMap<K, V> extends AbstractMap<K, V>
}
}
/**
/**
*
Implements Map.put and related methods
*
填入值
*
*
* @param hash hash for key
* @param hash hash for key
* @param key the key
* @param key the key
...
@@ -649,23 +631,23 @@ public class HashMap<K, V> extends AbstractMap<K, V>
...
@@ -649,23 +631,23 @@ public class HashMap<K, V> extends AbstractMap<K, V>
Node
<
K
,
V
>[]
tab
;
Node
<
K
,
V
>[]
tab
;
Node
<
K
,
V
>
p
;
Node
<
K
,
V
>
p
;
int
n
,
i
;
int
n
,
i
;
if
((
tab
=
table
)
==
null
||
(
n
=
tab
.
length
)
==
0
)
if
((
tab
=
table
)
==
null
||
(
n
=
tab
.
length
)
==
0
)
//还未进行初始化
n
=
(
tab
=
resize
()).
length
;
n
=
(
tab
=
resize
()).
length
;
//进行初始化
if
((
p
=
tab
[
i
=
(
n
-
1
)
&
hash
])
==
null
)
if
((
p
=
tab
[
i
=
(
n
-
1
)
&
hash
])
==
null
)
//该位置为空,则直接放入
tab
[
i
]
=
newNode
(
hash
,
key
,
value
,
null
);
tab
[
i
]
=
newNode
(
hash
,
key
,
value
,
null
);
else
{
else
{
Node
<
K
,
V
>
e
;
Node
<
K
,
V
>
e
;
K
k
;
K
k
;
if
(
p
.
hash
==
hash
&&
if
(
p
.
hash
==
hash
&&
((
k
=
p
.
key
)
==
key
||
(
key
!=
null
&&
key
.
equals
(
k
))))
((
k
=
p
.
key
)
==
key
||
(
key
!=
null
&&
key
.
equals
(
k
))))
//替换value
e
=
p
;
e
=
p
;
else
if
(
p
instanceof
TreeNode
)
else
if
(
p
instanceof
TreeNode
)
//如果是红黑树
e
=
((
TreeNode
<
K
,
V
>)
p
).
putTreeVal
(
this
,
tab
,
hash
,
key
,
value
);
e
=
((
TreeNode
<
K
,
V
>)
p
).
putTreeVal
(
this
,
tab
,
hash
,
key
,
value
);
else
{
else
{
for
(
int
binCount
=
0
;
;
++
binCount
)
{
for
(
int
binCount
=
0
;
;
++
binCount
)
{
//遍历链表
if
((
e
=
p
.
next
)
==
null
)
{
if
((
e
=
p
.
next
)
==
null
)
{
p
.
next
=
newNode
(
hash
,
key
,
value
,
null
);
p
.
next
=
newNode
(
hash
,
key
,
value
,
null
);
if
(
binCount
>=
TREEIFY_THRESHOLD
-
1
)
//
-1 for 1st
if
(
binCount
>=
TREEIFY_THRESHOLD
-
1
)
//
链表长度大于等于8,则转为红黑树
treeifyBin
(
tab
,
hash
);
treeifyBin
(
tab
,
hash
);
break
;
break
;
}
}
...
@@ -691,74 +673,115 @@ public class HashMap<K, V> extends AbstractMap<K, V>
...
@@ -691,74 +673,115 @@ public class HashMap<K, V> extends AbstractMap<K, V>
}
}
/**
/**
* Initializes or doubles table size. If null, allocates in
* 扩容
* accord with initial capacity target held in field threshold.
* ①用来初始化一个Map,
* Otherwise, because we are using power-of-two expansion, the
* ②或者对Map容量进行扩容
* elements from each bin must either stay at same index, or move
* 初始化或加倍表大小。如果为空,则根据字段阈值中保持的初始容量目标进行分配。
* with a power of two offset in the new table.
* 否则,因为我们使用的是二次幂展开,所以每个bin中的元素必须保持在同一索引处,
* 或者在新表中以二次幂偏移量移动。
* <p>
* <p>
* 第一部分:
* 1.初始化了newThr
* 2.初始化了newCap
* 3.创建newTab
* 第二部分:将原Node数组中的Node元素,迁移到扩容后的新的Node数组中
* 1.将链表分成两个不同的部分,可以使得数据更加的分散,使得链表的长度变短
*
*
* @return the table
* @return the table
*/
*/
final
Node
<
K
,
V
>[]
resize
()
{
final
Node
<
K
,
V
>[]
resize
()
{
//扩容前,原Node数组
Node
<
K
,
V
>[]
oldTab
=
table
;
Node
<
K
,
V
>[]
oldTab
=
table
;
//扩容前,原Node数组的长度
int
oldCap
=
(
oldTab
==
null
)
?
0
:
oldTab
.
length
;
int
oldCap
=
(
oldTab
==
null
)
?
0
:
oldTab
.
length
;
//扩容前,原扩容阀值
int
oldThr
=
threshold
;
int
oldThr
=
threshold
;
//扩容后,新扩容阀值,扩容后,新Node数组的长度
int
newCap
,
newThr
=
0
;
int
newCap
,
newThr
=
0
;
//如果原Node数组的长度>0
if
(
oldCap
>
0
)
{
if
(
oldCap
>
0
)
{
//旧容量大于最大值
if
(
oldCap
>=
MAXIMUM_CAPACITY
)
{
if
(
oldCap
>=
MAXIMUM_CAPACITY
)
{
threshold
=
Integer
.
MAX_VALUE
;
threshold
=
Integer
.
MAX_VALUE
;
return
oldTab
;
return
oldTab
;
}
else
if
((
newCap
=
oldCap
<<
1
)
<
MAXIMUM_CAPACITY
&&
}
else
if
((
newCap
=
oldCap
<<
1
)
<
MAXIMUM_CAPACITY
&&
oldCap
>=
DEFAULT_INITIAL_CAPACITY
)
oldCap
>=
DEFAULT_INITIAL_CAPACITY
)
//新cap是旧cap的2倍,并且旧cap大于16
newThr
=
oldThr
<<
1
;
// double threshold
//阈值翻倍
}
else
if
(
oldThr
>
0
)
// initial capacity was placed in threshold
newThr
=
oldThr
<<
1
;
newCap
=
oldThr
;
}
else
if
(
oldThr
>
0
)
else
{
// zero initial threshold signifies using defaults
newCap
=
oldThr
;
//旧阈值大于0
else
{
//如果初始化的时候用户什么都没传,那么此时oldCap和oldThr都==0
//将默认容量大小DEFAULT_INITIAL_CAPACITY(16)赋给newCap(新Node数组的长度)
newCap
=
DEFAULT_INITIAL_CAPACITY
;
newCap
=
DEFAULT_INITIAL_CAPACITY
;
//将(默认负载因子)0.75f * (默认的容量大小)DEFAULT_INITIAL_CAPACITY的计算结果,赋值给newThr(新的扩容阀值)
newThr
=
(
int
)
(
DEFAULT_LOAD_FACTOR
*
DEFAULT_INITIAL_CAPACITY
);
newThr
=
(
int
)
(
DEFAULT_LOAD_FACTOR
*
DEFAULT_INITIAL_CAPACITY
);
}
}
/**
* 如果初始化的时候用户传入了容量参数和负载因子或者只传入了容量参数,
* 那么意味着oldCap==0、oldThr>0,那么上边的else里边是不会进入的,那么此时newThr(新的扩容阀值)仍然==0
*/
if
(
newThr
==
0
)
{
if
(
newThr
==
0
)
{
//将用户传入的容量参数 * 用户传入的负载因子loadFactor(也可能是默认的负载因子)
float
ft
=
(
float
)
newCap
*
loadFactor
;
float
ft
=
(
float
)
newCap
*
loadFactor
;
//将计算结果赋值给newThr(新的扩容阀值)
newThr
=
(
newCap
<
MAXIMUM_CAPACITY
&&
ft
<
(
float
)
MAXIMUM_CAPACITY
?
newThr
=
(
newCap
<
MAXIMUM_CAPACITY
&&
ft
<
(
float
)
MAXIMUM_CAPACITY
?
(
int
)
ft
:
Integer
.
MAX_VALUE
);
(
int
)
ft
:
Integer
.
MAX_VALUE
);
}
}
//初始化好了扩容阀值
threshold
=
newThr
;
threshold
=
newThr
;
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
Node
<
K
,
V
>[]
newTab
=
(
Node
<
K
,
V
>[])
new
Node
[
newCap
];
//所以新建一个entry数组,容量为newCap,即创建出扩容后新的Node数组
Node
<
K
,
V
>[]
newTab
=
(
Node
<
K
,
V
>[])
new
Node
[
newCap
];
//扩容后,新Node数组
table
=
newTab
;
table
=
newTab
;
if
(
oldTab
!=
null
)
{
if
(
oldTab
!=
null
)
{
//如果原Node数组不为空
for
(
int
j
=
0
;
j
<
oldCap
;
++
j
)
{
for
(
int
j
=
0
;
j
<
oldCap
;
++
j
)
{
//遍历原Node数组
Node
<
K
,
V
>
e
;
Node
<
K
,
V
>
e
;
if
((
e
=
oldTab
[
j
])
!=
null
)
{
if
((
e
=
oldTab
[
j
])
!=
null
)
{
oldTab
[
j
]
=
null
;
oldTab
[
j
]
=
null
;
if
(
e
.
next
==
null
)
if
(
e
.
next
==
null
)
{
//如果该Node元素的next==null,则说明该Node元素后边既没有链表又没有红黑树
//则将该Node元素直接存于新Node数组的指定位置
newTab
[
e
.
hash
&
(
newCap
-
1
)]
=
e
;
newTab
[
e
.
hash
&
(
newCap
-
1
)]
=
e
;
else
if
(
e
instanceof
TreeNode
)
}
else
if
(
e
instanceof
TreeNode
)
{
//如果该Node元素后边跟着的是一个红黑树结构
//在新的Node数组中,将该红黑树进行拆分
//如果拆分后的子树过小(子树的节点小于等于6个),则取消树化,即将其转为链表结构
((
TreeNode
<
K
,
V
>)
e
).
split
(
this
,
newTab
,
j
,
oldCap
);
((
TreeNode
<
K
,
V
>)
e
).
split
(
this
,
newTab
,
j
,
oldCap
);
else
{
// preserve order
}
else
{
//如果是链表的情况下,则进行下面的链表数据转移的操作
Node
<
K
,
V
>
loHead
=
null
,
loTail
=
null
;
Node
<
K
,
V
>
loHead
=
null
,
loTail
=
null
;
//低头尾
Node
<
K
,
V
>
hiHead
=
null
,
hiTail
=
null
;
Node
<
K
,
V
>
hiHead
=
null
,
hiTail
=
null
;
//高头尾
Node
<
K
,
V
>
next
;
Node
<
K
,
V
>
next
;
do
{
do
{
//对链表进行遍历,把链表中的节点分成两个类别,一类是需要更换数组下标的,另一类是不需要的
next
=
e
.
next
;
next
=
e
.
next
;
//如果e.hash&oldCap进行与运算,算出的结果是为0,即说明该Node节点所对应的数组下标不需要改变
if
((
e
.
hash
&
oldCap
)
==
0
)
{
if
((
e
.
hash
&
oldCap
)
==
0
)
{
if
(
loTail
==
null
)
//如果loTail为null,说明该链表没有头节点
if
(
loTail
==
null
)
{
//所以把头节点指向该节点
loHead
=
e
;
loHead
=
e
;
else
}
else
{
//如果该链表有头结点,则把遍历出来的节点放在该链表的尾部
loTail
.
next
=
e
;
loTail
.
next
=
e
;
}
loTail
=
e
;
loTail
=
e
;
}
else
{
}
else
{
//如果e.hash&oldCap进行与运算,算出的结果不为0,则更新该Node节点所对应的数组下标
if
(
hiTail
==
null
)
if
(
hiTail
==
null
)
{
hiHead
=
e
;
hiHead
=
e
;
else
}
else
{
hiTail
.
next
=
e
;
hiTail
.
next
=
e
;
}
hiTail
=
e
;
hiTail
=
e
;
}
}
}
while
((
e
=
next
)
!=
null
);
}
while
((
e
=
next
)
!=
null
);
//该Node节点所对应的数组下标不需要改变,直接把数组下标对应的节点指向新Node数组下标位置链表的头节点
if
(
loTail
!=
null
)
{
if
(
loTail
!=
null
)
{
loTail
.
next
=
null
;
loTail
.
next
=
null
;
newTab
[
j
]
=
loHead
;
newTab
[
j
]
=
loHead
;
}
}
//该Node节点所对应的数组下标需要改变,重新计算出所对应的数组下标值,然后指向新Node数组下标位置链表的头节点
if
(
hiTail
!=
null
)
{
if
(
hiTail
!=
null
)
{
hiTail
.
next
=
null
;
hiTail
.
next
=
null
;
newTab
[
j
+
oldCap
]
=
hiHead
;
newTab
[
j
+
oldCap
]
=
hiHead
;
...
@@ -2284,6 +2307,7 @@ public class HashMap<K, V> extends AbstractMap<K, V>
...
@@ -2284,6 +2307,7 @@ public class HashMap<K, V> extends AbstractMap<K, V>
}
}
}
}
if
(
hiHead
!=
null
)
{
if
(
hiHead
!=
null
)
{
//如果拆分后的子树过小(子树的节点小于等于6个),则取消树化,即将其转为链表结构
if
(
hc
<=
UNTREEIFY_THRESHOLD
)
if
(
hc
<=
UNTREEIFY_THRESHOLD
)
tab
[
index
+
bit
]
=
hiHead
.
untreeify
(
map
);
tab
[
index
+
bit
]
=
hiHead
.
untreeify
(
map
);
else
{
else
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录