给HashMap做个深度剖析手术
来源:互联网 发布:centos mosh 编辑:程序博客网 时间:2024/04/29 14:45
标签:HashMap 机制 原理 剖析
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://yangfei520.blog.51cto.com/1041581/1566942
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* 初始容量,大小必须是2的指数次方,默认是16.
*/
static
final
int
DEFAULT_INITIAL_CAPACITY =
16
;
/**
* 默认最大容量,值为1<<30
*/
static
final
int
MAXIMUM_CAPACITY =
1
<<
30
;
/**
* 哈希表的默认加载因子,值为0.75.
*/
static
final
float
DEFAULT_LOAD_FACTOR =
0
.75f;
/**
* 存储元素的数组,大小必须是2的指数次方,默认是16.
*/
transient
Entry[] table;
/**
* THashMap中的存储的<K,V>映射的数目.
*/
transient
int
size;
/**
* threshold=容量*加载因子。当实际数目大于threshold时,HashMap就需要扩容.
* @serial
*/
int
threshold;
/**
* 哈希表的加载因子,如果创建时不指定loadFactor,则使用DEFAULT_LOAD_FACTOR.
*
* @serial
*/
final
float
loadFactor;
//... ...
/**
*
* <p>用一个静态内部类来定义数组项链表的元素</p>
*
* @author bruce.yang
* @version 1.0 Created on 2014-10-22 上午11:39:50
*/
static
class
Entry<K,V>
implements
Map.Entry<K,V> {
final
K key;
V value;
Entry<K,V> next;
final
int
hash;
/**
* Creates new entry.
*/
Entry(
int
h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
public
final
K getKey() {
return
key;
}
public
final
V getValue() {
return
value;
}
public
final
V setValue(V newValue) {
V oldValue = value;
value = newValue;
return
oldValue;
}
public
final
boolean
equals(Object o) {
if
(!(o
instanceof
Map.Entry))
return
false
;
Map.Entry e = (Map.Entry)o;
Object k1 = getKey();
Object k2 = e.getKey();
if
(k1 == k2 || (k1 !=
null
&& k1.equals(k2))) {
Object v1 = getValue();
Object v2 = e.getValue();
if
(v1 == v2 || (v1 !=
null
&& v1.equals(v2)))
return
true
;
}
return
false
;
}
public
final
int
hashCode() {
return
(key==
null
?
0
: key.hashCode()) ^
(value==
null
?
0
: value.hashCode());
}
public
final
String toString() {
return
getKey() +
"="
+ getValue();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* 建立指定key和value之间的映射:
* 如果已经存在key,则替换对应的value,并返回原来的value;如果原来没有建立映射,则建立映射,并返回null。
* 当然了,由于HashMap允许key和value为null,返回null还有可能就是原来的value为null。
*/
public
V put(K key, V value) {
//如果key==null,则调用专门的方法处理
if
(key ==
null
)
return
putForNullKey(value);
int
hash = hash(key.hashCode());
int
i = indexFor(hash, table.length);
for
(Entry<K,V> e = table[i]; e !=
null
; e = e.next) {
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++;
//加入新的项
addEntry(hash, key, value, i);
//原来不存在这项
return
null
;
}
private
V putForNullKey(V value) {
for
(Entry<K,V> e = table[
0
]; e !=
null
; e = e.next) {
if
(e.key ==
null
) {
V oldValue = e.value;
e.value = value;
e.recordAccess(
this
);
return
oldValue;
}
}
modCount++;
addEntry(
0
,
null
, value,
0
);
return
null
;
}
/**
* 添加key-value项
*/
void
addEntry(
int
hash, K key, V value,
int
bucketIndex) {
Entry<K,V> e = table[bucketIndex];
//加入该项到链表头部
table[bucketIndex] =
new
Entry<K,V>(hash, key, value, e);
if
(size++ >= threshold)
resize(
2
* table.length);
}
/**
*扩充HashMap的容量
*/
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);
table = newTable;
threshold = (
int
)(newCapacity * loadFactor);
}
/**
* 将原来数组中元素传输到新数组中
*/
void
transfer(Entry[] newTable) {
Entry[] src = table;
int
newCapacity = newTable.length;
for
(
int
j =
0
; j < src.length; j++) {
Entry<K,V> e = src[j];
if
(e !=
null
) {
src[j] =
null
;
do
{
Entry<K,V> next = e.next;
int
i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
while
(e !=
null
);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 根据key取得value
*/
public
V get(Object key) {
if
(key ==
null
)
return
getForNullKey();
int
hash = hash(key.hashCode());
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.equals(k)))
return
e.value;
}
return
null
;
}
private
V getForNullKey() {
for
(Entry<K,V> e = table[
0
]; e !=
null
; e = e.next) {
if
(e.key ==
null
)
return
e.value;
}
return
null
;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
*指定初始化容量和加载因子
*/
public
HashMap(
int
initialCapacity,
float
loadFactor) {
//initialCapacity不能小于0
if
(initialCapacity <
0
)
throw
new
IllegalArgumentException(
"Illegal initial capacity: "
+
initialCapacity);
//如果指定容量大于默认最大容量,则按照默认最大容量创建
if
(initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
//loadFactor不能小于0
if
(loadFactor <=
0
|| Float.isNaN(loadFactor))
throw
new
IllegalArgumentException(
"Illegal load factor: "
+
loadFactor);
//找最大于指定初始化容量的2的指数幂作为表的真实容量
int
capacity =
1
;
while
(capacity < initialCapacity)
capacity <<=
1
;
this
.loadFactor = loadFactor;
threshold = (
int
)(capacity * loadFactor);
table =
new
Entry[capacity];
init();
}
/**
*只指定初始化容量,加载因子采用默认
*/
public
HashMap(
int
initialCapacity) {
this
(initialCapacity, DEFAULT_LOAD_FACTOR);
}
/**
* 初始化容量和加载因子都采用默认
*/
public
HashMap() {
this
.loadFactor = DEFAULT_LOAD_FACTOR;
threshold = (
int
)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
table =
new
Entry[DEFAULT_INITIAL_CAPACITY];
init();
}
0 0
- 给HashMap做个深度剖析手术
- 有个朋友要做眼睛手术
- Android面试题----HashMap深度剖析
- 重庆做静脉曲张手术
- JavaSE第五十二讲:HashSet 与HashMap源代码深度剖析
- 给自己做个广告
- 给自己做个广告
- 给自己做个标记
- 给自己做个计划
- 南宁做肛瘘手术视频
- 南宁做肛瘘手术费用
- HashMap剖析
- HashMap剖析
- Hashmap 剖析
- 给 eclipse 做个日历的插件
- 给自己的博客做个广告
- 给自己的网站做个“toolbar”
- 给eclipse 做个日历的插件
- 工程师薪酬分析:Java 不是过去式
- 文章标题
- Java面试--io
- 使用InjectView和findViewById说拜拜
- Linux下Tomcat的安装配置
- 给HashMap做个深度剖析手术
- bootstrap入门
- 表达式树 csu1655 文本计算器
- jQuery的animate中callback函数会立即执行
- 细节处理
- Fiddler 学习(一)
- hdu4059容斥原理
- HNOI2008玩具装箱(T_OY)bzoj1010
- Oracle11g和oracle10g之间的导入/导出