Skip to content

Commit 6b502c3

Browse files
author
Deepak Malik
committed
Bug fix in associative array
1 parent ba6eae8 commit 6b502c3

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/com/deepak/data/structures/Arrays/AssociativeArray.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ public void put(K key, V value) {
5454
/* Find the hash of the key and bucket it belongs to */
5555
int hash = key.hashCode();
5656
int bucket = getBucket(hash);
57-
/* Loop through each entry of the associative array
57+
Entry<K, V> entry = table[bucket];
58+
/* Loop through the entry of the associative array
5859
* and check if it's just a value update or a new key, value */
59-
for (Entry<K, V> entry : table) {
60-
if (entry != null && entry.getKey().equals(key)) {
61-
entry.value = value;
60+
while (entry != null) {
61+
if (entry.getHash() == hash && entry.getKey().equals(key)) {
6262
isNewEntry = false;
63+
entry.value = value;
6364
}
65+
entry = entry.next;
6466
}
6567
/* Create a new entry and push to array */
6668
if (isNewEntry) {

0 commit comments

Comments
 (0)