-
Notifications
You must be signed in to change notification settings - Fork 1.5k
my HashMap #1590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
my HashMap #1590
Conversation
There was a problem hiding this 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:
-
Invalid Class Structure: The
MyHashMap
class is currently split across two files (MyHashMap.java
andMyMap.java
). In Java, a single class must be defined entirely within one.java
file. Please consolidate all methods and fields ofMyHashMap
into thesrc/main/java/core/basesyntax/MyHashMap.java
file. -
Incorrect
Node
Import: InMyHashMap.java
, you have an importimport org.w3c.dom.Node;
. This conflicts with your own innerNode
class and will cause a compilation error. Since you are using your ownNode
class, this import should be removed. -
Missing
Objects
Import: Your code correctly usesObjects.equals()
to handlenull
keys safely, which is great! However, thejava.util.Objects
class needs to be imported for the code to compile. Please addimport java.util.Objects;
at the top of yourMyHashMap.java
file. -
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. Ifkey.hashCode()
returnsInteger.MIN_VALUE
,Math.abs()
will also returnInteger.MIN_VALUE
(which is negative), leading to anArrayIndexOutOfBoundsException
.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
package core.basesyntax; | ||
|
||
public class MyHashMap<K, V> implements MyMap<K, V> { | ||
import org.w3c.dom.Node; |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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.
K key; | ||
V value; | ||
Node<K, V> next; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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:
-
File/Class Name Mismatch: The implementation of
MyHashMap
is inMyMap.java
, but the file name and class name must match in Java. This will cause compilation errors. Please rename the file toMyHashMap.java
or the class toMyMap
so they match. -
Invalid Import in
MyHashMap.java
: The fileMyHashMap.java
containsimport org.w3c.dom.Node;
, which conflicts with your own innerNode
class and will cause compilation errors. Remove this import statement. -
Missing Implementation in
MyHashMap.java
: The fileMyHashMap.java
is currently a stub and does not contain the required implementation. All required methods and logic must be present in this file. -
Explicit Access Modifiers: The fields
key
,value
, andnext
in your innerNode
class lack explicit access modifiers. Addprivate
(or another appropriate modifier) to each field, as required by the checklist. -
Magic Number in Resize: The number
2
used for resizing (e.g.,oldTable.length * 2
) should be replaced with a named constant, such asRESIZE_MULTIPLIER
, to improve readability and maintainability.
Next Steps:
- Move or rename your implementation so that the file and class names match (
MyHashMap.java
andMyHashMap
). - 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
package core.basesyntax; | ||
|
||
public class MyHashMap<K, V> implements MyMap<K, V> { | ||
import org.w3c.dom.Node; |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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
.
There was a problem hiding this 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 allpublic
ones in your class file. Currently, yourprivate
methods (getIndex
,resize
) and the private static inner classNode
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 theNode
class to the end of your class file, after allpublic
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
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> { |
There was a problem hiding this comment.
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.
No description provided.