Skip to content
Open
Changes from 2 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
69 changes: 68 additions & 1 deletion src/main/java/core/basesyntax/MyHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,85 @@

public class MyHashMap<K, V> implements MyMap<K, V> {

private static final int DEFAULT_INITIAL_CAPACITY = 16;

Copy link
Copy Markdown

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 at the start of the class body before the field declarations — remove it so the class/method implementations don't begin with a blank line.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates the checklist item exactly: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)" — there is an empty line immediately after the class declaration; remove that leading blank line so the class body starts without an empty line.

private static final double LOAD_FACTOR = 0.75;

private Node<K, V>[] table = new Node[DEFAULT_INITIAL_CAPACITY];
private int size;

private static class Node<K, V> {
private final K key;
private V value;
private Node<K, V> next;

public Node(K key, V value, Node<K, V> next) {
this.key = key;
this.value = value;
this.next = next;
}
}

private int getIndex(K key, int length) {
return (key == null) ? 0 : Math.abs(key.hashCode() % length);
}

private boolean keysAreEqual(K key1, K key2) {
if (key1 == key2) {
return true;
}
return key1 != null && key1.equals(key2);
}

private void resize() {

Copy link
Copy Markdown

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." The private inner Node class (line 11) and the private helper methods getIndex (line 23), keysAreEqual (line 27) and resize (line 34) are declared before the public API methods (put, getValue, getSize). Move private members below the public methods to follow the checklist guidance.

Node<K,V>[] oldTable = table;
int newCapacity = table.length * 2;
table = new Node[newCapacity];
size = 0;

for (Node<K, V> currentNode : oldTable) {
while (currentNode != null) {
int newIndex = getIndex(currentNode.key, newCapacity);
table[newIndex] = new Node<>(currentNode.key, currentNode.value, table[newIndex]);
size++;
currentNode = currentNode.next;
}
}
}

@Override
public void put(K key, V value) {
int index = getIndex(key, table.length);

Node<K, V> currentNode = table[index];
while (currentNode != null) {
if (keysAreEqual(currentNode.key, key)) {
currentNode.value = value;
return;
}
currentNode = currentNode.next;
}
table[index] = new Node<>(key, value, table[index]);
size++;
if (size >= table.length * LOAD_FACTOR) {
resize();
}
}

@Override
public V getValue(K key) {
int index = getIndex(key, table.length);
Node<K, V> currentNode = table[index];
while (currentNode != null) {
if (keysAreEqual(currentNode.key, key)) {
return currentNode.value;
}
currentNode = currentNode.next;
}
return null;
}

@Override
public int getSize() {
return 0;
return size;
}
}
Loading