java.lang.Object | +--java.util.AbstractMap | +--java.util.HashMapAll Implemented Interfaces:
HashMap() Construct a new HashMap with the default capacity (11) and the default load factor (0.75). |
HashMap(java.util.Map m) Construct a new HashMap from the given Map, with initial capacity
the greater of the size of |
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. |
void | clear() Clears the Map so it has no keys. |
java.lang.Object | clone() Returns a shallow clone of this HashMap. |
boolean | containsKey(java.lang.Object key) Returns true if the supplied object |
boolean | containsValue(java.lang.Object value) Returns true if this HashMap contains a value |
java.util.Set | entrySet() Returns a "set view" of this HashMap's entries. |
java.lang.Object | get(java.lang.Object key) Return the value in this HashMap associated with the supplied key,
or |
boolean | isEmpty() Returns true if there are no key-value mappings currently in this Map. |
java.util.Set | keySet() Returns a "set view" of this HashMap's keys. |
java.lang.Object | put(java.lang.Object key, java.lang.Object value) Puts the supplied value into the Map, mapped by the supplied key. |
void | putAll(java.util.Map m) Copies all elements of the given map into this hashtable. |
java.lang.Object | remove(java.lang.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. |
java.util.Collection | values() Returns a "collection view" (or "bag view") of this HashMap's values. |
public HashMap()
public 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)public HashMap(java.util.Map 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.NullPointerException
- if m is nullpublic void clear()
public Object clone()
public boolean containsKey(java.lang.Object key)
equals()
a key
in this HashMap.
key
- the key to search for in this HashMappublic boolean containsValue(java.lang.Object value)
o
, such that
o.equals(value)
.
value
- the value to search for in this HashMapcontainsKey(Object)
public Set entrySet()
Note that the iterators for all three views, from keySet(), entrySet(), and values(), traverse the HashMap in the same sequence.
public Object get(java.lang.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.
key
- the key for which to fetch an associated valuepublic boolean isEmpty()
size() == 0
public Set keySet()
public Object put(java.lang.Object key, java.lang.Object 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.
key
- the key used to locate the valuevalue
- the value to be stored in the HashMappublic void putAll(java.util.Map m)
m
- the map to be hashed into thispublic Object remove(java.lang.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.
key
- the key used to locate the value to removepublic int size()
public Collection values()
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 aConcurrentModificationException
rather than exhibit non-deterministic behavior.