Created
October 21, 2013 04:56
-
-
Save rohitkandhal/7078851 to your computer and use it in GitHub Desktop.
Java HashMapget function
This file contains 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
/** | |
* Returns the value to which the specified key is mapped, | |
* or {@code null} if this map contains no mapping for the key. | |
*/ | |
public V get(Object key) { | |
if (key == null) | |
return getForNullKey(); | |
Entry<K,V> entry = getEntry(key); | |
return null == entry ? null : entry.getValue(); | |
} | |
/** | |
* Returns the entry associated with the specified key in the | |
* HashMap. Returns null if the HashMap contains no mapping | |
* for the key. | |
*/ | |
final Entry<K,V> getEntry(Object key) { | |
int hash = (key == null) ? 0 : hash(key); | |
for (Entry<K,V> e = table[indexFor(hash, table.length)]; | |
e != null; | |
e = e.next) { | |
Object k; | |
if (e.hash == hash && | |
((k = e.key) == key || (key != null && key.equals(k)))) | |
return e; | |
} | |
return null; | |
} | |
/** | |
* The table, resized as necessary. Length MUST Always be a power of two. | |
*/ | |
transient Entry<K,V>[] table; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment