-
Notifications
You must be signed in to change notification settings - Fork 1.5k
done #1698
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?
done #1698
Changes from 4 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,148 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class MyHashMap<K, V> implements MyMap<K, V> { | ||
| private static final int initialCapacity = 16; | ||
| private static final int sizeMultiplier = 2; | ||
| private static final double loadFactor = 0.75; | ||
| private int currentMaxSize = initialCapacity; | ||
| private int threshold = (int) (initialCapacity * loadFactor); | ||
| private Node<K, V>[] buckets; | ||
| private int size = 0; | ||
|
|
||
| public MyHashMap() { | ||
|
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 #1: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)" — there is an empty line before the constructor. Remove the redundant blank line so the constructor does not begin with an empty line. |
||
| buckets = new Node[initialCapacity]; | ||
| } | ||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| if (size >= threshold) { | ||
| resize(); | ||
| } | ||
|
|
||
| int index = getIndexByKey(key); | ||
|
|
||
| if (buckets[index] == null) { | ||
| buckets[index] = new Node<>(key, value); | ||
| size++; | ||
| return; | ||
| } | ||
|
|
||
| Node<K, V> current = buckets[index]; | ||
|
|
||
| if (current.key == null | ||
| ? key == null | ||
| : current.key.equals(key)) { | ||
| current.value = value; | ||
| return; | ||
| } | ||
|
|
||
| while (current.next != null) { | ||
| if (current.next.key == null | ||
| ? key == null | ||
| : current.next.key.equals(key)) { | ||
| current.next.value = value; | ||
| return; | ||
| } | ||
| current = current.next; | ||
| } | ||
|
|
||
| current.next = new Node<>(key, value); | ||
| size++; | ||
| } | ||
|
|
||
| @Override | ||
| public V getValue(K key) { | ||
| if (size == 0) { | ||
| return null; | ||
| } | ||
|
|
||
| int index = getIndexByKey(key); | ||
| Node<K, V> current = buckets[index]; | ||
|
|
||
| while (current != null) { | ||
| if (current.key == null | ||
| ? key == null | ||
| : current.key.equals(key)) { | ||
| return current.value; | ||
| } | ||
| current = current.next; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public int getSize() { | ||
| return 0; | ||
| return size; | ||
| } | ||
|
|
||
| private void addNewNode(Node<K, V> newNode) { | ||
| newNode.next = null; | ||
|
|
||
| int index = getIndexByKey(newNode.key); | ||
|
|
||
| if (buckets[index] == null) { | ||
| buckets[index] = newNode; | ||
| return; | ||
| } | ||
|
|
||
| Node<K, V> current = buckets[index]; | ||
|
|
||
| if (current.key == null | ||
| ? newNode.key == null | ||
| : current.key.equals(newNode.key)) { | ||
| current.value = newNode.value; | ||
| return; | ||
| } | ||
|
|
||
| while (current.next != null) { | ||
| if (current.next.key == null | ||
| ? newNode.key == null | ||
| : current.next.key.equals(newNode.key)) { | ||
| current.next.value = newNode.value; | ||
| return; | ||
| } | ||
| current = current.next; | ||
| } | ||
|
|
||
| current.next = newNode; | ||
| } | ||
|
|
||
| private void resize() { | ||
| currentMaxSize = currentMaxSize * sizeMultiplier; | ||
| threshold = (int) (currentMaxSize * loadFactor); | ||
|
|
||
| Node<K, V>[] oldBuckets = buckets; | ||
| buckets = new Node[currentMaxSize]; | ||
|
|
||
| for (Node<K, V> node : oldBuckets) { | ||
| while (node != null) { | ||
| Node<K, V> next = node.next; | ||
| node.next = null; | ||
| addNewNode(node); | ||
| node = next; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private int getIndexByKey(K key) { | ||
| if (key == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| int hash = key.hashCode(); | ||
| int hashMask = 0x7fffffff; | ||
|
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: "Any magic numbers should be constants" — the mask |
||
| return (hash & hashMask) % currentMaxSize; | ||
| } | ||
|
|
||
| private static class Node<K, V> { | ||
| private final K key; | ||
| private V value; | ||
| private Node<K, V> next; | ||
|
|
||
| Node(K key, V 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 #7: "Remember about access modifiers. All fields and methods should have access modifiers. Remember about the encapsulation principle." — the Node constructor |
||
| 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. Remove all redundant empty lines, be careful :)" — there is an empty line immediately before the class declaration. Remove the redundant blank line so the class does not begin with an empty line.