-
Notifications
You must be signed in to change notification settings - Fork 1.5k
MY HASHMAP #1589
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?
MY HASHMAP #1589
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package core.basesyntax; | ||
|
||
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. There are several redundant empty lines throughout the file (e.g., here, and on lines 4, 9, 13, etc.). Please remove them to improve code readability and adhere to the style guide. |
||
import java.util.Objects; | ||
public class MyHashMap<K, V> implements MyMap<K, V> { | ||
private static final int DEFAULT_CAPACITY = 16; | ||
private static final float DEFAULT_LOAD_FACTOR = 0.75f; | ||
private static final int RESIZE_MULTIPLIER = 2; | ||
private Node<K, V>[] table; | ||
private int size; | ||
private int threshold; | ||
|
||
public MyHashMap() { | ||
table = new Node[DEFAULT_CAPACITY]; | ||
threshold = (int) (DEFAULT_CAPACITY * DEFAULT_LOAD_FACTOR); | ||
} | ||
|
||
@Override | ||
public void put(K key, V value) { | ||
if (size >= threshold) { | ||
resize(); | ||
} | ||
int index = getIndex(key, table.length); | ||
Node<K, V> current = table[index]; | ||
if (current == null) { | ||
table[index] = new Node<>(key, value, null); | ||
size++; | ||
return; | ||
} | ||
Node<K, V> prev = null; | ||
while (current != null) { | ||
if (Objects.equals(current.key, key)) { | ||
current.value = value; | ||
return; | ||
} | ||
prev = current; | ||
current = current.next; | ||
} | ||
prev.next = new Node<>(key, value, null); | ||
size++; | ||
Comment on lines
+23
to
+39
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. The logic for adding a new element to a bucket can be simplified. Currently, you are traversing the linked list to add a new node at the end. A more common and slightly more efficient approach is to add new nodes to the beginning of the list. This avoids the need to iterate to the end and simplifies the code by removing the |
||
} | ||
|
||
@Override | ||
public V getValue(K key) { | ||
int index = getIndex(key, table.length); | ||
Node<K, V> current = table[index]; | ||
while (current != null) { | ||
if (Objects.equals(current.key, key)) { | ||
return current.value; | ||
} | ||
current = current.next; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
return size; | ||
} | ||
|
||
private void resize() { | ||
Node<K, V>[] oldTable = table; | ||
int newCapacity = oldTable.length * RESIZE_MULTIPLIER; | ||
table = new Node[newCapacity]; | ||
threshold = (int) (newCapacity * DEFAULT_LOAD_FACTOR); | ||
size = 0; | ||
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. In the |
||
for (Node<K, V> headNode : oldTable) { | ||
while (headNode != null) { | ||
Node<K, V> nextNode = headNode.next; | ||
int index = getIndex(headNode.key, newCapacity); | ||
headNode.next = table[index]; | ||
table[index] = headNode; | ||
size++; | ||
headNode = nextNode; | ||
} | ||
} | ||
} | ||
|
||
private int getIndex(K key, int length) { | ||
if (key == null) { | ||
return 0; | ||
} | ||
return (key.hashCode() & 0x7fffffff) % length; | ||
} | ||
|
||
private static class Node<K, V> { | ||
private final K key; | ||
private V value; | ||
private Node<K, V> next; | ||
Node(K key, V value, Node<K, V> next) { | ||
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. The |
||
this.key = key; | ||
this.value = value; | ||
this.next = next; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
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. Avoid a redundant empty line after the package declaration. 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. There are several redundant empty lines in the interface definition (e.g., here, and on lines 5 and 7). Please remove them to make the code more compact and clean, following the coding style guidelines. |
||
public interface MyMap<K, V> { | ||
void put(K key, V value); | ||
V getValue(K key); | ||
int getSize(); | ||
} |
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.
Avoid redundant empty lines like this one. There are several of them in the file (e.g., lines 4, 8, 12, 17, etc.), which reduces readability.