Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 93 additions & 1 deletion src/main/java/core/basesyntax/MyHashMap.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,111 @@
package core.basesyntax;

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: .

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;

Choose a reason for hiding this comment

The 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;

Choose a reason for hiding this comment

The 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();
}

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

The 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;
}

Choose a reason for hiding this comment

The 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;
}

Choose a reason for hiding this comment

The 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;
}

Choose a reason for hiding this comment

The 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];

Choose a reason for hiding this comment

The 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;
}

Choose a reason for hiding this comment

The 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];

Choose a reason for hiding this comment

The 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;
}
}
}