-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(DEVELOP): create hashMap #1598
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?
Changes from all 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,111 @@ | ||
package core.basesyntax; | ||
|
||
public class MyHashMap<K, V> implements MyMap<K, V> { | ||
private static final int DEFAULT_CAPACITY = 16; | ||
private static final float LOAD_FACTOR = 0.75f; | ||
private static final int RESIZE_MULTIPLIER = 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. Redundant empty line. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Please remove the empty line at the top of the class to match the checklist. |
||
private int threshold; | ||
private int size; | ||
private Node<K, V>[] table; | ||
|
||
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. Redundant empty line. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Please remove this extra blank line between fields. |
||
public MyHashMap() { | ||
this.size = 0; | ||
this.threshold = (int) (DEFAULT_CAPACITY * LOAD_FACTOR); | ||
this.table = new Node[DEFAULT_CAPACITY]; | ||
} | ||
|
||
@Override | ||
public void put(K key, V value) { | ||
if (size >= threshold) { | ||
resize(); | ||
} | ||
|
||
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. Redundant empty line before local logic. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Delete the unnecessary blank line. |
||
int index = getIndex(key, table.length); | ||
Node<K, V> newNode = new Node<>(key, 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. Redundant empty line. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Remove it to keep the file compact. |
||
if (table[index] == null) { | ||
table[index] = newNode; | ||
size++; | ||
return; | ||
} | ||
|
||
Node<K, V> currentNode = table[index]; | ||
while (true) { | ||
if ((currentNode.key == null && key == null) | ||
|| (currentNode.key != null && currentNode.key.equals(key))) { | ||
currentNode.value = value; | ||
return; | ||
} | ||
|
||
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. Redundant empty line inside put loop area. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Please remove. |
||
if (currentNode.next == null) { | ||
currentNode.next = newNode; | ||
size++; | ||
return; | ||
} | ||
|
||
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. Redundant empty line. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Remove this blank line. |
||
currentNode = currentNode.next; | ||
} | ||
} | ||
|
||
@Override | ||
public V getValue(K key) { | ||
if (table == null) { | ||
return null; | ||
} | ||
|
||
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. Redundant empty line before index calculation in getValue. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Please remove. |
||
int index = getIndex(key, table.length); | ||
Node<K, V> currentNode = 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. Redundant empty line inside getValue. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Remove the extra blank line for consistency. |
||
while (currentNode != null) { | ||
if ((currentNode.key == null && key == null) | ||
|| (currentNode.key != null && currentNode.key.equals(key))) { | ||
return currentNode.value; | ||
} | ||
currentNode = currentNode.next; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
return 0; | ||
return size; | ||
} | ||
|
||
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. Redundant empty line before the resize method. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Please remove. |
||
private void resize() { | ||
int newCapacity = table.length * RESIZE_MULTIPLIER; | ||
Node<K, V>[] newTable = new Node[newCapacity]; | ||
|
||
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. Redundant empty line inside resize logic. This violates the checklist item: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)". Remove it to satisfy the checklist. |
||
for (Node<K, V> node : table) { | ||
while (node != null) { | ||
Node<K, V> nextNode = node.next; | ||
int newIndex = getIndex(node.key, newCapacity); | ||
|
||
node.next = newTable[newIndex]; | ||
newTable[newIndex] = node; | ||
node = nextNode; | ||
} | ||
} | ||
|
||
table = newTable; | ||
threshold = (int) (newCapacity * LOAD_FACTOR); | ||
} | ||
|
||
private int getIndex(K key, int capacity) { | ||
if (key == null) { | ||
return 0; | ||
} | ||
return (key.hashCode() & 0x7fffffff) % capacity; | ||
} | ||
|
||
private static class Node<K, V> { | ||
private final K key; | ||
private V value; | ||
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: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)" There is a leading redundant empty line at the top of the file (line 2) and several redundant blank lines inside the class. Remove leading and unnecessary blank lines at method/class starts to comply with the style checklist. See checklist guidance: .