Skip to content

implement MyHashMap functionality#1654

Open
Sibertdom wants to merge 3 commits intomate-academy:masterfrom
Sibertdom:develop
Open

implement MyHashMap functionality#1654
Sibertdom wants to merge 3 commits intomate-academy:masterfrom
Sibertdom:develop

Conversation

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

This requires a few changes. Your MyHashMap implementation is well-structured, but there's a critical issue that can cause a runtime error.

  • The method for calculating the bucket index, Math.abs(key.hashCode()), can return a negative number if key.hashCode() is Integer.MIN_VALUE. This will lead to an ArrayIndexOutOfBoundsException. A safer way to ensure a non-negative index is to use a bitwise operation, like (key.hashCode() & 0x7fffffff) % table.length.

Additionally, please address these checklist violations:

  • The number 2 used for resizing is a magic number and should be a named constant. [CHECKLIST ITEM #2]
  • The constructor for the private inner Node class should not be public. [CHECKLIST ITEM #6]

Once you've addressed these 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 int getBucketIndex(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.

Math.abs(key.hashCode()) can return a negative number if key.hashCode() is equal to Integer.MIN_VALUE. This would result in a negative array index, causing an ArrayIndexOutOfBoundsException. A safer approach to ensure a non-negative result is to use a bitwise operation, such as (key.hashCode() & 0x7fffffff) % table.length.


private void resize() {
Node<K, V>[] oldTable = table;
table = 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 checklist item #2: 'Any magic numbers should be constants'. The value 2 is a magic number. It's better to extract it into a named constant, like RESIZE_FACTOR, to improve code clarity and maintainability.

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

public Node(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 #6: 'If the method has only a utility purpose and is used only inside the same class, it should not be public'. The Node class is private to MyHashMap, so its constructor should not be public. Using a more restrictive access modifier like private or package-private would better encapsulate the implementation details.

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

Excellent work on this iteration! I'm approving your solution as you have successfully addressed all the points from the previous review. You've fixed the critical hashCode bug, removed the magic number for resizing by creating a constant, and corrected the access modifier on the Node constructor. The code is now clean, robust, and fully meets all the project requirements. Keep up the great work!


✨ 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