Hashmap源码解析-get

get#

查找过程和删除基本差不多, 找到返回节点,否则返回null

系列目录#

  1. 总览&目录
  2. 链表节点NODE
  3. 构造函数
  4. 扩容函数
  5. put
  6. remove
  7. get
  8. 遍历
  9. &hashtable
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
public V get(Object key) {

Node<K,V> e;

//传入扰动后的哈希值 和 key 找到目标节点Node

return (e = getNode(hash(key), key)) == null ? null : e.value;

}

//传入扰动后的哈希值 和 key 找到目标节点Node

final Node<K,V> getNode(int hash, Object key) {

Node<K,V>[] tab; Node<K,V> first, e; int n; K k;

//查找过程和删除基本差不多, 找到返回节点,否则返回null

if ((tab = table) != null && (n = tab.length) > 0 &&

(first = tab[(n - 1) & hash]) != null) {

if (first.hash == hash && // always check first node

((k = first.key) == key || (key != null && key.equals(k))))

return first;

if ((e = first.next) != null) {

if (first instanceof TreeNode)

return ((TreeNode<K,V>)first).getTreeNode(hash, key);

do {

if (e.hash == hash &&

((k = e.key) == key || (key != null && key.equals(k))))

return e;

} while ((e = e.next) != null);

}

}

return null;

}

public boolean containsKey(Object key) {

return getNode(hash(key), key) != null;

}

public boolean containsValue(Object value) {

Node<K,V>[] tab; V v;

//遍历哈希桶上的每一个链表

if ((tab = table) != null && size > 0) {

for (int i = 0; i < tab.length; ++i) {

for (Node<K,V> e = tab[i]; e != null; e = e.next) {

//如果找到value一致的返回true

if ((v = e.value) == value ||

(value != null && value.equals(v)))

return true;

}

}

}

return false;

}
Your browser is out-of-date!

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

×