1 *java.util.concurrent.ConcurrentHashMap* *ConcurrentHashMap* A hash table suppor
3 public class ConcurrentHashMap<K,V>
4 extends |java.util.AbstractMap|
5 implements |java.util.concurrent.ConcurrentMap|
8 |java.util.concurrent.ConcurrentHashMap_Description|
9 |java.util.concurrent.ConcurrentHashMap_Fields|
10 |java.util.concurrent.ConcurrentHashMap_Constructors|
11 |java.util.concurrent.ConcurrentHashMap_Methods|
13 ================================================================================
15 *java.util.concurrent.ConcurrentHashMap_Constructors*
16 |java.util.concurrent.ConcurrentHashMap()|Creates a new, empty map with a defau
17 |java.util.concurrent.ConcurrentHashMap(int)|Creates a new, empty map with the
18 |java.util.concurrent.ConcurrentHashMap(int,float)|Creates a new, empty map wit
19 |java.util.concurrent.ConcurrentHashMap(int,float,int)|Creates a new, empty map
20 |java.util.concurrent.ConcurrentHashMap(Map<?extendsK,?extendsV>)|Creates a new
22 *java.util.concurrent.ConcurrentHashMap_Methods*
23 |java.util.concurrent.ConcurrentHashMap.clear()|Removes all of the mappings fro
24 |java.util.concurrent.ConcurrentHashMap.contains(Object)|Legacy method testing
25 |java.util.concurrent.ConcurrentHashMap.containsKey(Object)|Tests if the specif
26 |java.util.concurrent.ConcurrentHashMap.containsValue(Object)|Returns true if t
27 |java.util.concurrent.ConcurrentHashMap.elements()|Returns an enumeration of th
28 |java.util.concurrent.ConcurrentHashMap.entrySet()|Returns aSetview of the mapp
29 |java.util.concurrent.ConcurrentHashMap.get(Object)|Returns the value to which
30 |java.util.concurrent.ConcurrentHashMap.isEmpty()|Returns true if this map cont
31 |java.util.concurrent.ConcurrentHashMap.keys()|Returns an enumeration of the ke
32 |java.util.concurrent.ConcurrentHashMap.keySet()|Returns aSetview of the keys c
33 |java.util.concurrent.ConcurrentHashMap.put(K,V)|Maps the specified key to the
34 |java.util.concurrent.ConcurrentHashMap.putAll(Map<?extendsK,?extendsV>)|Copies
35 |java.util.concurrent.ConcurrentHashMap.putIfAbsent(K,V)|
36 |java.util.concurrent.ConcurrentHashMap.remove(Object)|Removes the key (and its
37 |java.util.concurrent.ConcurrentHashMap.remove(Object,Object)|
38 |java.util.concurrent.ConcurrentHashMap.replace(K,V)|
39 |java.util.concurrent.ConcurrentHashMap.replace(K,V,V)|
40 |java.util.concurrent.ConcurrentHashMap.size()|Returns the number of key-value
41 |java.util.concurrent.ConcurrentHashMap.values()|Returns aCollectionview of the
43 *java.util.concurrent.ConcurrentHashMap_Description*
45 A hash table supporting full concurrency of retrievals and adjustable expected
46 concurrency for updates. This class obeys the same functional specification as
47 (|java.util.Hashtable|) , and includes versions of methods corresponding to
48 each method of Hashtable. However, even though all operations are thread-safe,
49 retrieval operations do not entail locking, and there is not any support for
50 locking the entire table in a way that prevents all access. This class is fully
51 interoperable with Hashtable in programs that rely on its thread safety but not
52 on its synchronization details.
54 Retrieval operations (including get) generally do not block, so may overlap
55 with update operations (including put and remove). Retrievals reflect the
56 results of the most recently completed update operations holding upon their
57 onset. For aggregate operations such as putAll and clear, concurrent retrievals
58 may reflect insertion or removal of only some entries. Similarly, Iterators and
59 Enumerations return elements reflecting the state of the hash table at some
60 point at or since the creation of the iterator/enumeration. They do not throw
61 (|java.util.ConcurrentModificationException|) . However, iterators are designed
62 to be used by only one thread at a time.
64 The allowed concurrency among update operations is guided by the optional
65 concurrencyLevel constructor argument (default 16), which is used as a hint for
66 internal sizing. The table is internally partitioned to try to permit the
67 indicated number of concurrent updates without contention. Because placement in
68 hash tables is essentially random, the actual concurrency will vary. Ideally,
69 you should choose a value to accommodate as many threads as will ever
70 concurrently modify the table. Using a significantly higher value than you need
71 can waste space and time, and a significantly lower value can lead to thread
72 contention. But overestimates and underestimates within an order of magnitude
73 do not usually have much noticeable impact. A value of one is appropriate when
74 it is known that only one thread will modify and all others will only read.
75 Also, resizing this or any other kind of hash table is a relatively slow
76 operation, so, when possible, it is a good idea to provide estimates of
77 expected table sizes in constructors.
79 This class and its views and iterators implement all of the optional methods of
80 the (|java.util.Map|) and (|java.util.Iterator|) interfaces.
82 Like (|java.util.Hashtable|) but unlike (|java.util.HashMap|) , this class does
83 not allow null to be used as a key or value.
85 This class is a member of the <a
86 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
90 *java.util.concurrent.ConcurrentHashMap()*
92 public ConcurrentHashMap()
94 Creates a new, empty map with a default initial capacity (16), load factor
95 (0.75) and concurrencyLevel (16).
98 *java.util.concurrent.ConcurrentHashMap(int)*
100 public ConcurrentHashMap(int initialCapacity)
102 Creates a new, empty map with the specified initial capacity, and with default
103 load factor (0.75) and concurrencyLevel (16).
105 initialCapacity - the initial capacity. The implementation performs internal sizing to
106 accommodate this many elements.
108 *java.util.concurrent.ConcurrentHashMap(int,float)*
110 public ConcurrentHashMap(
114 Creates a new, empty map with the specified initial capacity and load factor
115 and with the default concurrencyLevel (16).
117 initialCapacity - The implementation performs internal sizing to accommodate this many elements.
118 loadFactor - the load factor threshold, used to control resizing. Resizing may be performed
119 when the average number of elements per bin exceeds this threshold.
121 *java.util.concurrent.ConcurrentHashMap(int,float,int)*
123 public ConcurrentHashMap(
126 int concurrencyLevel)
128 Creates a new, empty map with the specified initial capacity, load factor and
131 initialCapacity - the initial capacity. The implementation performs internal sizing to
132 accommodate this many elements.
133 loadFactor - the load factor threshold, used to control resizing. Resizing may be performed
134 when the average number of elements per bin exceeds this threshold.
135 concurrencyLevel - the estimated number of concurrently updating threads. The implementation
136 performs internal sizing to try to accommodate this many threads.
138 *java.util.concurrent.ConcurrentHashMap(Map<?extendsK,?extendsV>)*
140 public ConcurrentHashMap(java.util.Map<? extends K, ? extends V> m)
142 Creates a new map with the same mappings as the given map. The map is created
143 with a capacity of 1.5 times the number of mappings in the given map or 16
144 (whichever is greater), and a default load factor (0.75) and concurrencyLevel
149 *java.util.concurrent.ConcurrentHashMap.clear()*
153 Removes all of the mappings from this map.
157 *java.util.concurrent.ConcurrentHashMap.contains(Object)*
159 public boolean contains(java.lang.Object value)
161 Legacy method testing if some key maps into the specified value in this table.
162 This method is identical in functionality to
163 (|java.util.concurrent.ConcurrentHashMap|) , and exists solely to ensure full
164 compatibility with class (|java.util.Hashtable|) , which supported this method
165 prior to introduction of the Java Collections framework.
168 value - a value to search for
170 Returns: true if and only if some key maps to the value argument in this table as
171 determined by the equals method; false otherwise
173 *java.util.concurrent.ConcurrentHashMap.containsKey(Object)*
175 public boolean containsKey(java.lang.Object key)
177 Tests if the specified object is a key in this table.
182 Returns: true if and only if the specified object is a key in this table, as determined
183 by the equals method; false otherwise.
185 *java.util.concurrent.ConcurrentHashMap.containsValue(Object)*
187 public boolean containsValue(java.lang.Object value)
189 Returns true if this map maps one or more keys to the specified value. Note:
190 This method requires a full internal traversal of the hash table, and so is
191 much slower than method containsKey.
194 value - value whose presence in this map is to be tested
196 Returns: true if this map maps one or more keys to the specified value
198 *java.util.concurrent.ConcurrentHashMap.elements()*
200 public |java.util.Enumeration|<V> elements()
202 Returns an enumeration of the values in this table.
206 Returns: an enumeration of the values in this table
208 *java.util.concurrent.ConcurrentHashMap.entrySet()*
210 public |java.util.Set|<Entry<K,V>> entrySet()
212 Returns a (|java.util.Set|) view of the mappings contained in this map. The set
213 is backed by the map, so changes to the map are reflected in the set, and
214 vice-versa. The set supports element removal, which removes the corresponding
215 mapping from the map, via the Iterator.remove, Set.remove, removeAll,
216 retainAll, and clear operations. It does not support the add or addAll
219 The view's iterator is a "weakly consistent" iterator that will never throw
220 (|java.util.ConcurrentModificationException|) , and guarantees to traverse
221 elements as they existed upon construction of the iterator, and may (but is not
222 guaranteed to) reflect any modifications subsequent to construction.
226 *java.util.concurrent.ConcurrentHashMap.get(Object)*
228 public |V| get(java.lang.Object key)
230 Returns the value to which the specified key is mapped, ornullif this map
231 contains no mapping for the key.
233 More formally, if this map contains a mapping from a keykto a valuevsuch
234 thatkey.equals(k), then this method returnsv; otherwise it returnsnull. (There
235 can be at most one such mapping.)
239 *java.util.concurrent.ConcurrentHashMap.isEmpty()*
241 public boolean isEmpty()
243 Returns true if this map contains no key-value mappings.
247 Returns: true if this map contains no key-value mappings
249 *java.util.concurrent.ConcurrentHashMap.keys()*
251 public |java.util.Enumeration|<K> keys()
253 Returns an enumeration of the keys in this table.
257 Returns: an enumeration of the keys in this table
259 *java.util.concurrent.ConcurrentHashMap.keySet()*
261 public |java.util.Set|<K> keySet()
263 Returns a (|java.util.Set|) view of the keys contained in this map. The set is
264 backed by the map, so changes to the map are reflected in the set, and
265 vice-versa. The set supports element removal, which removes the corresponding
266 mapping from this map, via the Iterator.remove, Set.remove, removeAll,
267 retainAll, and clear operations. It does not support the add or addAll
270 The view's iterator is a "weakly consistent" iterator that will never throw
271 (|java.util.ConcurrentModificationException|) , and guarantees to traverse
272 elements as they existed upon construction of the iterator, and may (but is not
273 guaranteed to) reflect any modifications subsequent to construction.
277 *java.util.concurrent.ConcurrentHashMap.put(K,V)*
283 Maps the specified key to the specified value in this table. Neither the key
284 nor the value can be null.
286 The value can be retrieved by calling the get method with a key that is equal
290 key - key with which the specified value is to be associated
291 value - value to be associated with the specified key
293 Returns: the previous value associated with key, or null if there was no mapping for key
295 *java.util.concurrent.ConcurrentHashMap.putAll(Map<?extendsK,?extendsV>)*
297 public void putAll(java.util.Map<? extends K, ? extends V> m)
299 Copies all of the mappings from the specified map to this one. These mappings
300 replace any mappings that this map had for any of the keys currently in the
304 m - mappings to be stored in this map
306 *java.util.concurrent.ConcurrentHashMap.putIfAbsent(K,V)*
308 public |V| putIfAbsent(
316 Returns: the previous value associated with the specified key, or null if there was no
319 *java.util.concurrent.ConcurrentHashMap.remove(Object)*
321 public |V| remove(java.lang.Object key)
323 Removes the key (and its corresponding value) from this map. This method does
324 nothing if the key is not in the map.
327 key - the key that needs to be removed
329 Returns: the previous value associated with key, or null if there was no mapping for key
331 *java.util.concurrent.ConcurrentHashMap.remove(Object,Object)*
333 public boolean remove(
334 java.lang.Object key,
335 java.lang.Object value)
341 *java.util.concurrent.ConcurrentHashMap.replace(K,V)*
351 Returns: the previous value associated with the specified key, or null if there was no
354 *java.util.concurrent.ConcurrentHashMap.replace(K,V,V)*
356 public boolean replace(
365 *java.util.concurrent.ConcurrentHashMap.size()*
369 Returns the number of key-value mappings in this map. If the map contains more
370 than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
374 Returns: the number of key-value mappings in this map
376 *java.util.concurrent.ConcurrentHashMap.values()*
378 public |java.util.Collection|<V> values()
380 Returns a (|java.util.Collection|) view of the values contained in this map.
381 The collection is backed by the map, so changes to the map are reflected in the
382 collection, and vice-versa. The collection supports element removal, which
383 removes the corresponding mapping from this map, via the Iterator.remove,
384 Collection.remove, removeAll, retainAll, and clear operations. It does not
385 support the add or addAll operations.
387 The view's iterator is a "weakly consistent" iterator that will never throw
388 (|java.util.ConcurrentModificationException|) , and guarantees to traverse
389 elements as they existed upon construction of the iterator, and may (but is not
390 guaranteed to) reflect any modifications subsequent to construction.