Skip to content
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

Implemented the Storage of the key-value #1674

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,66 @@
import core.basesyntax.Storage;

public class StorageImpl<K, V> implements Storage<K, V> {
private static final int MAX_SIZE = 10;
private final K[] keys;
private final V[] values;
private int size;

public StorageImpl() {
//noinspection unchecked
keys = (K[]) new Object [MAX_SIZE];
//noinspection unchecked
values = (V[]) new Object [MAX_SIZE];
size = 0;
}

@Override
public void put(K key, V value) {
if (size >= MAX_SIZE) {
throw new RuntimeException("Storage is full, cannot add more elements.");
}

if (key == null) {
for (int i = 0; i < size; i++) {
if (keys[i] == null) {
values[i] = value;
return;
}
}
keys[size] = null;
Comment on lines +25 to +32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation allows multiple null keys to be stored. According to the task requirements, you should prevent storing duplicate null keys. Consider adding a check to ensure that a null key is not added if it already exists in the storage.

} else {
for (int i = 0; i < size; i++) {
if (keys[i] != null && keys[i].equals(key)) {
values[i] = value;
return;
}
}
keys[size] = key;
}
values[size] = value;
size++;
}

@Override
public V get(K key) {
if (key == null) {
for (int i = 0; i < size; i++) {
if (keys[i] == null) {
return values[i];
}
}
} else {
for (int i = 0; i < size; i++) {
if (keys[i] != null && keys[i].equals(key)) {
return values[i];
}
}
}
return null;
}

@Override
public int size() {
return -1;
return size;
}
}
Loading