-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Hw hash map solution #1699
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?
Hw hash map solution #1699
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 |
|---|---|---|
|
|
@@ -2,18 +2,85 @@ | |
|
|
||
| public class MyHashMap<K, V> implements MyMap<K, V> { | ||
|
|
||
| private static final int DEFAULT_INITIAL_CAPACITY = 16; | ||
|
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. This violates the checklist item exactly: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)" — there is an empty line immediately after the class declaration; remove that leading blank line so the class body starts without an empty line. |
||
| private static final double LOAD_FACTOR = 0.75; | ||
|
|
||
| private Node<K, V>[] table = new Node[DEFAULT_INITIAL_CAPACITY]; | ||
| private int size; | ||
|
|
||
| private static class Node<K, V> { | ||
| private final K key; | ||
| private V value; | ||
| private Node<K, V> next; | ||
|
|
||
| public Node(K key, V value, Node<K, V> next) { | ||
| this.key = key; | ||
| this.value = value; | ||
| this.next = next; | ||
| } | ||
| } | ||
|
|
||
| private int getIndex(K key, int length) { | ||
| return (key == null) ? 0 : Math.abs(key.hashCode() % length); | ||
| } | ||
|
|
||
| private boolean keysAreEqual(K key1, K key2) { | ||
| if (key1 == key2) { | ||
| return true; | ||
| } | ||
| return key1 != null && key1.equals(key2); | ||
| } | ||
|
|
||
| private void resize() { | ||
|
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. This violates checklist item #5: "Private methods and classes should be after public ones in your class." The private inner |
||
| Node<K,V>[] oldTable = table; | ||
| int newCapacity = table.length * 2; | ||
| table = new Node[newCapacity]; | ||
| size = 0; | ||
|
|
||
| for (Node<K, V> currentNode : oldTable) { | ||
| while (currentNode != null) { | ||
| int newIndex = getIndex(currentNode.key, newCapacity); | ||
| table[newIndex] = new Node<>(currentNode.key, currentNode.value, table[newIndex]); | ||
| size++; | ||
| currentNode = currentNode.next; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| int index = getIndex(key, table.length); | ||
|
|
||
| Node<K, V> currentNode = table[index]; | ||
| while (currentNode != null) { | ||
| if (keysAreEqual(currentNode.key, key)) { | ||
| currentNode.value = value; | ||
| return; | ||
| } | ||
| currentNode = currentNode.next; | ||
| } | ||
| table[index] = new Node<>(key, value, table[index]); | ||
| size++; | ||
| if (size >= table.length * LOAD_FACTOR) { | ||
| resize(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public V getValue(K key) { | ||
| int index = getIndex(key, table.length); | ||
| Node<K, V> currentNode = table[index]; | ||
| while (currentNode != null) { | ||
| if (keysAreEqual(currentNode.key, key)) { | ||
| return currentNode.value; | ||
| } | ||
| currentNode = currentNode.next; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public int getSize() { | ||
| return 0; | ||
| return size; | ||
| } | ||
| } | ||
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 violates checklist item #1: "Don't begin class or method implementation with empty line. Remove all redundant empty lines, be careful :)" There is an empty line at the start of the class body before the field declarations — remove it so the class/method implementations don't begin with a blank line.