Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion src/main/java/core/basesyntax/MyHashMap.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,101 @@
package core.basesyntax;

public class MyHashMap<K, V> implements MyMap<K, V> {
private static final int INIT_CAP = 16;
private static final double MAX_SIZE = 0.75;
private int capacity;
private int size;
private double threshold;
private Node<K, V>[] table;

public MyHashMap() {
table = new Node[INIT_CAP];
capacity = INIT_CAP;
threshold = INIT_CAP * MAX_SIZE;
}

@Override
public void put(K key, V value) {
if (size >= threshold) {
resize();
}
int index = hash(key);
Node<K, V> node = table[index];

if (node == null) {
table[index] = new Node<>(key, value, null);
size++;
return;
}

while (node != null) {
if ((key == node.key) || (key != null && key.equals(node.key))) {
node.value = value;
return;
}
if (node.next == null) {
node.next = new Node<>(key, value, null);
size++;
return;
}
node = node.next;

}
}

@Override
public V getValue(K key) {
int index = hash(key);
Node<K, V> node = table[index];
while (node != null) {
if ((key == node.key) || (key != null && key.equals(node.key))) {
return node.value;
}
node = node.next;
}
return null;
}

@Override
public int getSize() {
return 0;
return size;
}

private void resize() {
size = 0;
Comment on lines +63 to +64

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical bug: Setting size = 0 before re-inserting elements corrupts the size counter. The put() method increments size for each element added, so after all re-insertions, size will only reflect the elements added during resize, not the total map size. Consider removing this line or tracking the original size separately.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line resets size to 0, but subsequent put() calls during rehashing increment it for each element. After resize completes, size will be approximately double the actual count. Remove this line - size should remain unchanged during resize since elements are only reorganized, not newly added.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the critical bug from the previous review. During resize, elements are being REORGANIZED (not newly added), so size should remain unchanged. Setting size = 0 causes the actual size to approximately double after each resize completes, because each put() call on line 75 increments size. Simply remove this line.

int oldCapacity = capacity;
capacity += capacity;
Node<K, V>[] oldTable = table;
Node<K, V>[] newTable = new Node[capacity];
table = newTable;
for (int i = 0; i < oldCapacity; i++) {
Node<K, V> node = oldTable[i];

while (node != null) {
Node<K, V> next = node.next;
put(node.key, node.value);
node = next;
}
}
threshold = capacity * MAX_SIZE;
}

private int hash(Object key) {
if (key == null) {
return 0;
}
return Math.abs(key.hashCode() % capacity);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Node class is missing the private access modifier. Per checklist requirements, all classes/fields/methods should have access modifiers, and internal utility classes should be private to maintain encapsulation. Change to 'private static class Node<K, V>'.

class Node<K, V> {
private K key;
private V value;
private Node<K, V> next;

public Node(K key, V value, Node<K, V> next) {
this.key = key;
this.value = value;
this.next = next;
}
}
}
Loading