Skip to content
Open
Changes from 1 commit
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
86 changes: 84 additions & 2 deletions src/main/java/core/basesyntax/MyHashMap.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,101 @@
package core.basesyntax;

import java.util.Objects;

public class MyHashMap<K, V> implements MyMap<K, V> {
private static final int DEFAULT_CAPACITY = 16;
private static final double DEFAULT_LOAD_FACTOR = .75;

private int size;
private int capacity = DEFAULT_CAPACITY;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use the constant instead of hardcoded value for consistency: private int capacity = DEFAULT_CAPACITY;

private Entry<K, V>[] table = new Entry[capacity];

public MyHashMap() {
}

private static class Entry<K, V> {
private final K key;
private final int hash;
private V value;
private Entry<K, V> next;

public Entry(K key, V value) {
this.key = key;
this.hash = getHash(key);
this.value = value;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #5 violation: Private inner class Entry is defined after the constructor but before the public methods. According to the requirements, private methods and classes must be placed after all public ones. Move the entire Entry class to the end of the file, after all public methods.

}

@Override
public void put(K key, V value) {
if (size + 1 > capacity * DEFAULT_LOAD_FACTOR) {
increaseBuckets();
}

int keyHash = getHash(key);
int bucketIndex = keyHash & (capacity - 1);

putInBucket(key, value, keyHash, bucketIndex);
}

@Override
public V getValue(K key) {
return null;
int keyHash = getHash(key);
int bucketIndex = keyHash & (capacity - 1);
Entry<K, V> entry = table[bucketIndex];
if (entry == null) {
return null;
}

while (entry != null && (entry.hash != keyHash || !Objects.equals(entry.key, key))) {
entry = entry.next;
}
return entry == null ? null : entry.value;
}

@Override
public int getSize() {
return 0;
return size;
}

private void increaseBuckets() {
size = 0;
capacity *= 2;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CHECKLIST ITEM #2 violation: The value '2' used in capacity doubling is a magic number. Extract this to a named constant field (e.g., CAPACITY_MULTIPLIER) at the top of the class with other constants.

Entry<K, V>[] oldEntries = table;
table = new Entry[capacity];
for (Entry<K, V> entry : oldEntries) {
if (entry != null) {
do {
put(entry.key, entry.value);
entry = entry.next;
} while (entry != null);
}
}
}

private void putInBucket(K key, V value, int keyHash, int bucketIndex) {
Entry<K, V> entry = table[bucketIndex];
Entry<K, V> prevEntry = entry;

if (entry == null) {
table[bucketIndex] = new Entry<>(key, value);
size++;
return;
}

while (entry != null) {
if (entry.hash == keyHash && Objects.equals(entry.key, key)) {
entry.value = value;
return;
}
prevEntry = entry;
entry = entry.next;
}
prevEntry.next = new Entry<>(key, value);
size++;
}

private static <K> int getHash(K key) {
return key == null ? 0 : key.hashCode();
}
}
Loading