-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add my hash map #1707
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?
Add my hash map #1707
Changes from 1 commit
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 |
|---|---|---|
| @@ -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; | ||
| 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; | ||
| } | ||
|
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. Checklist item #5 violation: Private inner class |
||
| } | ||
|
|
||
| @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; | ||
|
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. 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(); | ||
| } | ||
| } | ||
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.
Use the constant instead of hardcoded value for consistency:
private int capacity = DEFAULT_CAPACITY;