first solution#1686
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Your MyHashMap implementation is functionally correct, which is a great start! However, changes are needed to align your code with the project's specific requirements. Please address the following points:
- The
Nodeinner class currently has getters and setters, which violates a specific project requirement. You should remove these methods and access the node's fields directly from the outer class. [CHECKLIST ITEM #3] - The value
0x7fffffffis used as a magic number. Please extract it into aprivate static finalconstant with a descriptive name. [CHECKLIST ITEM #2] - The private
Nodeclass and other private helper methods should be moved to the bottom of theMyHashMapclass, after all public methods. TheNodeclass should also be declaredprivate. [CHECKLIST ITEM #5]
Once these adjustments are made, 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
| public class MyHashMap<K, V> implements MyMap<K, V> { | ||
| private static final int DEFAULT_INITIAL_CAPACITY = 16; | ||
| private static final float DEFAULT_LOAD_FACTOR = 0.75f; | ||
| static class Node<K, V> implements Map.Entry<K, V> { |
There was a problem hiding this comment.
This violates checklist item #5: 'Private methods and classes should be after public ones in your class.' and #7: 'Remember about access modifiers.' The Node class should be declared private since it is only used within MyHashMap. It should also be moved to the bottom of the MyHashMap class, after all public methods.
| public K getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| @Override | ||
| public V getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public V setValue(V value) { | ||
| V oldValue = this.value; | ||
| this.value = value; | ||
| return oldValue; | ||
| } |
There was a problem hiding this comment.
This violates checklist item #3: 'Don't use getters and setters in class Node'. While implementing Map.Entry requires these methods, the task requirements do not require you to implement this interface. Please remove implements Map.Entry<K, V> from the Node class definition and delete these getter/setter methods to comply with the checklist.
| threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); | ||
| } | ||
|
|
||
| private int hash(K key) { |
There was a problem hiding this comment.
This violates checklist item #5: 'Private methods and classes should be after public ones in your class.' Private helper methods should be placed after all public methods in the class.
| resize(); | ||
| } | ||
| int hash = hash(key); | ||
| int index = (hash & 0x7fffffff) % table.length; |
There was a problem hiding this comment.
This violates checklist item #2: 'Any magic numbers should be constants'. The value 0x7fffffff is a magic number used to ensure the hash code is positive. It appears in multiple places and should be extracted into a private static final constant with a descriptive name.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Great job on the core logic of your MyHashMap! However, this submission requires changes because several key requirements from the project checklist have not been met. These were also mentioned in the previous review.
Please address the following:
- Magic Number: The value
0x7fffffffis used directly in your code. Please extract it into aprivate static finalconstant with a descriptive name. [CHECKLIST ITEM #2] NodeClass Implementation: TheNodeclass should not have getters or setters. To fix this, removeimplements Map.Entry<K, V>and thegetKey(),getValue(), andsetValue()methods. [CHECKLIST ITEM #3]- Class Structure: All private members, including the
Nodeclass and any private helper methods, must be placed after all public methods in the class file. [CHECKLIST ITEM #5]
Once these checklist items are addressed, 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 static final int DEFAULT_INITIAL_CAPACITY = 16; | ||
| private static final float DEFAULT_LOAD_FACTOR = 0.75f; | ||
|
|
||
| static class Node<K, V> implements Map.Entry<K, V> { |
There was a problem hiding this comment.
This Node class should be declared private and moved to the end of the MyHashMap class, after all public methods. Additionally, per checklist item #3, it should not contain getters and setters. Implementing Map.Entry forces you to add these methods, but the project requirements take precedence. Please remove the implements Map.Entry<K, V> part and the corresponding getKey, getValue, and setValue methods.
| threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); | ||
| } | ||
|
|
||
| private int hash(K key) { |
There was a problem hiding this comment.
This violates checklist item #5: 'Private methods and classes should be after public ones in your class.' Please move this private helper method to a position after all public methods like put, getValue, and getSize.
| resize(); | ||
| } | ||
| int hash = hash(key); | ||
| int index = (hash & 0x7fffffff) % table.length; |
There was a problem hiding this comment.
This violates checklist item #2: 'Any magic numbers should be constants'. The value 0x7fffffff is a magic number and should be extracted into a private static final constant with a descriptive name. This value is used in multiple places (getValue and resize), and using a constant will improve readability and maintainability.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am approving this submission as an educational override to help you progress. Great work on refactoring the Node class—your core logic is looking very strong!
For your next submission, please focus on a couple of key style guide rules:
- Class Structure: All private members, including the inner
Nodeclass and helper methods likehash(), should be placed at the end of the file, after all public methods. [CHECKLIST ITEM #5] - Magic Numbers: Values like
Integer.MAX_VALUEshould be extracted intoprivate static finalconstants with descriptive names to make their purpose clear. [CHECKLIST ITEM #2]
Also, be sure to remove any unused imports, as one is causing the build to fail. Addressing these points will make your excellent logic even more professional. Keep up the great 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
| private static final float DEFAULT_LOAD_FACTOR = 0.75f; | ||
|
|
||
| private class Node<K, V> { | ||
| private final int hash; |
There was a problem hiding this comment.
This violates checklist item #5: 'Private methods and classes should be after public ones in your class.' Please move the private Node inner class and the private hash() method to the end of the MyHashMap class, after all public methods.
| } | ||
| int hash = hash(key); | ||
| int index = (hash & Integer.MAX_VALUE) % table.length; | ||
| Node<K, V> current = table[index]; |
There was a problem hiding this comment.
This violates checklist item #2: 'Any magic numbers should be constants'. While using Integer.MAX_VALUE is better than a raw hex value, the requirement is to create your own private static final constant with a descriptive name (e.g., POSITIVE_INTEGER_MASK) to make the intent of the bitwise operation clear.
slade13
left a comment
There was a problem hiding this comment.
Your code does not build: https://github.com/mate-academy/jv-hash-map/actions/runs/24158894003/job/70504942928?pr=1686
Error: /home/runner/work/jv-hash-map/jv-hash-map/src/main/java/core/basesyntax/MyHashMap.java:89:5: Instance variable definition in wrong order. [DeclarationOrder]
Error: /home/runner/work/jv-hash-map/jv-hash-map/src/main/java/core/basesyntax/MyHashMap.java:90:5: Instance variable definition in wrong order. [DeclarationOrder]
Error: /home/runner/work/jv-hash-map/jv-hash-map/src/main/java/core/basesyntax/MyHashMap.java:91:5: Instance variable definition in wrong order. [DeclarationOrder]
Please fix it and make sure that it builds successfully with command mvn clean verify before sending it for review.
mateuszwojtkowiak
left a comment
There was a problem hiding this comment.
Your project did not build with Maven correctly. Please run mvn clean package and fix all errors.
slade13
left a comment
There was a problem hiding this comment.
Good work, please review my comments and add necessary changes. Besides these small problems your code looks good and follows the task requirements.
| return key == null ? 0 : key.hashCode(); | ||
| } | ||
|
|
||
| private class Node<K, V> { |
There was a problem hiding this comment.
Node should be static:
| private class Node<K, V> { | |
| private static class Node<K, V> { |
Since Node does not need access to the outer MyHashMap instance, making it static avoids storing an unnecessary reference to the outer object.
| private V value; | ||
| private Node<K, V> next; | ||
|
|
||
| public Node(int hash, K key, V value, Node<K, V> next) { |
There was a problem hiding this comment.
Since Node is used only inside MyHashMap, the constructor should also be restricted:
| public Node(int hash, K key, V value, Node<K, V> next) { | |
| private Node(int hash, K key, V value, Node<K, V> next) { |
| resize(); | ||
| } | ||
| int hash = hash(key); | ||
| int index = (hash & HASH_MASK) % table.length; |
There was a problem hiding this comment.
Index calculation is duplicated, this appears in more than one place:
int index = (hash & HASH_MASK) % table.length;
It would be cleaner to extract it into a private helper method, for example:
private int getIndex(int hash, int length)
No description provided.