-
Notifications
You must be signed in to change notification settings - Fork 1.5k
first solution #1686
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 solution #1686
Changes from 2 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,113 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| public class MyHashMap<K, V> implements MyMap<K, V> { | ||
| private static final int DEFAULT_INITIAL_CAPACITY = 16; | ||
| private static final float DEFAULT_LOAD_FACTOR = 0.75f; | ||
|
|
||
| static class Node<K, V> implements Map.Entry<K, V> { | ||
|
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 |
||
| private final 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. This violates checklist item #5: 'Private methods and classes should be after public ones in your class.' Please move the private |
||
| private final K key; | ||
| private V value; | ||
| private Node<K, V> next; | ||
|
|
||
| Node(int hash, K key, V value, Node<K, V> next) { | ||
| this.hash = hash; | ||
| this.key = key; | ||
| this.value = value; | ||
| this.next = next; | ||
| } | ||
|
|
||
| @Override | ||
| public K getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| @Override | ||
| public V getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public V setValue(V value) { | ||
| V oldValue = this.value; | ||
| this.value = value; | ||
| return oldValue; | ||
| } | ||
|
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 violates checklist item #3: 'Don't use getters and setters in class Node'. While implementing |
||
| } | ||
|
|
||
| private Node<K, V>[] table; | ||
| private int size; | ||
| private int threshold; | ||
|
|
||
| public MyHashMap() { | ||
| table = new Node[DEFAULT_INITIAL_CAPACITY]; | ||
| threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); | ||
| } | ||
|
|
||
| private int hash(K key) { | ||
|
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 violates checklist item #5: 'Private methods and classes should be after public ones in your class.' Private helper methods should be placed after all public methods in the class. 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 violates checklist item #5: 'Private methods and classes should be after public ones in your class.' Please move this private helper method to a position after all public methods like |
||
| return key == null ? 0 : key.hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| if (size >= threshold) { | ||
| resize(); | ||
| } | ||
| int hash = hash(key); | ||
| int index = (hash & 0x7fffffff) % table.length; | ||
|
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 violates checklist item #2: 'Any magic numbers should be constants'. The value 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 violates checklist item #2: 'Any magic numbers should be constants'. The value |
||
| Node<K, V> current = table[index]; | ||
|
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 violates checklist item #2: 'Any magic numbers should be constants'. While using |
||
| while (current != null) { | ||
| // Porównujemy hash oraz klucze (Objects.equals radzi sobie z null) | ||
| if (current.hash == hash && Objects.equals(current.key, key)) { | ||
| current.value = value; | ||
| return; | ||
| } | ||
| current = current.next; | ||
| } | ||
|
|
||
| Node<K, V> newNode = new Node<>(hash, key, value, table[index]); | ||
| table[index] = newNode; | ||
| size++; | ||
| } | ||
|
|
||
| @Override | ||
| public V getValue(K key) { | ||
| int hash = hash(key); | ||
| int index = (hash & 0x7fffffff) % table.length; | ||
| Node<K, V> current = table[index]; | ||
| while (current != null) { | ||
| if (current.hash == hash && Objects.equals(current.key, key)) { | ||
| return current.value; | ||
| } | ||
| current = current.next; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public int getSize() { | ||
| return 0; | ||
| return size; | ||
| } | ||
|
|
||
| private void resize() { | ||
| int newCapacity = table.length * 2; | ||
| Node<K, V>[] newTable = new Node[newCapacity]; | ||
| for (Node<K, V> node : table) { | ||
| while (node != null) { | ||
| Node<K, V> next = node.next; | ||
| int index = (node.hash & 0x7fffffff) % newCapacity; | ||
| node.next = newTable[index]; | ||
| newTable[index] = node; | ||
| node = next; | ||
| } | ||
| } | ||
| table = newTable; | ||
| threshold = (int) (newCapacity * DEFAULT_LOAD_FACTOR); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
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 #5: 'Private methods and classes should be after public ones in your class.' and #7: 'Remember about access modifiers.' The
Nodeclass should be declaredprivatesince it is only used withinMyHashMap. It should also be moved to the bottom of theMyHashMapclass, after all public methods.