Skip to content

Conversation

IrynaF-e30
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Your implementation of MyHashMap has a solid foundation, and the core logic for put, getValue, and resize is well-structured. However, there are a few critical issues that prevent the code from compiling and running correctly. These need to be fixed before we can approve the submission.

Here are the blockers you'll need to address:

  1. Invalid Class Structure: The MyHashMap class is currently split across two files (MyHashMap.java and MyMap.java). In Java, a single class must be defined entirely within one .java file. Please consolidate all methods and fields of MyHashMap into the src/main/java/core/basesyntax/MyHashMap.java file.

  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.

  3. Missing Objects Import: Your code correctly uses Objects.equals() to handle null keys safely, which is great! However, the java.util.Objects class needs to be imported for the code to compile. Please add import java.util.Objects; at the top of your MyHashMap.java file.

  4. Bug in Hash Index Calculation: There's a subtle bug in how you calculate the bucket index. The expression Math.abs(key.hashCode()) can fail. If key.hashCode() returns Integer.MIN_VALUE, Math.abs() will also return Integer.MIN_VALUE (which is negative), leading to an ArrayIndexOutOfBoundsException.

    A safer way to ensure a non-negative hash is to use a bitwise AND mask. For example:

    int hash = key.hashCode();
    int index = (hash & 0x7fffffff) % table.length;

Once these blockers are resolved, your implementation will be in great shape! As a minor improvement for the future, consider defining the number 2 in your resize method as a named constant (e.g., RESIZE_MULTIPLIER) to avoid "magic numbers".

You're on the right track. Address these issues, and I'm confident you'll have a robust solution!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

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.


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


int getSize();
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;.

}

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

Choose a reason for hiding this comment

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

There's a subtle bug here with Math.abs(). If key.hashCode() returns Integer.MIN_VALUE, Math.abs(key.hashCode()) will also return Integer.MIN_VALUE, which is a negative number. This will cause an ArrayIndexOutOfBoundsException when used as an index. A safer way to ensure a non-negative hash code is to use a bitwise AND, for example: (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.

Comment on lines 61 to 63
K key;
V value;
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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

There are several critical blockers that must be addressed before your submission can be approved:

  1. File/Class Name Mismatch: The implementation of MyHashMap is in MyMap.java, but the file name and class name must match in Java. This will cause compilation errors. Please rename the file to MyHashMap.java or the class to MyMap so they match.

  2. Invalid Import in MyHashMap.java: The file MyHashMap.java contains import org.w3c.dom.Node;, which conflicts with your own inner Node class and will cause compilation errors. Remove this import statement.

  3. Missing Implementation in MyHashMap.java: The file MyHashMap.java is currently a stub and does not contain the required implementation. All required methods and logic must be present in this file.

  4. Explicit Access Modifiers: The fields key, value, and next in your inner Node class lack explicit access modifiers. Add private (or another appropriate modifier) to each field, as required by the checklist.

  5. Magic Number in Resize: The number 2 used for resizing (e.g., oldTable.length * 2) should be replaced with a named constant, such as RESIZE_MULTIPLIER, to improve readability and maintainability.

Next Steps:

  • Move or rename your implementation so that the file and class names match (MyHashMap.java and MyHashMap).
  • Remove the invalid import from MyHashMap.java.
  • Ensure all required methods and logic are present in MyHashMap.java.
  • Add explicit access modifiers to all fields in your inner Node class.
  • Replace magic numbers with named constants.

Once you address these issues, your implementation should be fully compliant. You're very close—just a few structural and checklist fixes needed! Keep up the good work.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

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


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


private static class Node<K, V> {
K key;
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.

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

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.

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

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

This is an educational approval to help you continue progressing in your learning journey. You’ve done a great job implementing the core functionality of your MyHashMap—your use of generics, constants, and method structure shows a solid understanding of Java and data structures!

What you did well:

  • All main methods (put, getValue, getSize) are present and work as expected.
  • You handled resizing, collision resolution, and type safety correctly.
  • Your code is clean, readable, and follows Java naming conventions.

What to improve:

  • According to the checklist, all private methods and classes should be declared after all public ones in your class file. Currently, your private methods (getIndex, resize) and the private static inner class Node are placed before the end of your public methods.
    • Why this matters: Keeping public methods at the top improves readability and makes it easier for others to see how to use your class.
    • How to fix: Move all private methods and the Node class to the end of your class file, after all public methods and constructors.

Keep up the great work! Paying attention to these small organizational details will help you write even more professional code in the future. You're very close to perfect—just a minor adjustment needed!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Comment on lines +66 to +83
private int getIndex(K key) {
return (key == null) ? 0 : Math.abs(key.hashCode() % table.length);
}

@SuppressWarnings("unchecked")
private void resize() {
Node<K, V>[] oldTable = table;
table = (Node<K, V>[]) new Node[oldTable.length * 2];
size = 0;
for (Node<K, V> node : oldTable) {
while (node != null) {
put(node.getKey(), node.getValue());
node = node.getNext();
}
}
}

private static class Node<K, V> {

Choose a reason for hiding this comment

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

This violates checklist item: 'Private methods and classes must be declared after public ones in the class file.'

Your private methods (getIndex, resize) and the private static inner class Node are declared before the end of the public methods. Please move all private methods and the private inner class to the end of the class file, after all public methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants