First solution by Viodon#1914
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Your implementation has a critical bug in the put() method that completely breaks core functionality. The empty slot detection on line 14 checks keys[i] == null && values[i] == null, but after inserting a key, values[i] becomes non-null, so subsequent put() calls will never find empty slots—only one element can ever be stored. You need to change this condition to only check keys[i] == null. Additionally, there are checklist violations that must be fixed: fields keys and values should be initialized in the constructor, not inline at field declaration [CHECKLIST ITEM #2]. The put() method also contains duplicate code patterns where two identical values[i] = value; return; blocks appear for different conditions [CHECKLIST ITEM #5]. Finally, remove the empty line before the @Override annotation [CHECKLIST ITEM #1]. The reference equality check keys[i] == key on line 18 is redundant since keys[i].equals(key) already handles all cases including null keys. Once you fix the critical bug, the storage should be able to hold up to 10 elements as required.
✨ 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
| import core.basesyntax.Storage; | ||
|
|
||
| public class StorageImpl<K, V> implements Storage<K, V> { | ||
|
|
There was a problem hiding this comment.
Violates checklist item #1: Remove all redundant empty lines. Do not begin class or method implementation with empty line.
| public class StorageImpl<K, V> implements Storage<K, V> { | ||
|
|
||
| public static final int MAX_SIZE = 10; | ||
| private Object[] keys = new Object[MAX_SIZE]; |
There was a problem hiding this comment.
Violates checklist item #2: All non-constant fields should be initialized in the constructor, not inline.
|
|
||
| public static final int MAX_SIZE = 10; | ||
| private Object[] keys = new Object[MAX_SIZE]; | ||
| private Object[] values = new Object[MAX_SIZE]; |
There was a problem hiding this comment.
Violates checklist item #2: All non-constant fields should be initialized in the constructor, not inline.
| } | ||
| if (keys[i] == key) { |
There was a problem hiding this comment.
Violates checklist item #5 (DRY): This condition duplicates the logic from lines 14 and 18. The reference check keys[i] == key is redundant when keys[i].equals(key) already handles all cases including null.
| } | ||
| if (keys[i] == null && values[i] == null) { |
There was a problem hiding this comment.
CRITICAL BUG: This condition is wrong. It checks keys[i] == null && values[i] == null, but after inserting a key-value pair, values[i] is set to a non-null value. On subsequent insertions, this condition will never be true, preventing new entries from being stored. Should only check if the key slot is empty: keys[i] == null.
|
|
||
| @Override | ||
| public V get(K key) { | ||
|
|
There was a problem hiding this comment.
Violates checklist item #1: Remove empty line before method implementation.
| } | ||
| if (keys[i] == null && key == null) { |
There was a problem hiding this comment.
Violates checklist item #5 (DRY): Duplicate logic from line 14. The condition keys[i] != null && keys[i].equals(key) already handles the case where both are non-null.
| public int size() { | ||
| return -1; | ||
| int size = 0; | ||
|
|
There was a problem hiding this comment.
Violates checklist item #1: Remove empty line before method implementation.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
The implementation still has a critical issue that must be addressed. The put() method uses values[i] == null for empty slot detection, but the task requirement explicitly specifies it should check keys[i] == null. This is a functional bug - after inserting a value into a slot, that value becomes non-null, which means the method won't be able to find empty slots for subsequent insertions, breaking the core storage operation.
Critical fix needed:
// Change from:
if (values[i] == null) { ... }
// To:
if (keys[i] == null) { ... }Medium priority: Remove the redundant keys[i] == key checks in both put() and get() methods. The keys[i].equals(key) condition already handles all cases including null key comparisons, so these duplicate reference checks are unnecessary.
Once you fix the empty slot detection condition, the implementation should be functional. The constructor initialization and general structure look correct.
✨ 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
| return; | ||
| } | ||
|
|
||
| if (values[i] == null) { |
There was a problem hiding this comment.
The empty slot detection checks values[i] == null, but the task requirement specifies it should check only keys[i] == null. Change to maintain consistency with the stated requirement.
There was a problem hiding this comment.
Шукає вільний слот для значення коли ключ не збігається з жодним іншим, перевіряємо values[і], перевіряємо values[i], тому що може бути значення з ключем null, @mateacademy-ai-mentor
| @Override | ||
| public void put(K key, V value) { | ||
| for (int i = 0; i < keys.length; i++) { | ||
| if (keys[i] != null && keys[i].equals(key) || keys[i] == key) { |
There was a problem hiding this comment.
The reference equality check keys[i] == key is redundant. The condition keys[i].equals(key) already handles all cases including when both are null, so this OR clause can be removed.
There was a problem hiding this comment.
@mateacademy-ai-mentor строка потрібна, ввона не обробляє варіанти коли ключ = null, без неї код працює не коректно
| } | ||
| if (keys[i] == key) { | ||
| return (V) values[i]; |
There was a problem hiding this comment.
This keys[i] == key check is redundant - the first condition on line 34 already handles null keys and equal keys via keys[i].equals(key). This duplicate check should be removed.
There was a problem hiding this comment.
@mateacademy-ai-mentor ця стрічка потрібна, вона перевіряє ключ null, в рядку 34 значення порівнюються лише якщо keys[i] != null, щоб уникнути помилки NullPointerExceptions
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Educational Override: This solution demonstrates solid understanding of array-based storage with proper use of generics and the MAX_SIZE constant. However, there are two critical bugs that must be addressed for correct functionality: 1) In put(), the empty slot check uses values[i] == null instead of keys[i] == null — this breaks when storing null values since they'll be treated as empty slots; 2) The keys[i] == key reference checks in both put() and get() are redundant since equals() already handles null safely. A minor DRY improvement would be extracting a findKeyIndex() helper method, but this is optional. You're on the right track — fixing the critical bugs will make this implementation fully correct!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| /*Шукає вільний слот для значення коли ключ не збігається з жодним іншим, | ||
| перевіряємо values[i] | ||
| перевіряємо values[i], тому що може бути значення з ключем null*/ | ||
| if (values[i] == null) { |
There was a problem hiding this comment.
CRITICAL BUG: Empty slot detection checks values[i] == null instead of keys[i] == null. This breaks when storing null values—they'll be treated as empty slots and overwritten. Change to if (keys[i] == null) to properly detect unused positions.
| перевіряємо values[i] | ||
| перевіряємо values[i], тому що може бути значення з ключем null*/ | ||
| if (values[i] == null) { | ||
| values[i] = value; |
There was a problem hiding this comment.
CRITICAL BUG: After fixing the empty slot condition, this line should set keys[i] = key first, then values[i] = value. Currently if the fix isn't applied, this assigns value before the key.
| @Override | ||
| public void put(K key, V value) { | ||
| for (int i = 0; i < keys.length; i++) { | ||
| if (keys[i] != null && keys[i].equals(key) || keys[i] == key) { |
There was a problem hiding this comment.
Redundant check: The || keys[i] == key part is unnecessary. keys[i].equals(key) handles null keys correctly (returns false when keys[i] is null), so the reference equality check for null is already covered. Keep only the keys[i] != null && keys[i].equals(key) condition.
| if (keys[i] != null && keys[i].equals(key)) { | ||
| return (V) values[i]; | ||
| } | ||
| if (keys[i] == key) { |
There was a problem hiding this comment.
Redundant check: Same issue as in put() - keys[i].equals(key) already handles null key comparison. Remove the separate keys[i] == key check.
| for (int i = 0; i < keys.length; i++) { | ||
| if (keys[i] != null && keys[i].equals(key)) { | ||
| return (V) values[i]; | ||
| } | ||
| if (keys[i] == key) { | ||
| return (V) values[i]; |
There was a problem hiding this comment.
DRY violation: The return (V) values[i] logic is duplicated in get() (lines 37 and 40). Consider extracting the index-finding logic to a private method findKeyIndex(K key) that returns the index or -1, then use it in both get() and put().
| /*Шукає вільний слот для значення коли ключ не збігається з жодним іншим, | ||
| перевіряємо values[i] | ||
| перевіряємо values[i], тому що може бути значення з ключем null*/ |
There was a problem hiding this comment.
DRY violation: The comment suggests the code is explanatory but also indicates repeated logic exists. Consider a private helper method to avoid duplication between put() and get() methods.
No description provided.