-
Notifications
You must be signed in to change notification settings - Fork 1.5k
first commit #1703
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?
first commit #1703
Changes from 3 commits
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,89 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class MyHashMap<K, V> implements MyMap<K, V> { | ||
|
Comment on lines
1
to
2
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. Add empty line after package declaration. |
||
| private Node<K, V>[] table; | ||
| private int size; | ||
| private static final int DEFAULT_CAPACITY = 16; | ||
| private static final int DEFAULT_RESIZE= 2; | ||
| private static final float LOAD_FACTOR = 0.75f; | ||
|
|
||
|
|
||
|
|
||
|
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. There should be no empty lines before the class declaration. Remove lines 9, 10, 11. |
||
| public MyHashMap() { | ||
| table = new Node[DEFAULT_CAPACITY]; | ||
| size = 0; | ||
| } | ||
|
|
||
| private void checkThreshold() { | ||
| if (size >= table.length * LOAD_FACTOR) { | ||
|
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. Resize is triggered after adding a new element, currently, the logic is: This means that the map first adds the new element and only then checks whether resizing is needed. This is not necessarily a critical bug, but the current condition: means that for the default capacity Usually, it is slightly clearer to resize only when the threshold is exceeded: or to check before adding the new element: The current solution can work, but this small change would make the behavior more natural. |
||
| resize(); | ||
| } | ||
| } | ||
|
|
||
| private void resize() { | ||
| Node<K, V>[] oldTable = table; | ||
| table = new Node[oldTable.length * DEFAULT_RESIZE]; | ||
| size = 0; | ||
|
|
||
| for (Node<K, V> node : oldTable) { | ||
| while (node != null) { | ||
| put(node.key, node.value); | ||
| node = node.next; | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+14
to
+31
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. According to the checklist:
Currently, |
||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| int hash = key == null ? 0 : key.hashCode(); | ||
| int index = hash & (table.length - 1); | ||
|
|
||
| Node<K, V> current = table[index]; | ||
| while (current != null) { | ||
| if ((current.key == null && key == null) || (current.key != null && current.key.equals(key))) { | ||
| current.value = value; | ||
| return; | ||
| } | ||
| current = current.next; | ||
| } | ||
|
|
||
| Node<K, V> newNode = new Node<>(key, value); | ||
| newNode.hash = hash; | ||
| newNode.next = table[index]; | ||
| table[index] = newNode; | ||
| size++; | ||
| checkThreshold(); | ||
| } | ||
|
|
||
| @Override | ||
| public V getValue(K key) { | ||
| int hash = key == null ? 0 : key.hashCode(); | ||
| int index = hash & (table.length - 1); | ||
|
|
||
| Node<K, V> current = table[index]; | ||
| while (current != null) { | ||
| if ((current.key == null && key == null) || (current.key != null && current.key.equals(key))) { | ||
| return current.value; | ||
| } | ||
| current = current.next; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public int getSize() { | ||
| return 0; | ||
| return size; | ||
| } | ||
|
|
||
| private static class Node<K, V> { | ||
| private final K key; | ||
| private V value; | ||
| private int hash; | ||
|
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 but this field is not used anywhere later. In This field can be removed because it does not bring any value to the current implementation. |
||
| private Node<K, V> next; | ||
|
|
||
| public Node(K key, V value) { | ||
| this.key = key; | ||
| this.value = value; | ||
| } | ||
| } | ||
| } | ||
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.
This violates checklist item #1: 'Don't begin class or method implementation with empty line.' There should be no empty lines between the package declaration and class declaration.