Skip to content

Commit c9b16dd

Browse files
author
jsquared21
committed
Fix method MyHashMap.rehash()
1 parent efe3acb commit c9b16dd

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed
115 Bytes
Binary file not shown.

Exercise_27/Exercise_27_01/MyHashMap.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,15 @@ private void removeEntries() {
215215

216216
/** Rehash the map */
217217
private void rehash() {
218+
java.util.Set<Entry<K, V>> set = entrySet();
218219
capacity <<= 1; // Same as capacity *= 2. <= is more efficient
219-
for (int i = (size + 1); i < capacity; i++) {
220+
size = 0; // Reset size to 0
221+
table.clear(); // Clear the hash table
222+
for (int i = 0; i < capacity; i++)
220223
table.add(null);
224+
225+
for (Entry<K, V> entry : set) {
226+
put(entry.getKey(), entry.getValue());
221227
}
222228
}
223229

0 commit comments

Comments
 (0)