Hashmap源码解析-链表节点NODE

系列目录#

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

链表节点NODE#

重点:

1.单链表

2.hashCode()是将key的hashCode和value的hashCode异或得到

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
static class Node<K,V> implements Map.Entry<K,V> {

final int hash; //哈希值

final K key; //key

V value; //value

Node<K,V> next; //链表后置节点

Node(int hash, K key, V value, Node<K,V> next) {

this.hash = hash;

this.key = key;

this.value = value;

this.next = next;

}

public final K getKey() { return key; }

public final V getValue() { return value; }

public final String toString() { return key + "=" + value; }

//每一个节点的hash值,是将key的hashCode 和 value的hashCode 亦或得到的。

public final int hashCode() {

return Objects.hashCode(key) ^ Objects.hashCode(value);

}

//设置新的value 同时返回旧value

public final V setValue(V newValue) {

V oldValue = value;

value = newValue;

return oldValue;

}

public final boolean equals(Object o) {

if (o == this)

return true;

if (o instanceof Map.Entry) {

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

if (Objects.equals(key, e.getKey()) &&

Objects.equals(value, e.getValue()))

return true;

}

return false;

}

}
Your browser is out-of-date!

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

×