Skip to content

Instantly share code, notes, and snippets.

@linnykoleh
Created March 29, 2017 04:02
Show Gist options
  • Save linnykoleh/c19c4750f61b5523f3fa1a0c945eb2d8 to your computer and use it in GitHub Desktop.
Save linnykoleh/c19c4750f61b5523f3fa1a0c945eb2d8 to your computer and use it in GitHub Desktop.
Getting value from an array
public V get(Object key) {
final int hashCodeForKey = hash(key);
final Node<K,V> nodeByKey = getNode(hashCodeForKey, key);
return nodeByKey == null ? null : nodeByKey.value;
}
private Node<K,V> getNode(int hashCodeKey, Object keySearch) {
Node<K,V> current;
Node<K,V> next;
K keyForNode;
if (hashTable != null && hashTable.length > 0) {
int indexByHashCodeKey = (hashTable.length - 1) & hashCodeKey;
current = hashTable[indexByHashCodeKey];
if(current != null) {
if (current.hashCodeKey == hashCodeKey) {
keyForNode = current.key;
if (keyForNode == keySearch || (keySearch != null && keySearch.equals(keyForNode))) {
return current;
}
}
next = current.next;
if (next != null) {
do {
if (next.hashCodeKey == hashCodeKey) {
keyForNode = next.key;
if (keyForNode == keySearch || (keySearch != null && keySearch.equals(keyForNode))) {
return next;
}
}
}
while ((next = next.next) != null);
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment