|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.util.AbstractMap<K,V>
java.util.HashMap<K,V>
public class HashMap<K,V>
This class provides a hashtable-backed implementation of the Map interface.
It uses a hash-bucket approach; that is, hash collisions are handled by linking the new node off of the pre-existing node (or list of nodes). In this manner, techniques such as linear probing (which can cause primary clustering) and rehashing (which does not fit very well with Java's method of precomputing hash codes) are avoided.
Under ideal circumstances (no collisions), HashMap offers O(1)
performance on most operations (containsValue() is,
of course, O(n)). In the worst case (all keys map to the same
hash code -- very unlikely), most operations are O(n).
HashMap is part of the JDK1.2 Collections API. It differs from
Hashtable in that it accepts the null key and null values, and it
does not support "Enumeration views." Also, it is not synchronized;
if you plan to use it in multiple threads, consider using:
Map m = Collections.synchronizedMap(new HashMap(...));
The iterators are fail-fast, meaning that any structural
modification, except for remove() called on the iterator
itself, cause the iterator to throw a
ConcurrentModificationException rather than exhibit
non-deterministic behavior.
Object.hashCode(),
Collection,
Map,
TreeMap,
LinkedHashMap,
IdentityHashMap,
Hashtable,
Serialized Form| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class java.util.AbstractMap |
|---|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V> |
| Nested classes/interfaces inherited from interface java.util.Map |
|---|
Map.Entry<K,V> |
| Constructor Summary | |
|---|---|
HashMap()
Construct a new HashMap with the default capacity (11) and the default load factor (0.75). |
|
HashMap(int initialCapacity)
Construct a new HashMap with a specific inital capacity and default load factor of 0.75. |
|
HashMap(int initialCapacity,
float loadFactor)
Construct a new HashMap with a specific inital capacity and load factor. |
|
HashMap(Map<? extends K,? extends V> m)
Construct a new HashMap from the given Map, with initial capacity the greater of the size of m or the default of 11. |
|
| Method Summary | |
|---|---|
void |
clear()
Clears the Map so it has no keys. |
Object |
clone()
Returns a shallow clone of this HashMap. |
boolean |
containsKey(Object key)
Returns true if the supplied object equals() a key
in this HashMap. |
boolean |
containsValue(Object value)
Returns true if this HashMap contains a value o, such that
o.equals(value). |
Set<Map.Entry<K,V>> |
entrySet()
Returns a "set view" of this HashMap's entries. |
V |
get(Object key)
Return the value in this HashMap associated with the supplied key, or null if the key maps to nothing. |
boolean |
isEmpty()
Returns true if there are no key-value mappings currently in this Map. |
Set<K> |
keySet()
Returns a "set view" of this HashMap's keys. |
V |
put(K key,
V value)
Puts the supplied value into the Map, mapped by the supplied key. |
void |
putAll(Map<? extends K,? extends V> m)
Copies all elements of the given map into this hashtable. |
V |
remove(Object key)
Removes from the HashMap and returns the value which is mapped by the supplied key. |
int |
size()
Returns the number of kay-value mappings currently in this Map. |
Collection<V> |
values()
Returns a "collection view" (or "bag view") of this HashMap's values. |
| Methods inherited from class java.util.AbstractMap |
|---|
equals, hashCode, toString |
| Methods inherited from class java.lang.Object |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.util.Map |
|---|
equals, hashCode |
| Constructor Detail |
|---|
public HashMap()
public HashMap(Map<? extends K,? extends V> m)
m or the default of 11.
Every element in Map m will be put into this new HashMap.
m - a Map whose key / value pairs will be put into the new HashMap.
NOTE: key / value pairs are not cloned in this constructor.
NullPointerException - if m is nullpublic HashMap(int initialCapacity)
initialCapacity - the initial capacity of this HashMap (>=0)
IllegalArgumentException - if (initialCapacity < 0)
public HashMap(int initialCapacity,
float loadFactor)
initialCapacity - the initial capacity (>=0)loadFactor - the load factor (> 0, not NaN)
IllegalArgumentException - if (initialCapacity < 0) ||
! (loadFactor > 0.0)| Method Detail |
|---|
public int size()
size in interface Map<K,V>size in class AbstractMap<K,V>Set.size()public boolean isEmpty()
isEmpty in interface Map<K,V>isEmpty in class AbstractMap<K,V>size() == 0AbstractMap.size()public V get(Object key)
null if the key maps to nothing. NOTE: Since the value
could also be null, you must use containsKey to see if this key
actually maps to something.
get in interface Map<K,V>get in class AbstractMap<K,V>key - the key for which to fetch an associated value
put(Object, Object),
containsKey(Object)public boolean containsKey(Object key)
equals() a key
in this HashMap.
containsKey in interface Map<K,V>containsKey in class AbstractMap<K,V>key - the key to search for in this HashMap
containsValue(Object)
public V put(K key,
V value)
equals()
this key. NOTE: Since the prior value could also be null, you must
first use containsKey if you want to see if you are replacing the
key's mapping.
put in interface Map<K,V>put in class AbstractMap<K,V>key - the key used to locate the valuevalue - the value to be stored in the HashMap
get(Object),
Object.equals(Object)public void putAll(Map<? extends K,? extends V> m)
putAll in interface Map<K,V>putAll in class AbstractMap<K,V>m - the map to be hashed into thisAbstractMap.put(Object, Object)public V remove(Object key)
null is returned. NOTE: Since the value
could also be null, you must use containsKey to see if you are
actually removing a mapping.
remove in interface Map<K,V>remove in class AbstractMap<K,V>key - the key used to locate the value to remove
Iterator.remove()public void clear()
clear in interface Map<K,V>clear in class AbstractMap<K,V>Set.clear()public boolean containsValue(Object value)
o, such that
o.equals(value).
containsValue in interface Map<K,V>containsValue in class AbstractMap<K,V>value - the value to search for in this HashMap
containsKey(Object)public Object clone()
clone in class AbstractMap<K,V>Cloneable,
Object.clone()public Set<K> keySet()
keySet in interface Map<K,V>keySet in class AbstractMap<K,V>values(),
entrySet()public Collection<V> values()
values in interface Map<K,V>values in class AbstractMap<K,V>keySet(),
entrySet()public Set<Map.Entry<K,V>> entrySet()
Note that the iterators for all three views, from keySet(), entrySet(), and values(), traverse the HashMap in the same sequence.
entrySet in interface Map<K,V>entrySet in class AbstractMap<K,V>keySet(),
values(),
Map.Entry
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||