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
96 changes: 95 additions & 1 deletion src/main/java/core/basesyntax/MyHashMap.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,113 @@
package core.basesyntax;

import java.util.Map;
import java.util.Objects;

public class MyHashMap<K, V> implements MyMap<K, V> {
private static final int DEFAULT_INITIAL_CAPACITY = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;

static class Node<K, V> implements Map.Entry<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 #5: 'Private methods and classes should be after public ones in your class.' and #7: 'Remember about access modifiers.' The Node class should be declared private since it is only used within MyHashMap. It should also be moved to the bottom of the MyHashMap class, after all public methods.

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 Node class should be declared private and moved to the end of the MyHashMap class, after all public methods. Additionally, per checklist item #3, it should not contain getters and setters. Implementing Map.Entry forces you to add these methods, but the project requirements take precedence. Please remove the implements Map.Entry<K, V> part and the corresponding getKey, getValue, and setValue methods.

private final int hash;

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.' Please move the private Node inner class and the private hash() method to the end of the MyHashMap class, after all public methods.

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

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

@Override
public K getKey() {
return key;
}

@Override
public V getValue() {
return value;
}

@Override
public V setValue(V value) {
V oldValue = this.value;
this.value = value;
return oldValue;
}

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 #3: 'Don't use getters and setters in class Node'. While implementing Map.Entry requires these methods, the task requirements do not require you to implement this interface. Please remove implements Map.Entry<K, V> from the Node class definition and delete these getter/setter methods to comply with the checklist.

}

private Node<K, V>[] table;
private int size;
private int threshold;

public MyHashMap() {
table = new Node[DEFAULT_INITIAL_CAPACITY];
threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
}

private int hash(K key) {

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.' Private helper methods should be placed after all public methods in the class.

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.' Please move this private helper method to a position after all public methods like put, getValue, and getSize.

return key == null ? 0 : key.hashCode();
}

@Override
public void put(K key, V value) {
if (size >= threshold) {
resize();
}
int hash = hash(key);
int index = (hash & 0x7fffffff) % table.length;

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 #2: 'Any magic numbers should be constants'. The value 0x7fffffff is a magic number used to ensure the hash code is positive. It appears in multiple places and should be extracted into a private static final constant with a descriptive name.

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 #2: 'Any magic numbers should be constants'. The value 0x7fffffff is a magic number and should be extracted into a private static final constant with a descriptive name. This value is used in multiple places (getValue and resize), and using a constant will improve readability and maintainability.

Node<K, V> current = table[index];

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 #2: 'Any magic numbers should be constants'. While using Integer.MAX_VALUE is better than a raw hex value, the requirement is to create your own private static final constant with a descriptive name (e.g., POSITIVE_INTEGER_MASK) to make the intent of the bitwise operation clear.

while (current != null) {
// Porównujemy hash oraz klucze (Objects.equals radzi sobie z null)
if (current.hash == hash && Objects.equals(current.key, key)) {
current.value = value;
return;
}
current = current.next;
}

Node<K, V> newNode = new Node<>(hash, key, value, table[index]);
table[index] = newNode;
size++;
}

@Override
public V getValue(K key) {
int hash = hash(key);
int index = (hash & 0x7fffffff) % table.length;
Node<K, V> current = table[index];
while (current != null) {
if (current.hash == hash && Objects.equals(current.key, key)) {
return current.value;
}
current = current.next;
}
return null;
}

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

private void resize() {
int newCapacity = table.length * 2;
Node<K, V>[] newTable = new Node[newCapacity];
for (Node<K, V> node : table) {
while (node != null) {
Node<K, V> next = node.next;
int index = (node.hash & 0x7fffffff) % newCapacity;
node.next = newTable[index];
newTable[index] = node;
node = next;
}
}
table = newTable;
threshold = (int) (newCapacity * DEFAULT_LOAD_FACTOR);
}
}



Loading