Skip to content

Commit

Permalink
Edit method MyHashMap.remove(key) for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
jsquared21 committed Sep 7, 2016
1 parent 5711790 commit 73ddcf1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Binary file modified Exercise_27/Exercise_27_04/MyHashMap.class
Binary file not shown.
18 changes: 9 additions & 9 deletions Exercise_27/Exercise_27_04/MyHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public java.util.Set<V> getAll(K key) {
if (table.get(index).getKey().equals(key)) {
set.add(table.get(index).getValue());
}

index++;
index %= capacity;
}
Expand Down Expand Up @@ -162,18 +162,18 @@ public V put(K key, V value) {
@Override /** Remove the entry for the specified key */
public void remove(K key) {
int index = hash(key.hashCode());
int i = index - 1;

while (i != index && (table.get(index) == null ||
table.get(index).getKey() != key)) {
// Remove the first entry that matches the key
while (table.get(index) != null) {
if (table.get(index).getKey().equals(key)) {
table.remove(index);
size--; // Decrease size
break; // Remove just one entry that matches the key
}

index++;
index %= capacity;
}

if (table.get(index).getKey() == key) {
table.remove(index);
size--; // Decrease size
}
}

@Override /** Return the number of entries in this map */
Expand Down

0 comments on commit 73ddcf1

Please sign in to comment.