-
Notifications
You must be signed in to change notification settings - Fork 1.5k
made my own hashmap #1702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
made my own hashmap #1702
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical bug: Setting
size = 0before re-inserting elements corrupts the size counter. Theput()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.