java.util
Class HashMap<K,V>

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
      extended by java.util.HashMap<K,V>
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Map sample code for java.util.Map definition code for java.util.Map <K,V>
Direct Known Subclasses:
LinkedHashMap sample code for java.util.LinkedHashMap definition code for java.util.LinkedHashMap , PrinterStateReasons sample code for javax.print.attribute.standard.PrinterStateReasons definition code for javax.print.attribute.standard.PrinterStateReasons

public class HashMap<K,V>
extends AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
implements Map sample code for java.util.Map definition code for java.util.Map <K,V>, Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Serializable sample code for java.io.Serializable definition code for java.io.Serializable

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method.

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.

Note that this implementation is not synchronized. If multiple threads access this map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

 Map m = Collections.synchronizedMap(new HashMap(...));
 

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class is a member of the Java Collections Framework.

Since:
1.2
See Also:
Object.hashCode() sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , Collection sample code for java.util.Collection definition code for java.util.Collection , Map sample code for java.util.Map definition code for java.util.Map , TreeMap sample code for java.util.TreeMap definition code for java.util.TreeMap , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable , Serialized Form

Constructor Summary
HashMap sample code for java.util.HashMap.HashMap() definition code for java.util.HashMap.HashMap() ()
          Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
HashMap sample code for java.util.HashMap.HashMap(int) definition code for java.util.HashMap.HashMap(int) (int initialCapacity)
          Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
HashMap sample code for java.util.HashMap.HashMap(int, float) definition code for java.util.HashMap.HashMap(int, float) (int initialCapacity, float loadFactor)
          Constructs an empty HashMap with the specified initial capacity and load factor.
HashMap sample code for java.util.HashMap.HashMap(java.util.Map) definition code for java.util.HashMap.HashMap(java.util.Map) (Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> m)
          Constructs a new HashMap with the same mappings as the specified Map.
 
Method Summary
 void clear sample code for java.util.HashMap.clear() definition code for java.util.HashMap.clear() ()
          Removes all mappings from this map.
 Object sample code for java.lang.Object definition code for java.lang.Object clone sample code for java.util.HashMap.clone() definition code for java.util.HashMap.clone() ()
          Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
 boolean containsKey sample code for java.util.HashMap.containsKey(java.lang.Object) definition code for java.util.HashMap.containsKey(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue sample code for java.util.HashMap.containsValue(java.lang.Object) definition code for java.util.HashMap.containsValue(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  value)
          Returns true if this map maps one or more keys to the specified value.
 Set sample code for java.util.Set definition code for java.util.Set <Map.Entry sample code for java.util.Map.Entry definition code for java.util.Map.Entry <K,V>> entrySet sample code for java.util.HashMap.entrySet() definition code for java.util.HashMap.entrySet() ()
          Returns a collection view of the mappings contained in this map.
 V get sample code for java.util.HashMap.get(java.lang.Object) definition code for java.util.HashMap.get(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  key)
          Returns the value to which the specified key is mapped in this identity hash map, or null if the map contains no mapping for this key.
 boolean isEmpty sample code for java.util.HashMap.isEmpty() definition code for java.util.HashMap.isEmpty() ()
          Returns true if this map contains no key-value mappings.
 Set sample code for java.util.Set definition code for java.util.Set <K> keySet sample code for java.util.HashMap.keySet() definition code for java.util.HashMap.keySet() ()
          Returns a set view of the keys contained in this map.
 V put sample code for java.util.HashMap.put(K, V) definition code for java.util.HashMap.put(K, V) (K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll sample code for java.util.HashMap.putAll(java.util.Map) definition code for java.util.HashMap.putAll(java.util.Map) (Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> m)
          Copies all of the mappings from the specified map to this map These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
 V remove sample code for java.util.HashMap.remove(java.lang.Object) definition code for java.util.HashMap.remove(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  key)
          Removes the mapping for this key from this map if present.
 int size sample code for java.util.HashMap.size() definition code for java.util.HashMap.size() ()
          Returns the number of key-value mappings in this map.
 Collection sample code for java.util.Collection definition code for java.util.Collection <V> values sample code for java.util.HashMap.values() definition code for java.util.HashMap.values() ()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class java.util.AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap
equals sample code for java.util.AbstractMap.equals(java.lang.Object) definition code for java.util.AbstractMap.equals(java.lang.Object) , hashCode sample code for java.util.AbstractMap.hashCode() definition code for java.util.AbstractMap.hashCode() , toString sample code for java.util.AbstractMap.toString() definition code for java.util.AbstractMap.toString()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.util.Map sample code for java.util.Map definition code for java.util.Map
equals sample code for java.util.Map.equals(java.lang.Object) definition code for java.util.Map.equals(java.lang.Object) , hashCode sample code for java.util.Map.hashCode() definition code for java.util.Map.hashCode()
 

Constructor Detail

HashMap sample code for java.util.HashMap(int, float) definition code for java.util.HashMap(int, float)

public HashMap(int initialCapacity,
               float loadFactor)
Constructs an empty HashMap with the specified initial capacity and load factor.

Parameters:
initialCapacity - The initial capacity.
loadFactor - The load factor.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive.

HashMap sample code for java.util.HashMap(int) definition code for java.util.HashMap(int)

public HashMap(int initialCapacity)
Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).

Parameters:
initialCapacity - the initial capacity.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the initial capacity is negative.

HashMap sample code for java.util.HashMap() definition code for java.util.HashMap()

public HashMap()
Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).


HashMap sample code for java.util.HashMap(java.util.Map<? extends K, ? extends V>) definition code for java.util.HashMap(java.util.Map<? extends K, ? extends V>)

public HashMap(Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> m)
Constructs a new HashMap with the same mappings as the specified Map. The HashMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.

Parameters:
m - the map whose mappings are to be placed in this map.
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified map is null.
Method Detail

size sample code for java.util.HashMap.size() definition code for java.util.HashMap.size()

public int size()
Returns the number of key-value mappings in this map.

Specified by:
size sample code for java.util.Map.size() definition code for java.util.Map.size() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
size sample code for java.util.AbstractMap.size() definition code for java.util.AbstractMap.size() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
the number of key-value mappings in this map.

isEmpty sample code for java.util.HashMap.isEmpty() definition code for java.util.HashMap.isEmpty()

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty sample code for java.util.Map.isEmpty() definition code for java.util.Map.isEmpty() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
isEmpty sample code for java.util.AbstractMap.isEmpty() definition code for java.util.AbstractMap.isEmpty() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
true if this map contains no key-value mappings.

get sample code for java.util.HashMap.get(java.lang.Object) definition code for java.util.HashMap.get(java.lang.Object)

public V get(Object sample code for java.lang.Object definition code for java.lang.Object  key)
Returns the value to which the specified key is mapped in this identity hash map, or null if the map contains no mapping for this key. A return value of null does not necessarily indicate that the map contains no mapping for the key; it is also possible that the map explicitly maps the key to null. The containsKey method may be used to distinguish these two cases.

Specified by:
get sample code for java.util.Map.get(java.lang.Object) definition code for java.util.Map.get(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
get sample code for java.util.AbstractMap.get(java.lang.Object) definition code for java.util.AbstractMap.get(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Parameters:
key - the key whose associated value is to be returned.
Returns:
the value to which this map maps the specified key, or null if the map contains no mapping for this key.
See Also:
put(Object, Object) sample code for java.util.HashMap.put(K, V) definition code for java.util.HashMap.put(K, V)

containsKey sample code for java.util.HashMap.containsKey(java.lang.Object) definition code for java.util.HashMap.containsKey(java.lang.Object)

public boolean containsKey(Object sample code for java.lang.Object definition code for java.lang.Object  key)
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey sample code for java.util.Map.containsKey(java.lang.Object) definition code for java.util.Map.containsKey(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
containsKey sample code for java.util.AbstractMap.containsKey(java.lang.Object) definition code for java.util.AbstractMap.containsKey(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Parameters:
key - The key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the specified key.

put sample code for java.util.HashMap.put(K, V) definition code for java.util.HashMap.put(K, V)

public V put(K key,
             V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

Specified by:
put sample code for java.util.Map.put(K, V) definition code for java.util.Map.put(K, V) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
put sample code for java.util.AbstractMap.put(K, V) definition code for java.util.AbstractMap.put(K, V) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the HashMap previously associated null with the specified key.

putAll sample code for java.util.HashMap.putAll(java.util.Map<? extends K, ? extends V>) definition code for java.util.HashMap.putAll(java.util.Map<? extends K, ? extends V>)

public void putAll(Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

Specified by:
putAll sample code for java.util.Map.putAll(java.util.Map) definition code for java.util.Map.putAll(java.util.Map) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
putAll sample code for java.util.AbstractMap.putAll(java.util.Map) definition code for java.util.AbstractMap.putAll(java.util.Map) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Parameters:
m - mappings to be stored in this map.
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified map is null.

remove sample code for java.util.HashMap.remove(java.lang.Object) definition code for java.util.HashMap.remove(java.lang.Object)

public V remove(Object sample code for java.lang.Object definition code for java.lang.Object  key)
Removes the mapping for this key from this map if present.

Specified by:
remove sample code for java.util.Map.remove(java.lang.Object) definition code for java.util.Map.remove(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
remove sample code for java.util.AbstractMap.remove(java.lang.Object) definition code for java.util.AbstractMap.remove(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Parameters:
key - key whose mapping is to be removed from the map.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key.

clear sample code for java.util.HashMap.clear() definition code for java.util.HashMap.clear()

public void clear()
Removes all mappings from this map.

Specified by:
clear sample code for java.util.Map.clear() definition code for java.util.Map.clear() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
clear sample code for java.util.AbstractMap.clear() definition code for java.util.AbstractMap.clear() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>

containsValue sample code for java.util.HashMap.containsValue(java.lang.Object) definition code for java.util.HashMap.containsValue(java.lang.Object)

public boolean containsValue(Object sample code for java.lang.Object definition code for java.lang.Object  value)
Returns true if this map maps one or more keys to the specified value.

Specified by:
containsValue sample code for java.util.Map.containsValue(java.lang.Object) definition code for java.util.Map.containsValue(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
containsValue sample code for java.util.AbstractMap.containsValue(java.lang.Object) definition code for java.util.AbstractMap.containsValue(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Parameters:
value - value whose presence in this map is to be tested.
Returns:
true if this map maps one or more keys to the specified value.

clone sample code for java.util.HashMap.clone() definition code for java.util.HashMap.clone()

public Object sample code for java.lang.Object definition code for java.lang.Object  clone()
Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.

Overrides:
clone sample code for java.util.AbstractMap.clone() definition code for java.util.AbstractMap.clone() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
a shallow copy of this map.
See Also:
Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable

keySet sample code for java.util.HashMap.keySet() definition code for java.util.HashMap.keySet()

public Set sample code for java.util.Set definition code for java.util.Set <K> keySet()
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
keySet sample code for java.util.Map.keySet() definition code for java.util.Map.keySet() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
keySet sample code for java.util.AbstractMap.keySet() definition code for java.util.AbstractMap.keySet() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
a set view of the keys contained in this map.

values sample code for java.util.HashMap.values() definition code for java.util.HashMap.values()

public Collection sample code for java.util.Collection definition code for java.util.Collection <V> values()
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
values sample code for java.util.Map.values() definition code for java.util.Map.values() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
values sample code for java.util.AbstractMap.values() definition code for java.util.AbstractMap.values() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
a collection view of the values contained in this map.

entrySet sample code for java.util.HashMap.entrySet() definition code for java.util.HashMap.entrySet()

public Set sample code for java.util.Set definition code for java.util.Set <Map.Entry sample code for java.util.Map.Entry definition code for java.util.Map.Entry <K,V>> entrySet()
Returns a collection view of the mappings contained in this map. Each element in the returned collection is a Map.Entry. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet sample code for java.util.Map.entrySet() definition code for java.util.Map.entrySet() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Specified by:
entrySet sample code for java.util.AbstractMap.entrySet() definition code for java.util.AbstractMap.entrySet() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
a collection view of the mappings contained in this map.
See Also:
Map.Entry sample code for java.util.Map.Entry definition code for java.util.Map.Entry