implement MyHashMap functionality#1654
implement MyHashMap functionality#1654Sibertdom wants to merge 3 commits intomate-academy:masterfrom
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
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 ifkey.hashCode()isInteger.MIN_VALUE. This will lead to anArrayIndexOutOfBoundsException. 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
2used for resizing is a magic number and should be a named constant. [CHECKLIST ITEM #2] - The constructor for the private inner
Nodeclass should not bepublic. [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
| } | ||
|
|
||
| private int getBucketIndex(K key) { | ||
| return (key == null) ? 0 : Math.abs(key.hashCode() % table.length); |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
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! ✨
No description provided.