MCPcopy
hub / github.com/wangzheng0822/algo / put

Method put

java/18_hashtable/HashTable.java:59–92  ·  view source on GitHub ↗

新增 @param key @param value

(K key, V value)

Source from the content-addressed store, hash-verified

57 * @param value
58 */
59 public void put(K key, V value) {
60 int index = hash(key);
61 // 位置未被引用,创建哨兵节点
62 if (table[index] == null) {
63 table[index] = new Entry<>(null, null, null);
64 }
65
66 Entry<K, V> tmp = table[index];
67 // 新增节点
68 if (tmp.next == null) {
69 tmp.next = new Entry<>(key, value, null);
70 size++;
71 use++;
72 // 动态扩容
73 if (use >= table.length * LOAD_FACTOR) {
74 resize();
75 }
76 }
77 // 解决散列冲突,使用链表法
78 else {
79 do {
80 tmp = tmp.next;
81 // key相同,覆盖旧的数据
82 if (tmp.key == key) {
83 tmp.value = value;
84 return;
85 }
86 } while (tmp.next != null);
87
88 Entry<K, V> temp = table[index].next;
89 table[index].next = new Entry<>(key, value, temp);
90 size++;
91 }
92 }
93
94 /**
95 * 散列函数

Callers 5

addMethod · 0.45
insertMethod · 0.45
updateMethod · 0.45
cacheMethod · 0.45
rightShiftMethod · 0.45

Calls 2

hashMethod · 0.95
resizeMethod · 0.95

Tested by

no test coverage detected