Skip to content
Open

done #1698

Changes from 4 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
131 changes: 130 additions & 1 deletion src/main/java/core/basesyntax/MyHashMap.java
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> {

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 immediately before the class declaration. Remove the redundant blank line so the class does not begin with an empty line.

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() {

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

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: "Any magic numbers should be constants" — the mask 0x7fffffff is used as a local magic number (line uses int hashMask = 0x7fffffff;). Extract it to a named constant (for example private static final int HASH_MASK = 0x7fffffff;) and use that constant in getIndexByKey(...).

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) {

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 #7: "Remember about access modifiers. All fields and methods should have access modifiers. Remember about the encapsulation principle." — the Node constructor Node(K key, V value) has no explicit access modifier. Add an access modifier (for example private Node(...)) to follow the encapsulation requirement.

this.key = key;
this.value = value;
}
}
}
Loading