Skip to content

Commit c73855d

Browse files
author
jsquared21
committed
Edit method MyHashMap.put(key, value) for efficiency
1 parent a2c6869 commit c73855d

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed
3 Bytes
Binary file not shown.

Exercise_27/Exercise_27_03/MyHashMap.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public V get(K key) {
9696
if (table.get(index).getKey().equals(key)) {
9797
return table.get(index).getValue();
9898
}
99-
99+
100100
// Secondary hash: (k + j * h'(key)) % N
101101
index = hash1 + j++ * hash2(hash1);
102102
index %= capacity;
@@ -129,8 +129,8 @@ public V put(K key, V value) {
129129
int j = 0;
130130

131131
while (table.get(index) != null) {
132-
// Add a new entry (key, value)
133-
if (table.get(index).getKey() == key) {
132+
// The key is already in the map
133+
if (table.get(index).getKey().equals(key)) {
134134
Entry<K, V> entry = table.get(index);
135135
V oldValue = entry.getValue();
136136
// Replace old value with new value

0 commit comments

Comments
 (0)