Skip to content

Commit 6fde7c4

Browse files
author
jsquared21
committed
Edit method MyHashMap.put(key, value) for efficiency
1 parent 80fc346 commit 6fde7c4

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_01/MyHashMap.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ public java.util.Set<K> keySet() {
120120
@Override /** Add an entry (key, value) into the map */
121121
public V put(K key, V value) {
122122
int index = hash(key.hashCode());
123-
123+
124124
while (table.get(index) != null) {
125-
// Add a new entry (key, value)
126-
if (table.get(index).getKey() == key) {
125+
// The key is already in the map
126+
if (table.get(index).getKey().equals(key)) {
127127
Entry<K, V> entry = table.get(index);
128128
V oldvalue = entry.getValue();
129129
// Replace old value with new value

0 commit comments

Comments
 (0)