1 *java.util.IdentityHashMap* *IdentityHashMap* This class implements the Map inte
3 public class IdentityHashMap
4 extends |java.util.AbstractMap|
5 implements |java.util.Map|
9 |java.util.IdentityHashMap_Description|
10 |java.util.IdentityHashMap_Fields|
11 |java.util.IdentityHashMap_Constructors|
12 |java.util.IdentityHashMap_Methods|
14 ================================================================================
16 *java.util.IdentityHashMap_Constructors*
17 |java.util.IdentityHashMap()|Constructs a new, empty identity hash map with a d
18 |java.util.IdentityHashMap(int)|Constructs a new, empty map with the specified
19 |java.util.IdentityHashMap(Map)|Constructs a new identity hash map containing t
21 *java.util.IdentityHashMap_Methods*
22 |java.util.IdentityHashMap.clear()|Removes all mappings from this map.
23 |java.util.IdentityHashMap.clone()|Returns a shallow copy of this identity hash
24 |java.util.IdentityHashMap.containsKey(Object)|Tests whether the specified obje
25 |java.util.IdentityHashMap.containsValue(Object)|Tests whether the specified ob
26 |java.util.IdentityHashMap.entrySet()|Returns a set view of the mappings contai
27 |java.util.IdentityHashMap.equals(Object)|Compares the specified object with th
28 |java.util.IdentityHashMap.get(Object)|Returns the value to which the specified
29 |java.util.IdentityHashMap.hashCode()|Returns the hash code value for this map.
30 |java.util.IdentityHashMap.isEmpty()|Returns true if this identity hash map con
31 |java.util.IdentityHashMap.keySet()|Returns an identity-based set view of the k
32 |java.util.IdentityHashMap.put(K,V)|Associates the specified value with the spe
33 |java.util.IdentityHashMap.putAll(Map)|Copies all of the mappings from the spec
34 |java.util.IdentityHashMap.remove(Object)|Removes the mapping for this key from
35 |java.util.IdentityHashMap.size()|Returns the number of key-value mappings in t
36 |java.util.IdentityHashMap.values()|Returns a collection view of the values con
38 *java.util.IdentityHashMap_Description*
40 This class implements the Map interface with a hash table, using
41 reference-equality in place of object-equality when comparing keys (and
42 values). In other words, in an IdentityHashMap, two keys k1 and k2 are
43 considered equal if and only if (k1==k2). (In normal Map implementations (like
44 HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ?
45 k2==null : k1.equals(k2)).)
47 This class is not a general-purpose Map implementation! While this class
48 implements the Map interface, it intentionally violates Map's general contract,
49 which mandates the use of the equals method when comparing objects. This class
50 is designed for use only in the rare cases wherein reference-equality semantics
53 A typical use of this class is topology-preserving object graph
54 transformations, such as serialization or deep-copying. To perform such a
55 transformation, a program must maintain a "node table" that keeps track of all
56 the object references that have already been processed. The node table must not
57 equate distinct objects even if they happen to be equal. Another typical use of
58 this class is to maintain proxy objects. For example, a debugging facility
59 might wish to maintain a proxy object for each object in the program being
62 This class provides all of the optional map operations, and permits null values
63 and the null key. This class makes no guarantees as to the order of the map; in
64 particular, it does not guarantee that the order will remain constant over
67 This class provides constant-time performance for the basic operations (get and
68 put), assuming the system identity hash function ( (|java.lang.System|) )
69 disperses elements properly among the buckets.
71 This class has one tuning parameter (which affects performance but not
72 semantics): expected maximum size. This parameter is the maximum number of
73 key-value mappings that the map is expected to hold. Internally, this parameter
74 is used to determine the number of buckets initially comprising the hash table.
75 The precise relationship between the expected maximum size and the number of
76 buckets is unspecified.
78 If the size of the map (the number of key-value mappings) sufficiently exceeds
79 the expected maximum size, the number of buckets is increased Increasing the
80 number of buckets ("rehashing") may be fairly expensive, so it pays to create
81 identity hash maps with a sufficiently large expected maximum size. On the
82 other hand, iteration over collection views requires time proportional to the
83 number of buckets in the hash table, so it pays not to set the expected maximum
84 size too high if you are especially concerned with iteration performance or
87 Note that this implementation is not synchronized. If multiple threads access
88 this map concurrently, and at least one of the threads modifies the map
89 structurally, it must be synchronized externally. (A structural modification is
90 any operation that adds or deletes one or more mappings; merely changing the
91 value associated with a key that an instance already contains is not a
92 structural modification.) This is typically accomplished by synchronizing on
93 some object that naturally encapsulates the map. If no such object exists, the
94 map should be "wrapped" using the Collections.synchronizedMap method. This is
95 best done at creation time, to prevent accidental unsynchronized access to the
98 Map m = Collections.synchronizedMap(new HashMap(...));
100 The iterators returned by all of this class's "collection view methods" are
101 fail-fast: if the map is structurally modified at any time after the iterator
102 is created, in any way except through the iterator's own remove or add methods,
103 the iterator will throw a ConcurrentModificationException. Thus, in the face of
104 concurrent modification, the iterator fails quickly and cleanly, rather than
105 risking arbitrary, non-deterministic behavior at an undetermined time in the
108 Note that the fail-fast behavior of an iterator cannot be guaranteed as it is,
109 generally speaking, impossible to make any hard guarantees in the presence of
110 unsynchronized concurrent modification. Fail-fast iterators throw
111 ConcurrentModificationException on a best-effort basis. Therefore, it would be
112 wrong to write a program that depended on this exception for its correctness:
113 fail-fast iterators should be used only to detect bugs.
115 Implementation note: This is a simple linear-probe hash table, as described for
116 example in texts by Sedgewick and Knuth. The array alternates holding keys and
117 values. (This has better locality for large tables than does using separate
118 arrays.) For many JRE implementations and operation mixes, this class will
119 yield better performance than (|java.util.HashMap|) (which uses chaining rather
120 than linear-probing).
122 This class is a member of the <a href="/../guide/collections/index.html"> Java
123 Collections Framework.
126 *java.util.IdentityHashMap()*
128 public IdentityHashMap()
130 Constructs a new, empty identity hash map with a default expected maximum size
134 *java.util.IdentityHashMap(int)*
136 public IdentityHashMap(int expectedMaxSize)
138 Constructs a new, empty map with the specified expected maximum size. Putting
139 more than the expected number of key-value mappings into the map may cause the
140 internal data structure to grow, which may be somewhat time-consuming.
142 expectedMaxSize - the expected maximum size of the map.
144 *java.util.IdentityHashMap(Map)*
146 public IdentityHashMap(java.util.Map m)
148 Constructs a new identity hash map containing the keys-value mappings in the
151 m - the map whose mappings are to be placed into this map.
153 *java.util.IdentityHashMap.clear()*
157 Removes all mappings from this map.
160 *java.util.IdentityHashMap.clone()*
162 public |java.lang.Object| clone()
164 Returns a shallow copy of this identity hash map: the keys and values
165 themselves are not cloned.
168 Returns: a shallow copy of this map.
169 *java.util.IdentityHashMap.containsKey(Object)*
171 public boolean containsKey(java.lang.Object key)
173 Tests whether the specified object reference is a key in this identity hash
178 Returns: true if the specified object reference is a key in this map.
179 *java.util.IdentityHashMap.containsValue(Object)*
181 public boolean containsValue(java.lang.Object value)
183 Tests whether the specified object reference is a value in this identity hash
186 value - value whose presence in this map is to be tested.
188 Returns: true if this map maps one or more keys to the specified object reference.
189 *java.util.IdentityHashMap.entrySet()*
191 public |java.util.Set| entrySet()
193 Returns a set view of the mappings contained in this map. Each element in the
194 returned set is a reference-equality-based Map.Entry. The set is backed by the
195 map, so changes to the map are reflected in the set, and vice-versa. If the map
196 is modified while an iteration over the set is in progress, the results of the
197 iteration are undefined. The set supports element removal, which removes the
198 corresponding mapping from the map, via the Iterator.remove, Set.remove,
199 removeAll, retainAll and clear methods. It does not support the add or addAll
202 Like the backing map, the Map.Entry objects in the set returned by this method
203 define key and value equality as reference-equality rather than
204 object-equality. This affects the behavior of the equals and hashCode methods
205 of these Map.Entry objects. A reference-equality based Map.Entry e is equal to
206 an object o if and only if o is a Map.Entry and e.getKey()==o.getKey()
207 e.getValue()==o.getValue(). To accommodate these equals semantics, the hashCode
208 method returns System.identityHashCode(e.getKey()) ^
209 System.identityHashCode(e.getValue()).
211 Owing to the reference-equality-based semantics of the Map.Entry instances in
212 the set returned by this method, it is possible that the symmetry and
213 transitivity requirements of the (|java.lang.Object|) contract may be violated
214 if any of the entries in the set is compared to a normal map entry, or if the
215 set returned by this method is compared to a set of normal map entries (such as
216 would be returned by a call to this method on a normal map). However, the
217 Object.equals contract is guaranteed to hold among identity-based map entries,
218 and among sets of such entries.
221 Returns: a set view of the identity-mappings contained in this map.
222 *java.util.IdentityHashMap.equals(Object)*
224 public boolean equals(java.lang.Object o)
226 Compares the specified object with this map for equality. Returns true if the
227 given object is also a map and the two maps represent identical
228 object-reference mappings. More formally, this map is equal to another map m if
229 and only if map this.entrySet().equals(m.entrySet()).
231 Owing to the reference-equality-based semantics of this map it is possible that
232 the symmetry and transitivity requirements of the Object.equals contract may be
233 violated if this map is compared to a normal map. However, the Object.equals
234 contract is guaranteed to hold among IdentityHashMap instances.
236 o - object to be compared for equality with this map.
238 Returns: true if the specified object is equal to this map.
239 *java.util.IdentityHashMap.get(Object)*
241 public |java.lang.Object| get(java.lang.Object key)
243 Returns the value to which the specified key is mapped in this identity hash
244 map, or null if the map contains no mapping for this key. A return value of
245 null does not necessarily indicate that the map contains no mapping for the
246 key; it is also possible that the map explicitly maps the key to null. The
247 containsKey method may be used to distinguish these two cases.
249 key - the key whose associated value is to be returned.
251 Returns: the value to which this map maps the specified key, or null if the map contains
252 no mapping for this key.
253 *java.util.IdentityHashMap.hashCode()*
255 public int hashCode()
257 Returns the hash code value for this map. The hash code of a map is defined to
258 be the sum of the hashcode of each entry in the map's entrySet view. This
259 ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any
260 two IdentityHashMap instances t1 and t2, as required by the general contract of
261 (|java.lang.Object|) .
263 Owing to the reference-equality-based semantics of the Map.Entry instances in
264 the set returned by this map's entrySet method, it is possible that the
265 contractual requirement of Object.hashCode mentioned in the previous paragraph
266 will be violated if one of the two objects being compared is an IdentityHashMap
267 instance and the other is a normal map.
270 Returns: the hash code value for this map.
271 *java.util.IdentityHashMap.isEmpty()*
273 public boolean isEmpty()
275 Returns true if this identity hash map contains no key-value mappings.
278 Returns: true if this identity hash map contains no key-value mappings.
279 *java.util.IdentityHashMap.keySet()*
281 public |java.util.Set| keySet()
283 Returns an identity-based set view of the keys contained in this map. The set
284 is backed by the map, so changes to the map are reflected in the set, and
285 vice-versa. If the map is modified while an iteration over the set is in
286 progress, the results of the iteration are undefined. The set supports element
287 removal, which removes the corresponding mapping from the map, via the
288 Iterator.remove, Set.remove, removeAll retainAll, and clear methods. It does
289 not support the add or addAll methods.
291 While the object returned by this method implements the Set interface, it does
292 not obey Set's general contract. Like its backing map, the set returned by this
293 method defines element equality as reference-equality rather than
294 object-equality. This affects the behavior of its contains, remove,
295 containsAll, equals, and hashCode methods.
297 The equals method of the returned set returns true only if the specified object
298 is a set containing exactly the same object references as the returned set. The
299 symmetry and transitivity requirements of the Object.equals contract may be
300 violated if the set returned by this method is compared to a normal set.
301 However, the Object.equals contract is guaranteed to hold among sets returned
304 The hashCode method of the returned set returns the sum of the identity
305 hashcodes of the elements in the set, rather than the sum of their hashcodes.
306 This is mandated by the change in the semantics of the equals method, in order
307 to enforce the general contract of the Object.hashCode method among sets
308 returned by this method.
311 Returns: an identity-based set view of the keys contained in this map.
312 *java.util.IdentityHashMap.put(K,V)*
314 public |java.lang.Object| put(
315 java.lang.Object key,
316 java.lang.Object value)
318 Associates the specified value with the specified key in this identity hash
319 map. If the map previously contained a mapping for this key, the old value is
322 key - the key with which the specified value is to be associated.
323 value - the value to be associated with the specified key.
325 Returns: the previous value associated with key, or null if there was no mapping for
326 key. (A null return can also indicate that the map previously
327 associated null with the specified key.)
328 *java.util.IdentityHashMap.putAll(Map)*
330 public void putAll(java.util.Map t)
332 Copies all of the mappings from the specified map to this map These mappings
333 will replace any mappings that this map had for any of the keys currently in
336 t - mappings to be stored in this map.
338 *java.util.IdentityHashMap.remove(Object)*
340 public |java.lang.Object| remove(java.lang.Object key)
342 Removes the mapping for this key from this map if present.
344 key - key whose mapping is to be removed from the map.
346 Returns: previous value associated with specified key, or null if there was no entry for
347 key. (A null return can also indicate that the map previously
348 associated null with the specified key.)
349 *java.util.IdentityHashMap.size()*
353 Returns the number of key-value mappings in this identity hash map.
356 Returns: the number of key-value mappings in this map.
357 *java.util.IdentityHashMap.values()*
359 public |java.util.Collection| values()
361 Returns a collection view of the values contained in this map. The collection
362 is backed by the map, so changes to the map are reflected in the collection,
363 and vice-versa. If the map is modified while an iteration over the collection
364 is in progress, the results of the iteration are undefined. The collection
365 supports element removal, which removes the corresponding mapping from the map,
366 via the Iterator.remove, Collection.remove, removeAll, retainAll and clear
367 methods. It does not support the add or addAll methods.
369 While the object returned by this method implements the Collection interface,
370 it does not obey Collection's general contract. Like its backing map, the
371 collection returned by this method defines element equality as
372 reference-equality rather than object-equality. This affects the behavior of
373 its contains, remove and containsAll methods.
376 Returns: a collection view of the values contained in this map.