-
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 5 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,92 @@ | ||||||
| package core.basesyntax; | ||||||
|
|
||||||
| 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; | ||||||
| private static final int HASH_MASK = Integer.MAX_VALUE; | ||||||
|
|
||||||
| public MyHashMap() { | ||||||
| table = new Node[DEFAULT_INITIAL_CAPACITY]; | ||||||
| threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void put(K key, V value) { | ||||||
| if (size >= threshold) { | ||||||
| resize(); | ||||||
| } | ||||||
| int hash = hash(key); | ||||||
| int index = (hash & HASH_MASK) % table.length; | ||||||
| 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) { | ||||||
| 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 & HASH_MASK) % 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 & HASH_MASK) % newCapacity; | ||||||
| node.next = newTable[index]; | ||||||
| newTable[index] = node; | ||||||
| node = next; | ||||||
| } | ||||||
| } | ||||||
| table = newTable; | ||||||
| threshold = (int) (newCapacity * DEFAULT_LOAD_FACTOR); | ||||||
| } | ||||||
|
|
||||||
| private int hash(K key) { | ||||||
| return key == null ? 0 : key.hashCode(); | ||||||
| } | ||||||
|
|
||||||
| private class Node<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.
Suggested change
Since |
||||||
| private final int hash; | ||||||
| private final K key; | ||||||
| private V value; | ||||||
| private Node<K, V> next; | ||||||
|
|
||||||
| public Node(int hash, K key, V value, Node<K, V> next) { | ||||||
|
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. Since
Suggested change
|
||||||
| this.hash = hash; | ||||||
| this.key = key; | ||||||
| this.value = value; | ||||||
| this.next = next; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private Node<K, V>[] table; | ||||||
| private int size; | ||||||
| private int threshold; | ||||||
| } | ||||||
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.
Index calculation is duplicated, this appears in more than one place:
It would be cleaner to extract it into a private helper method, for example: