Skip to content

Instantly share code, notes, and snippets.

@rohitvvv
Created June 13, 2019 20:19
Show Gist options
  • Save rohitvvv/b0cde774a97873c2d0f7fb29bfbf6f8d to your computer and use it in GitHub Desktop.
Save rohitvvv/b0cde774a97873c2d0f7fb29bfbf6f8d to your computer and use it in GitHub Desktop.
HashMap Get
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
/**
* Implements Map.get and related methods
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment