-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: implement custom HashMap storage #1908
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,17 +3,55 @@ | |
| import core.basesyntax.Storage; | ||
|
|
||
| public class StorageImpl<K, V> implements Storage<K, V> { | ||
| private static final int MAX_SIZE = 10; | ||
| private int size; | ||
| private K[] keys; | ||
| private V[] values; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line at the beginning of the constructor implementation. This is before the constructor body starts. |
||
| @SuppressWarnings("unchecked") | ||
| public StorageImpl() { | ||
| this.size = 0; | ||
| keys = (K[]) new Object[MAX_SIZE]; | ||
| values = (V[]) new Object[MAX_SIZE]; | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line at the beginning of the 'put' method implementation. The @OverRide annotation on line 18 marks the start of the method, and there should be no empty line before it. |
||
| @Override | ||
| public void put(K key, V value) { | ||
| int index = findKeyIndex(key); | ||
|
|
||
| if (index != -1) { | ||
| values[index] = value; | ||
| return; | ||
| } | ||
| if (size == MAX_SIZE) { | ||
| throw new IllegalStateException("Storage is full"); | ||
| } | ||
| keys[size] = key; | ||
| values[size] = value; | ||
| size++; | ||
| } | ||
|
|
||
| @Override | ||
| public V get(K key) { | ||
| return null; | ||
| int index = findKeyIndex(key); | ||
| if (index == -1) { | ||
| return null; | ||
| } | ||
| return values[index]; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return size; | ||
| } | ||
|
|
||
| private int findKeyIndex(K key) { | ||
| for (int i = 0; i < size; i++) { | ||
| if ((keys[i] == null && key == null) | ||
| || (keys[i] != null && keys[i].equals(key))) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| } | ||
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 'size' field should be explicitly initialized in the constructor. According to the checklist, all non-constant fields must be initialized in the constructor.