Skip to content

My own HashMap#1651

Open
obvly wants to merge 2 commits intomate-academy:masterfrom
obvly:master
Open

My own HashMap#1651
obvly wants to merge 2 commits intomate-academy:masterfrom
obvly:master

Conversation

@obvly
Copy link
Copy Markdown

@obvly obvly commented Jan 22, 2026

No description provided.

Copy link
Copy Markdown

@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 a solid attempt, but changes are needed due to a critical bug in the hashing logic that can cause a runtime error. Your current use of Math.abs(key.hashCode()) will fail if the key's hash code is Integer.MIN_VALUE, as Math.abs() will return a negative number, leading to an ArrayIndexOutOfBoundsException. A safer way to handle this is to use a bitwise operation to ensure the hash is always non-negative, for example: (key.hashCode() & 0x7fffffff) % table.length.

Additionally, please address two minor checklist violations:

  • The resizing multiplier 2 is a magic number and should be a named constant [CHECKLIST ITEM #2].
  • The Node constructor's public access modifier is too broad for an inner class used only within MyHashMap [CHECKLIST ITEM #6].

Once you fix the hashing bug and these minor points, your solution will be in great shape!


✨ 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


private void resize() {
Node<K, V>[] oldTable = table;
table = (Node<K, V>[]) new Node[oldTable.length * 2];
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 resizing factor 2 should be extracted into a named constant to improve readability and maintainability, for example, private static final int RESIZE_MULTIPLIER = 2;.

return 0;
}
int hash = key.hashCode();
return Math.abs(hash) % 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 approach to handling negative hash codes has a subtle bug. Math.abs(Integer.MIN_VALUE) returns a negative value, which will cause the modulo operation to produce a negative index, leading to an ArrayIndexOutOfBoundsException. A safer way to ensure a non-negative result is to use a bitwise AND operation, like (hash & 0x7fffffff) % table.length.

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

public 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 #6: 'If the method has only a utility purpose and is used only inside the same class, it should not be public'. Since the Node class is private and is only instantiated within MyHashMap, this constructor does not need public access. It can have private or default (package-private) access.

Copy link
Copy Markdown

@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

Excellent work on this iteration! I'm happy to approve your solution. You've successfully addressed all the feedback from the previous review, especially the critical hashing bug for Integer.MIN_VALUE. Replacing the magic numbers with named constants like GROW_FACTOR has also significantly improved readability. Your MyHashMap implementation is now robust, correct, and meets all the requirements. Well done!


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

Footnotes

  1. Rate AI review example

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