Skip to content
Open
Show file tree
Hide file tree
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
22 changes: 8 additions & 14 deletions src/main/java/core/basesyntax/MyHashMap.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package core.basesyntax;

public class MyHashMap<K, V> implements MyMap<K, V> {
import org.w3c.dom.Node;

Choose a reason for hiding this comment

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

This import statement is incorrect. The MyHashMap class uses its own inner Node class for its entries. Importing org.w3c.dom.Node introduces a name collision, as it's a class from the XML DOM API, and will cause your code to fail compilation. Please remove this line.

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: 'Incorrect Node Import: In MyHashMap.java, you have an import import org.w3c.dom.Node;. This conflicts with your own inner Node class and will cause a compilation error. Since you are using your own Node class, this import should be removed.'


@Override
public void put(K key, V value) {
public class MyHashMap<K, V> {

Choose a reason for hiding this comment

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

There is a redundant empty line before this class declaration. The project's checklist advises against starting a class definition with an empty line. Please remove the blank line for better code style consistency.

private static final int DEFAULT_CAPACITY = 16;
private static final float LOAD_FACTOR = 0.75f;

public MyHashMap() {
Node[] table = new Node[DEFAULT_CAPACITY];
int size = 0;
}
}

@Override
public V getValue(K key) {
return null;
}

@Override
public int getSize() {
return 0;
}
}
82 changes: 78 additions & 4 deletions src/main/java/core/basesyntax/MyMap.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,83 @@
package core.basesyntax;
import java.util.Objects;

public interface MyMap<K, V> {
void put(K key, V value);
public class MyHashMap<K, V> {

Choose a reason for hiding this comment

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

This violates the requirement that the file name and class name must match. The file is named MyMap.java but the class is MyHashMap. Rename the file to MyHashMap.java or the class to MyMap to match.

private static final int DEFAULT_CAPACITY = 16;
private static final float LOAD_FACTOR = 0.75f;

V getValue(K key);
private Node<K, V>[] table;
private int size;

int getSize();
@SuppressWarnings("unchecked")
public MyHashMap() {
table = (Node<K, V>[]) new Node[DEFAULT_CAPACITY];
size = 0;
}

public void put(K key, V value) {
if ((float) size / table.length >= LOAD_FACTOR) {
resize();
}
int index = getIndex(key);
Node<K, V> current = table[index];

while (current != null) {
if (Objects.equals(current.key, key)) {

Choose a reason for hiding this comment

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

Using Objects.equals is a good practice as it correctly handles null keys. However, the java.util.Objects class must be imported at the top of the file for this code to compile. Please add import java.util.Objects;.

current.value = value; // оновлюємо існуюче значення
return;
}
current = current.next;
}

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

public V getValue(K key) {
int index = getIndex(key);
Node<K, V> current = table[index];

while (current != null) {
if (Objects.equals(current.key, key)) {
return current.value;
}
current = current.next;
}

return null;
}

public int getSize() {
return size;
}

private int getIndex(K key) {
return (key == null) ? 0 : (key.hashCode() & 0x7fffffff) % table.length;
}

@SuppressWarnings("unchecked")
private void resize() {
Node<K, V>[] oldTable = table;
table = (Node<K, V>[]) new Node[oldTable.length * 2];

Choose a reason for hiding this comment

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

The number 2 here is a magic number. It's better to declare it as a named constant at the top of the class, for example: private static final int RESIZE_MULTIPLIER = 2;. This improves code readability and makes it easier to change the resize behavior in the future.

Choose a reason for hiding this comment

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

This violates the checklist requirement: 'All magic numbers must be replaced with named constants.' The number 2 in oldTable.length * 2 should be replaced with a named constant, such as RESIZE_MULTIPLIER.

size = 0; // заново перерахуємо при rehash

for (Node<K, V> node : oldTable) {
while (node != null) {
put(node.key, node.value);
node = node.next;
}
}
}

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

Choose a reason for hiding this comment

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

This violates checklist item: 'All fields and methods must have explicit access modifiers.' The fields key, value, and next in the Node class lack access modifiers. Add private or another appropriate modifier.

V value;

Choose a reason for hiding this comment

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

This violates checklist item: 'All fields and methods must have explicit access modifiers.' The fields key, value, and next in the Node class lack access modifiers. Add private or another appropriate modifier.

Node<K, V> next;

Choose a reason for hiding this comment

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

According to the encapsulation principle and the project checklist, all fields should have an access modifier. It is good practice to make the fields of an inner class private to restrict access to the enclosing class only. The key field can also be made final as it should not change after a node is created.

Choose a reason for hiding this comment

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

This violates checklist item: 'All fields and methods must have explicit access modifiers.' The fields key, value, and next in the Node class lack access modifiers. Add private or another appropriate modifier.


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