Created
May 3, 2017 17:37
-
-
Save linnykoleh/8a17a93129dda0278952179bac0f8a38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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