1 *java.util.HashMap* *HashMap* Hash table based implementation of the Map interfa
3 public class HashMap<K,V>
4 extends |java.util.AbstractMap|
5 implements |java.util.Map|
9 |java.util.HashMap_Description|
10 |java.util.HashMap_Fields|
11 |java.util.HashMap_Constructors|
12 |java.util.HashMap_Methods|
14 ================================================================================
16 *java.util.HashMap_Constructors*
17 |java.util.HashMap()|Constructs an empty HashMap with the default initial capac
18 |java.util.HashMap(int)|Constructs an empty HashMap with the specified initial
19 |java.util.HashMap(int,float)|Constructs an empty HashMap with the specified in
20 |java.util.HashMap(Map<?extendsK,?extendsV>)|Constructs a new HashMap with the
22 *java.util.HashMap_Methods*
23 |java.util.HashMap.clear()|Removes all of the mappings from this map.
24 |java.util.HashMap.clone()|Returns a shallow copy of this HashMap instance: the
25 |java.util.HashMap.containsKey(Object)|Returns true if this map contains a mapp
26 |java.util.HashMap.containsValue(Object)|Returns true if this map maps one or m
27 |java.util.HashMap.entrySet()|Returns aSetview of the mappings contained in thi
28 |java.util.HashMap.get(Object)|Returns the value to which the specified key is
29 |java.util.HashMap.isEmpty()|Returns true if this map contains no key-value map
30 |java.util.HashMap.keySet()|Returns aSetview of the keys contained in this map.
31 |java.util.HashMap.put(K,V)|Associates the specified value with the specified k
32 |java.util.HashMap.putAll(Map<?extendsK,?extendsV>)|Copies all of the mappings
33 |java.util.HashMap.remove(Object)|Removes the mapping for the specified key fro
34 |java.util.HashMap.size()|Returns the number of key-value mappings in this map.
35 |java.util.HashMap.values()|Returns aCollectionview of the values contained in
37 *java.util.HashMap_Description*
39 Hash table based implementation of the Map interface. This implementation
40 provides all of the optional map operations, and permits null values and the
41 null key. (The HashMap class is roughly equivalent to Hashtable, except that it
42 is unsynchronized and permits nulls.) This class makes no guarantees as to the
43 order of the map; in particular, it does not guarantee that the order will
44 remain constant over time.
46 This implementation provides constant-time performance for the basic operations
47 (get and put), assuming the hash function disperses the elements properly among
48 the buckets. Iteration over collection views requires time proportional to the
49 "capacity" of the HashMap instance (the number of buckets) plus its size (the
50 number of key-value mappings). Thus, it's very important not to set the initial
51 capacity too high (or the load factor too low) if iteration performance is
54 An instance of HashMap has two parameters that affect its performance: initial
55 capacity and load factor. The capacity is the number of buckets in the hash
56 table, and the initial capacity is simply the capacity at the time the hash
57 table is created. The load factor is a measure of how full the hash table is
58 allowed to get before its capacity is automatically increased. When the number
59 of entries in the hash table exceeds the product of the load factor and the
60 current capacity, the hash table is rehashed (that is, internal data structures
61 are rebuilt) so that the hash table has approximately twice the number of
64 As a general rule, the default load factor (.75) offers a good tradeoff between
65 time and space costs. Higher values decrease the space overhead but increase
66 the lookup cost (reflected in most of the operations of the HashMap class,
67 including get and put). The expected number of entries in the map and its load
68 factor should be taken into account when setting its initial capacity, so as to
69 minimize the number of rehash operations. If the initial capacity is greater
70 than the maximum number of entries divided by the load factor, no rehash
71 operations will ever occur.
73 If many mappings are to be stored in a HashMap instance, creating it with a
74 sufficiently large capacity will allow the mappings to be stored more
75 efficiently than letting it perform automatic rehashing as needed to grow the
78 Note that this implementation is not synchronized. If multiple threads access a
79 hash map concurrently, and at least one of the threads modifies the map
80 structurally, it must be synchronized externally. (A structural modification is
81 any operation that adds or deletes one or more mappings; merely changing the
82 value associated with a key that an instance already contains is not a
83 structural modification.) This is typically accomplished by synchronizing on
84 some object that naturally encapsulates the map.
86 If no such object exists, the map should be "wrapped" using the
87 Collections.synchronizedMap(|java.util.Collections|) method. This is best done
88 at creation time, to prevent accidental unsynchronized access to the map:
90 Map m = Collections.synchronizedMap(new HashMap(...));
92 The iterators returned by all of this class's "collection view methods" are
93 fail-fast: if the map is structurally modified at any time after the iterator
94 is created, in any way except through the iterator's own remove method, the
95 iterator will throw a (|java.util.ConcurrentModificationException|) . Thus, in
96 the face of concurrent modification, the iterator fails quickly and cleanly,
97 rather than risking arbitrary, non-deterministic behavior at an undetermined
100 Note that the fail-fast behavior of an iterator cannot be guaranteed as it is,
101 generally speaking, impossible to make any hard guarantees in the presence of
102 unsynchronized concurrent modification. Fail-fast iterators throw
103 ConcurrentModificationException on a best-effort basis. Therefore, it would be
104 wrong to write a program that depended on this exception for its correctness:
105 the fail-fast behavior of iterators should be used only to detect bugs.
107 This class is a member of the <a
108 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
112 *java.util.HashMap()*
116 Constructs an empty HashMap with the default initial capacity (16) and the
117 default load factor (0.75).
120 *java.util.HashMap(int)*
122 public HashMap(int initialCapacity)
124 Constructs an empty HashMap with the specified initial capacity and the default
127 initialCapacity - the initial capacity.
129 *java.util.HashMap(int,float)*
135 Constructs an empty HashMap with the specified initial capacity and load
138 initialCapacity - the initial capacity
139 loadFactor - the load factor
141 *java.util.HashMap(Map<?extendsK,?extendsV>)*
143 public HashMap(java.util.Map<? extends K, ? extends V> m)
145 Constructs a new HashMap with the same mappings as the specified Map. The
146 HashMap is created with default load factor (0.75) and an initial capacity
147 sufficient to hold the mappings in the specified Map.
149 m - the map whose mappings are to be placed in this map
151 *java.util.HashMap.clear()*
155 Removes all of the mappings from this map. The map will be empty after this
160 *java.util.HashMap.clone()*
162 public |java.lang.Object| clone()
164 Returns a shallow copy of this HashMap instance: the keys and values themselves
169 Returns: a shallow copy of this map
171 *java.util.HashMap.containsKey(Object)*
173 public boolean containsKey(java.lang.Object key)
175 Returns true if this map contains a mapping for the specified key.
178 key - The key whose presence in this map is to be tested
180 Returns: true if this map contains a mapping for the specified key.
182 *java.util.HashMap.containsValue(Object)*
184 public boolean containsValue(java.lang.Object value)
186 Returns true if this map maps one or more keys to the specified value.
189 value - value whose presence in this map is to be tested
191 Returns: true if this map maps one or more keys to the specified value
193 *java.util.HashMap.entrySet()*
195 public |java.util.Set|<Entry<K,V>> entrySet()
197 Returns a (|java.util.Set|) view of the mappings contained in this map. The set
198 is backed by the map, so changes to the map are reflected in the set, and
199 vice-versa. If the map is modified while an iteration over the set is in
200 progress (except through the iterator's own remove operation, or through the
201 setValue operation on a map entry returned by the iterator) the results of the
202 iteration are undefined. The set supports element removal, which removes the
203 corresponding mapping from the map, via the Iterator.remove, Set.remove,
204 removeAll, retainAll and clear operations. It does not support the add or
209 Returns: a set view of the mappings contained in this map
211 *java.util.HashMap.get(Object)*
213 public |V| get(java.lang.Object key)
215 Returns the value to which the specified key is mapped, ornullif this map
216 contains no mapping for the key.
218 More formally, if this map contains a mapping from a keykto a valuevsuch
219 that(key==null ? k==null : key.equals(k)), then this method returnsv; otherwise
220 it returnsnull. (There can be at most one such mapping.)
222 A return value ofnulldoes not necessarily indicate that the map contains no
223 mapping for the key; it's also possible that the map explicitly maps the key
224 tonull. The containsKey(|java.util.HashMap|) operation may be used to
225 distinguish these two cases.
229 *java.util.HashMap.isEmpty()*
231 public boolean isEmpty()
233 Returns true if this map contains no key-value mappings.
237 Returns: true if this map contains no key-value mappings
239 *java.util.HashMap.keySet()*
241 public |java.util.Set|<K> keySet()
243 Returns a (|java.util.Set|) view of the keys contained in this map. The set is
244 backed by the map, so changes to the map are reflected in the set, and
245 vice-versa. If the map is modified while an iteration over the set is in
246 progress (except through the iterator's own remove operation), the results of
247 the iteration are undefined. The set supports element removal, which removes
248 the corresponding mapping from the map, via the Iterator.remove, Set.remove,
249 removeAll, retainAll, and clear operations. It does not support the add or
254 *java.util.HashMap.put(K,V)*
260 Associates the specified value with the specified key in this map. If the map
261 previously contained a mapping for the key, the old value is replaced.
264 key - key with which the specified value is to be associated
265 value - value to be associated with the specified key
267 Returns: the previous value associated with key, or null if there was no mapping for
268 key. (A null return can also indicate that the map previously
269 associated null with key.)
271 *java.util.HashMap.putAll(Map<?extendsK,?extendsV>)*
273 public void putAll(java.util.Map<? extends K, ? extends V> m)
275 Copies all of the mappings from the specified map to this map. These mappings
276 will replace any mappings that this map had for any of the keys currently in
280 m - mappings to be stored in this map
282 *java.util.HashMap.remove(Object)*
284 public |V| remove(java.lang.Object key)
286 Removes the mapping for the specified key from this map if present.
289 key - key whose mapping is to be removed from the map
291 Returns: the previous value associated with key, or null if there was no mapping for
292 key. (A null return can also indicate that the map previously
293 associated null with key.)
295 *java.util.HashMap.size()*
299 Returns the number of key-value mappings in this map.
303 Returns: the number of key-value mappings in this map
305 *java.util.HashMap.values()*
307 public |java.util.Collection|<V> values()
309 Returns a (|java.util.Collection|) view of the values contained in this map.
310 The collection is backed by the map, so changes to the map are reflected in the
311 collection, and vice-versa. If the map is modified while an iteration over the
312 collection is in progress (except through the iterator's own remove operation),
313 the results of the iteration are undefined. The collection supports element
314 removal, which removes the corresponding mapping from the map, via the
315 Iterator.remove, Collection.remove, removeAll, retainAll and clear operations.
316 It does not support the add or addAll operations.