Skip to content

Instantly share code, notes, and snippets.

@linnykoleh
Created May 3, 2017 17:37
Show Gist options
  • Save linnykoleh/8a17a93129dda0278952179bac0f8a38 to your computer and use it in GitHub Desktop.
Save linnykoleh/8a17a93129dda0278952179bac0f8a38 to your computer and use it in GitHub Desktop.
public V get(Object key) {
Entry<K, V> entry = getEntry(key);
return (entry == null ? null : entry.value);
}
private Entry<K, V> getEntry(Object key) {
if (comparator != null) {
return getEntryUsingComparator(key);
}
if (key == null) {
throw new NullPointerException();
}
Comparable<? super K> searchingKey = (Comparable<? super K>) key;
Entry<K, V> entry = root;
while (entry != null) {
int result = searchingKey.compareTo(entry.key);
if (result < 0) {
entry = entry.left;
} else if (result > 0) {
entry = entry.right;
} else {
return entry;
}
}
return null;
}
private Entry<K, V> getEntryUsingComparator(Object key) {
Comparator<? super K> cpr = comparator;
if (cpr != null) {
Entry<K, V> entry = root;
while (entry != null) {
int result = cpr.compare((K)key, entry.key);
if (result < 0) {
entry = entry.left;
} else if (result > 0) {
entry = entry.right;
} else {
return entry;
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment