Skip to content

Commit a2c6869

Browse files
author
jsquared21
committed
Edit method MyHashMap.get(key) for efficiency
1 parent 2870c6d commit a2c6869

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed
22 Bytes
Binary file not shown.

Exercise_27/Exercise_27_03/MyHashMap.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ public V get(K key) {
9292
int index = hash1;
9393
int j = 0;
9494

95-
while (table.get(index) == null) {
95+
while (table.get(index) != null) {
96+
if (table.get(index).getKey().equals(key)) {
97+
return table.get(index).getValue();
98+
}
99+
96100
// Secondary hash: (k + j * h'(key)) % N
97101
index = hash1 + j++ * hash2(hash1);
98102
index %= capacity;
99103
}
100104

101-
if (table.get(index).getKey() == key) {
102-
return table.get(index).getValue();
103-
}
104-
105105
return null;
106106
}
107107

0 commit comments

Comments
 (0)