Skip to content

Commit 081c371

Browse files
author
jsquared21
committed
Edit method MyHashMap.get(key) for efficiency
1 parent 5c40747 commit 081c371

File tree

2 files changed

+5
-7
lines changed

2 files changed

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

Exercise_27/Exercise_27_04/MyHashMap.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,16 @@ public java.util.Set<Entry<K, V>> entrySet() {
8888
@Override /** Return the value that matched the specified key */
8989
public V get(K key) {
9090
int index = hash(key.hashCode());
91-
int i = index - 1;
9291

93-
while (i != index && (table.get(index) == null ||
94-
table.get(index).getKey() != key)) {
92+
while (table.get(index) != null) {
93+
if (table.get(index).getKey().equals(key)) {
94+
return table.get(index).getValue();
95+
}
96+
9597
index++;
9698
index %= capacity;
9799
}
98100

99-
if (table.get(index).getKey() == key) {
100-
return table.get(index).getValue();
101-
}
102-
103101
return null;
104102
}
105103

0 commit comments

Comments
 (0)