Skip to content

Commit 2e8e9b6

Browse files
author
jsquared21
committed
Edit method MyHashMap.get(key) for efficiency
1 parent 3a48e3f commit 2e8e9b6

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed
-17 Bytes
Binary file not shown.

Exercise_27/Exercise_27_02/MyHashMap.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,16 @@ public java.util.Set<MyMap.Entry<K, V>> entrySet() {
8989
@Override /** Return the value that matches the specified key */
9090
public V get(K key) {
9191
int index = hash(key.hashCode());
92-
int i = index - 1; // One cycle of the arraylist
9392
int j = 0;
94-
95-
while (index != i && (table.get(index) == null ||
96-
table.get(index).getKey() != key)) {
93+
94+
while (table.get(index) != null) {
95+
if (table.get(index).getKey().equals(key)) {
96+
return table.get(index).getValue();
97+
}
9798
index += Math.pow(j++, 2);
9899
index %= capacity;
99100
}
100101

101-
if (table.get(index).getKey() == key)
102-
return table.get(index).getValue();
103-
104102
return null;
105103
}
106104

0 commit comments

Comments
 (0)