Hashmap源码解析-遍历

系列目录#

  1. 总览&目录
  2. 链表节点NODE
  3. 构造函数
  4. 扩容函数
  5. put
  6. remove
  7. get
  8. 遍历
  9. &hashtable

遍历#

1.Node implements Map.Entry

2.EntryIterator extends HashIterator implements Iterator<Map.Entry<K,V>>

3.迭代器HashIterator实现 expectedModCount = modCount 支持fastfail

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
//缓存 entrySet

transient Set<Map.Entry<K,V>> entrySet;

*/

public Set<Map.Entry<K,V>> entrySet() {

Set<Map.Entry<K,V>> es;

return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;

}

final class EntrySet extends AbstractSet<Map.Entry<K,V>> {

public final int size() { return size; }

public final void clear() { HashMap.this.clear(); }

//一般我们用到EntrySet,都是为了获取iterator

public final Iterator<Map.Entry<K,V>> iterator() {

return new EntryIterator();

}

//最终还是调用getNode方法

public final boolean contains(Object o) {

if (!(o instanceof Map.Entry))

return false;

Map.Entry<?,?> e = (Map.Entry<?,?>) o;

Object key = e.getKey();

Node<K,V> candidate = getNode(hash(key), key);

return candidate != null && candidate.equals(e);

}

//最终还是调用removeNode方法

public final boolean remove(Object o) {

if (o instanceof Map.Entry) {

Map.Entry<?,?> e = (Map.Entry<?,?>) o;

Object key = e.getKey();

Object value = e.getValue();

return removeNode(hash(key), key, value, true, true) != null;

}

return false;

}

//。。。

}
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
final class EntryIterator extends HashIterator implements Iterator<Map.Entry<K,V>> {

public final Map.Entry<K,V> next() { return nextNode(); }

}

abstract class HashIterator {

Node<K,V> next; // next entry to return

Node<K,V> current; // current entry

int expectedModCount; // for fast-fail

int index; // current slot

HashIterator() {

//因为hashmap也是线程不安全的,所以要保存modCount。用于fail-fast策略

expectedModCount = modCount;

Node<K,V>[] t = table;

current = next = null;

index = 0;

//next 初始时,指向 哈希桶上第一个不为null的链表头

if (t != null && size > 0) { // advance to first entry

do {} while (index < t.length && (next = t[index++]) == null);

}

}

public final boolean hasNext() {

return next != null;

}

//由这个方法可以看出,遍历HashMap时,顺序是按照哈希桶从低到高,链表从前往后,依次遍历的。属于无序集合。

final Node<K,V> nextNode() {

Node<K,V>[] t;

Node<K,V> e = next;

//fail-fast策略

if (modCount != expectedModCount)

throw new ConcurrentModificationException();

if (e == null)

throw new NoSuchElementException();

//依次取链表下一个节点,

if ((next = (current = e).next) == null && (t = table) != null) {

//如果当前链表节点遍历完了,则取哈希桶下一个不为null的链表头

do {} while (index < t.length && (next = t[index++]) == null);

}

return e;

}

public final void remove() {

Node<K,V> p = current;

if (p == null)

throw new IllegalStateException();

////fail-fast策略

if (modCount != expectedModCount)

throw new ConcurrentModificationException();

current = null;

K key = p.key;

//最终还是利用removeNode 删除节点

removeNode(hash(key), key, null, false, false);

expectedModCount = modCount;

}

}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×