Skip to content

Commit 5f1aaac

Browse files
author
jsquared21
committed
Edit method MyHashMap.get(key) for efficiency
1 parent 0cce7b9 commit 5f1aaac

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed
-37 Bytes
Binary file not shown.

Exercise_27/Exercise_27_01/MyHashMap.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,15 @@ public java.util.Set<MyMap.Entry<K,V>> entrySet() {
8888
@Override /** Return the value that matches the specified key */
8989
public V get(K key) {
9090
int index = hash(key.hashCode());
91-
int i = index - 1; // One cycle of the arraylist
9291

93-
// While index is empty or a collision occurs check the next index
94-
while (index != i && (table.get(index) == null ||
95-
table.get(index).getKey() != key)) {
92+
while(table.get(index) != null) {
93+
if (table.get(index).getKey() == key) {
94+
return table.get(index).getValue();
95+
}
9696
index++;
9797
index %= capacity;
9898
}
9999

100-
if (table.get(index).getKey() == key) {
101-
return table.get(index).getValue();
102-
}
103-
104100
return null;
105101
}
106102

0 commit comments

Comments
 (0)