File tree Expand file tree Collapse file tree 2 files changed +9
-9
lines changed
Exercise_27/Exercise_27_04 Expand file tree Collapse file tree 2 files changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ public java.util.Set<V> getAll(K key) {
110
110
if (table .get (index ).getKey ().equals (key )) {
111
111
set .add (table .get (index ).getValue ());
112
112
}
113
-
113
+
114
114
index ++;
115
115
index %= capacity ;
116
116
}
@@ -162,18 +162,18 @@ public V put(K key, V value) {
162
162
@ Override /** Remove the entry for the specified key */
163
163
public void remove (K key ) {
164
164
int index = hash (key .hashCode ());
165
- int i = index - 1 ;
166
165
167
- while (i != index && (table .get (index ) == null ||
168
- table .get (index ).getKey () != key )) {
166
+ // Remove the first entry that matches the key
167
+ while (table .get (index ) != null ) {
168
+ if (table .get (index ).getKey ().equals (key )) {
169
+ table .remove (index );
170
+ size --; // Decrease size
171
+ break ; // Remove just one entry that matches the key
172
+ }
173
+
169
174
index ++;
170
175
index %= capacity ;
171
176
}
172
-
173
- if (table .get (index ).getKey () == key ) {
174
- table .remove (index );
175
- size --; // Decrease size
176
- }
177
177
}
178
178
179
179
@ Override /** Return the number of entries in this map */
You can’t perform that action at this time.
0 commit comments