-
Notifications
You must be signed in to change notification settings - Fork 1.5k
my HashMap #1590
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 #1590
Changes from 2 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 |
---|---|---|
@@ -1,19 +1,13 @@ | ||
package core.basesyntax; | ||
|
||
public class MyHashMap<K, V> implements MyMap<K, V> { | ||
import org.w3c.dom.Node; | ||
|
||
|
||
@Override | ||
public void put(K key, V value) { | ||
public class MyHashMap<K, V> { | ||
|
||
private static final int DEFAULT_CAPACITY = 16; | ||
private static final float LOAD_FACTOR = 0.75f; | ||
|
||
public MyHashMap() { | ||
Node[] table = new Node[DEFAULT_CAPACITY]; | ||
int size = 0; | ||
} | ||
} | ||
|
||
@Override | ||
public V getValue(K key) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
return 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,83 @@ | ||
package core.basesyntax; | ||
import java.util.Objects; | ||
|
||
public interface MyMap<K, V> { | ||
void put(K key, V value); | ||
public class MyHashMap<K, V> { | ||
|
||
private static final int DEFAULT_CAPACITY = 16; | ||
private static final float LOAD_FACTOR = 0.75f; | ||
|
||
V getValue(K key); | ||
private Node<K, V>[] table; | ||
private int size; | ||
|
||
int getSize(); | ||
@SuppressWarnings("unchecked") | ||
public MyHashMap() { | ||
table = (Node<K, V>[]) new Node[DEFAULT_CAPACITY]; | ||
size = 0; | ||
} | ||
|
||
public void put(K key, V value) { | ||
if ((float) size / table.length >= LOAD_FACTOR) { | ||
resize(); | ||
} | ||
int index = getIndex(key); | ||
Node<K, V> current = table[index]; | ||
|
||
while (current != null) { | ||
if (Objects.equals(current.key, key)) { | ||
|
||
current.value = value; // оновлюємо існуюче значення | ||
return; | ||
} | ||
current = current.next; | ||
} | ||
|
||
table[index] = new Node<>(key, value, table[index]); | ||
size++; | ||
} | ||
|
||
public V getValue(K key) { | ||
int index = getIndex(key); | ||
Node<K, V> current = table[index]; | ||
|
||
while (current != null) { | ||
if (Objects.equals(current.key, key)) { | ||
return current.value; | ||
} | ||
current = current.next; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public int getSize() { | ||
return size; | ||
} | ||
|
||
private int getIndex(K key) { | ||
return (key == null) ? 0 : (key.hashCode() & 0x7fffffff) % table.length; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void resize() { | ||
Node<K, V>[] oldTable = table; | ||
table = (Node<K, V>[]) new Node[oldTable.length * 2]; | ||
|
||
size = 0; // заново перерахуємо при rehash | ||
|
||
for (Node<K, V> node : oldTable) { | ||
while (node != null) { | ||
put(node.key, node.value); | ||
node = node.next; | ||
} | ||
} | ||
} | ||
|
||
private static class Node<K, V> { | ||
K key; | ||
|
||
V value; | ||
|
||
Node<K, V> next; | ||
|
||
|
||
Node(K key, V value, Node<K, V> next) { | ||
this.key = key; | ||
this.value = value; | ||
this.next = next; | ||
} | ||
} | ||
} |
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.
This
import
statement is incorrect. TheMyHashMap
class uses its own innerNode
class for its entries. Importingorg.w3c.dom.Node
introduces a name collision, as it's a class from the XML DOM API, and will cause your code to fail compilation. Please remove this line.