1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
10 |
|
|
11 |
|
/** |
12 |
|
* This class provides a skeletal implementation of the <tt>Map</tt> |
13 |
< |
* interface, to minimize the effort required to implement this interface. <p> |
13 |
> |
* interface, to minimize the effort required to implement this interface. |
14 |
|
* |
15 |
< |
* To implement an unmodifiable map, the programmer needs only to extend this |
15 |
> |
* <p>To implement an unmodifiable map, the programmer needs only to extend this |
16 |
|
* class and provide an implementation for the <tt>entrySet</tt> method, which |
17 |
|
* returns a set-view of the map's mappings. Typically, the returned set |
18 |
|
* will, in turn, be implemented atop <tt>AbstractSet</tt>. This set should |
19 |
|
* not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator |
20 |
< |
* should not support the <tt>remove</tt> method.<p> |
20 |
> |
* should not support the <tt>remove</tt> method. |
21 |
|
* |
22 |
< |
* To implement a modifiable map, the programmer must additionally override |
22 |
> |
* <p>To implement a modifiable map, the programmer must additionally override |
23 |
|
* this class's <tt>put</tt> method (which otherwise throws an |
24 |
|
* <tt>UnsupportedOperationException</tt>), and the iterator returned by |
25 |
|
* <tt>entrySet().iterator()</tt> must additionally implement its |
26 |
< |
* <tt>remove</tt> method.<p> |
26 |
> |
* <tt>remove</tt> method. |
27 |
|
* |
28 |
< |
* The programmer should generally provide a void (no argument) and map |
28 |
> |
* <p>The programmer should generally provide a void (no argument) and map |
29 |
|
* constructor, as per the recommendation in the <tt>Map</tt> interface |
30 |
< |
* specification.<p> |
30 |
> |
* specification. |
31 |
|
* |
32 |
< |
* The documentation for each non-abstract methods in this class describes its |
32 |
> |
* <p>The documentation for each non-abstract methods in this class describes its |
33 |
|
* implementation in detail. Each of these methods may be overridden if the |
34 |
< |
* map being implemented admits a more efficient implementation.<p> |
34 |
> |
* map being implemented admits a more efficient implementation. |
35 |
|
* |
36 |
< |
* This class is a member of the |
36 |
> |
* <p>This class is a member of the |
37 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
38 |
|
* Java Collections Framework</a>. |
39 |
|
* |
59 |
|
// Query Operations |
60 |
|
|
61 |
|
/** |
62 |
< |
* Returns the number of key-value mappings in this map. If the map |
63 |
< |
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
64 |
< |
* <tt>Integer.MAX_VALUE</tt>.<p> |
62 |
> |
* {@inheritDoc} |
63 |
|
* |
64 |
< |
* This implementation returns <tt>entrySet().size()</tt>. |
67 |
< |
* |
68 |
< |
* @return the number of key-value mappings in this map. |
64 |
> |
* <p>This implementation returns <tt>entrySet().size()</tt>. |
65 |
|
*/ |
66 |
|
public int size() { |
67 |
|
return entrySet().size(); |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* Returns <tt>true</tt> if this map contains no key-value mappings. <p> |
76 |
< |
* |
77 |
< |
* This implementation returns <tt>size() == 0</tt>. |
71 |
> |
* {@inheritDoc} |
72 |
|
* |
73 |
< |
* @return <tt>true</tt> if this map contains no key-value mappings. |
73 |
> |
* <p>This implementation returns <tt>size() == 0</tt>. |
74 |
|
*/ |
75 |
|
public boolean isEmpty() { |
76 |
|
return size() == 0; |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
< |
* Returns <tt>true</tt> if this map maps one or more keys to this value. |
87 |
< |
* More formally, returns <tt>true</tt> if and only if this map contains |
88 |
< |
* at least one mapping to a value <tt>v</tt> such that <tt>(value==null ? |
89 |
< |
* v==null : value.equals(v))</tt>. This operation will probably require |
90 |
< |
* time linear in the map size for most implementations of <tt>Map</tt>.<p> |
91 |
< |
* |
92 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an entry |
93 |
< |
* with the specified value. If such an entry is found, <tt>true</tt> is |
94 |
< |
* returned. If the iteration terminates without finding such an entry, |
95 |
< |
* <tt>false</tt> is returned. Note that this implementation requires |
96 |
< |
* linear time in the size of the map. |
80 |
> |
* {@inheritDoc} |
81 |
|
* |
82 |
< |
* @param value value whose presence in this map is to be tested. |
82 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching |
83 |
> |
* for an entry with the specified value. If such an entry is found, |
84 |
> |
* <tt>true</tt> is returned. If the iteration terminates without |
85 |
> |
* finding such an entry, <tt>false</tt> is returned. Note that this |
86 |
> |
* implementation requires linear time in the size of the map. |
87 |
|
* |
88 |
< |
* @return <tt>true</tt> if this map maps one or more keys to this value. |
88 |
> |
* @throws ClassCastException {@inheritDoc} |
89 |
> |
* @throws NullPointerException {@inheritDoc} |
90 |
|
*/ |
91 |
|
public boolean containsValue(Object value) { |
92 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* Returns <tt>true</tt> if this map contains a mapping for the specified |
122 |
< |
* key. <p> |
110 |
> |
* {@inheritDoc} |
111 |
|
* |
112 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an |
113 |
< |
* entry with the specified key. If such an entry is found, <tt>true</tt> |
114 |
< |
* is returned. If the iteration terminates without finding such an |
115 |
< |
* entry, <tt>false</tt> is returned. Note that this implementation |
116 |
< |
* requires linear time in the size of the map; many implementations will |
117 |
< |
* override this method. |
118 |
< |
* |
119 |
< |
* @param key key whose presence in this map is to be tested. |
120 |
< |
* @return <tt>true</tt> if this map contains a mapping for the specified |
133 |
< |
* key. |
134 |
< |
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt> |
135 |
< |
* and this map does not permit <tt>null</tt> keys. |
112 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching |
113 |
> |
* for an entry with the specified key. If such an entry is found, |
114 |
> |
* <tt>true</tt> is returned. If the iteration terminates without |
115 |
> |
* finding such an entry, <tt>false</tt> is returned. Note that this |
116 |
> |
* implementation requires linear time in the size of the map; many |
117 |
> |
* implementations will override this method. |
118 |
> |
* |
119 |
> |
* @throws ClassCastException {@inheritDoc} |
120 |
> |
* @throws NullPointerException {@inheritDoc} |
121 |
|
*/ |
122 |
|
public boolean containsKey(Object key) { |
123 |
|
Iterator<Map.Entry<K,V>> i = entrySet().iterator(); |
138 |
|
} |
139 |
|
|
140 |
|
/** |
141 |
< |
* Returns the value to which this map maps the specified key. Returns |
157 |
< |
* <tt>null</tt> if the map contains no mapping for this key. A return |
158 |
< |
* value of <tt>null</tt> does not <i>necessarily</i> indicate that the |
159 |
< |
* map contains no mapping for the key; it's also possible that the map |
160 |
< |
* explicitly maps the key to <tt>null</tt>. The containsKey operation |
161 |
< |
* may be used to distinguish these two cases. <p> |
162 |
< |
* |
163 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an |
164 |
< |
* entry with the specified key. If such an entry is found, the entry's |
165 |
< |
* value is returned. If the iteration terminates without finding such an |
166 |
< |
* entry, <tt>null</tt> is returned. Note that this implementation |
167 |
< |
* requires linear time in the size of the map; many implementations will |
168 |
< |
* override this method. |
169 |
< |
* |
170 |
< |
* @param key key whose associated value is to be returned. |
171 |
< |
* @return the value to which this map maps the specified key. |
141 |
> |
* {@inheritDoc} |
142 |
|
* |
143 |
< |
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt> |
144 |
< |
* and this map does not permit <tt>null</tt> keys. |
143 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching |
144 |
> |
* for an entry with the specified key. If such an entry is found, |
145 |
> |
* the entry's value is returned. If the iteration terminates without |
146 |
> |
* finding such an entry, <tt>null</tt> is returned. Note that this |
147 |
> |
* implementation requires linear time in the size of the map; many |
148 |
> |
* implementations will override this method. |
149 |
|
* |
150 |
< |
* @see #containsKey(Object) |
150 |
> |
* @throws ClassCastException {@inheritDoc} |
151 |
> |
* @throws NullPointerException {@inheritDoc} |
152 |
|
*/ |
153 |
|
public V get(Object key) { |
154 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
172 |
|
// Modification Operations |
173 |
|
|
174 |
|
/** |
175 |
< |
* Associates the specified value with the specified key in this map |
201 |
< |
* (optional operation). If the map previously contained a mapping for |
202 |
< |
* this key, the old value is replaced.<p> |
175 |
> |
* {@inheritDoc} |
176 |
|
* |
177 |
< |
* This implementation always throws an |
177 |
> |
* <p>This implementation always throws an |
178 |
|
* <tt>UnsupportedOperationException</tt>. |
179 |
|
* |
180 |
< |
* @param key key with which the specified value is to be associated. |
181 |
< |
* @param value value to be associated with the specified key. |
182 |
< |
* |
183 |
< |
* @return the previous value associated with specified key, or <tt>null</tt> |
211 |
< |
* if there was no mapping for key. (A <tt>null</tt> return can |
212 |
< |
* also indicate that the map previously associated <tt>null</tt> |
213 |
< |
* with the specified key, if the implementation supports |
214 |
< |
* <tt>null</tt> values.) |
215 |
< |
* |
216 |
< |
* @throws UnsupportedOperationException if the <tt>put</tt> operation |
217 |
< |
* is not supported by this map. |
218 |
< |
* @throws ClassCastException if the class of the specified key or value |
219 |
< |
* prevents it from being stored in this map. |
220 |
< |
* @throws IllegalArgumentException if some aspect of this key or value |
221 |
< |
* prevents it from being stored in this map. |
222 |
< |
* @throws NullPointerException if this map does not permit <tt>null</tt> |
223 |
< |
* keys or values, and the specified key or value is <tt>null</tt>. |
180 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
181 |
> |
* @throws ClassCastException {@inheritDoc} |
182 |
> |
* @throws NullPointerException {@inheritDoc} |
183 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
184 |
|
*/ |
185 |
|
public V put(K key, V value) { |
186 |
|
throw new UnsupportedOperationException(); |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
< |
* Removes the mapping for this key from this map if present (optional |
231 |
< |
* operation). <p> |
190 |
> |
* {@inheritDoc} |
191 |
|
* |
192 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an |
192 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching for an |
193 |
|
* entry with the specified key. If such an entry is found, its value is |
194 |
|
* obtained with its <tt>getValue</tt> operation, the entry is removed |
195 |
|
* from the collection (and the backing map) with the iterator's |
196 |
|
* <tt>remove</tt> operation, and the saved value is returned. If the |
197 |
|
* iteration terminates without finding such an entry, <tt>null</tt> is |
198 |
|
* returned. Note that this implementation requires linear time in the |
199 |
< |
* size of the map; many implementations will override this method.<p> |
199 |
> |
* size of the map; many implementations will override this method. |
200 |
> |
* |
201 |
> |
* <p>Note that this implementation throws an |
202 |
> |
* <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> |
203 |
> |
* iterator does not support the <tt>remove</tt> method and this map |
204 |
> |
* contains a mapping for the specified key. |
205 |
|
* |
206 |
< |
* Note that this implementation throws an |
207 |
< |
* <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> iterator |
208 |
< |
* does not support the <tt>remove</tt> method and this map contains a |
245 |
< |
* mapping for the specified key. |
246 |
< |
* |
247 |
< |
* @param key key whose mapping is to be removed from the map. |
248 |
< |
* @return the previous value associated with specified key, or <tt>null</tt> |
249 |
< |
* if there was no entry for key. (A <tt>null</tt> return can |
250 |
< |
* also indicate that the map previously associated <tt>null</tt> |
251 |
< |
* with the specified key, if the implementation supports |
252 |
< |
* <tt>null</tt> values.) |
253 |
< |
* @throws UnsupportedOperationException if the <tt>remove</tt> operation |
254 |
< |
* is not supported by this map. |
206 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
207 |
> |
* @throws ClassCastException {@inheritDoc} |
208 |
> |
* @throws NullPointerException {@inheritDoc} |
209 |
|
*/ |
210 |
|
public V remove(Object key) { |
211 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
236 |
|
// Bulk Operations |
237 |
|
|
238 |
|
/** |
239 |
< |
* Copies all of the mappings from the specified map to this map |
286 |
< |
* (optional operation). These mappings will replace any mappings that |
287 |
< |
* this map had for any of the keys currently in the specified map.<p> |
239 |
> |
* {@inheritDoc} |
240 |
|
* |
241 |
< |
* This implementation iterates over the specified map's |
241 |
> |
* <p>This implementation iterates over the specified map's |
242 |
|
* <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt> |
243 |
< |
* operation once for each entry returned by the iteration.<p> |
243 |
> |
* operation once for each entry returned by the iteration. |
244 |
|
* |
245 |
< |
* Note that this implementation throws an |
245 |
> |
* <p>Note that this implementation throws an |
246 |
|
* <tt>UnsupportedOperationException</tt> if this map does not support |
247 |
|
* the <tt>put</tt> operation and the specified map is nonempty. |
248 |
|
* |
249 |
< |
* @param t mappings to be stored in this map. |
250 |
< |
* |
251 |
< |
* @throws UnsupportedOperationException if the <tt>putAll</tt> operation |
252 |
< |
* is not supported by this map. |
301 |
< |
* @throws ClassCastException if the class of a key or value in the |
302 |
< |
* specified map prevents it from being stored in this map. |
303 |
< |
* @throws IllegalArgumentException if some aspect of a key or value in |
304 |
< |
* the specified map prevents it from being stored in this map. |
305 |
< |
* @throws NullPointerException if the specified map is <tt>null</tt>, or if |
306 |
< |
* this map does not permit <tt>null</tt> keys or values, and the |
307 |
< |
* specified map contains <tt>null</tt> keys or values. |
249 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
250 |
> |
* @throws ClassCastException {@inheritDoc} |
251 |
> |
* @throws NullPointerException {@inheritDoc} |
252 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
253 |
|
*/ |
254 |
< |
public void putAll(Map<? extends K, ? extends V> t) { |
255 |
< |
Iterator<? extends Entry<? extends K, ? extends V>> i = t.entrySet().iterator(); |
256 |
< |
while (i.hasNext()) { |
312 |
< |
Entry<? extends K, ? extends V> e = i.next(); |
313 |
< |
put(e.getKey(), e.getValue()); |
314 |
< |
} |
254 |
> |
public void putAll(Map<? extends K, ? extends V> m) { |
255 |
> |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
256 |
> |
put(e.getKey(), e.getValue()); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
260 |
< |
* Removes all mappings from this map (optional operation). <p> |
260 |
> |
* {@inheritDoc} |
261 |
|
* |
262 |
< |
* This implementation calls <tt>entrySet().clear()</tt>. |
262 |
> |
* <p>This implementation calls <tt>entrySet().clear()</tt>. |
263 |
|
* |
264 |
< |
* Note that this implementation throws an |
264 |
> |
* <p>Note that this implementation throws an |
265 |
|
* <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> |
266 |
|
* does not support the <tt>clear</tt> operation. |
267 |
|
* |
268 |
< |
* @throws UnsupportedOperationException if the <tt>clear</tt> operation |
327 |
< |
* is not supported by this map. |
268 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
269 |
|
*/ |
270 |
|
public void clear() { |
271 |
|
entrySet().clear(); |
283 |
|
transient volatile Collection<V> values = null; |
284 |
|
|
285 |
|
/** |
286 |
< |
* Returns a {@link Set} view of the keys contained in this map. |
346 |
< |
* The set is backed by the map, so changes to the map are |
347 |
< |
* reflected in the set, and vice-versa. If the map is modified |
348 |
< |
* while an iteration over the set is in progress (except through |
349 |
< |
* the iterator's own <tt>remove</tt> operation), the results of |
350 |
< |
* the iteration are undefined. The set supports element removal, |
351 |
< |
* which removes the corresponding mapping from the map, via the |
352 |
< |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
353 |
< |
* <tt>removeAll</tt> <tt>retainAll</tt>, and <tt>clear</tt> |
354 |
< |
* operations. It does not support the add or <tt>addAll</tt> |
355 |
< |
* operations. |
286 |
> |
* {@inheritDoc} |
287 |
|
* |
288 |
|
* <p>This implementation returns a set that subclasses {@link AbstractSet}. |
289 |
|
* The subclass's iterator method returns a "wrapper object" over this |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
< |
* Returns a {@link Collection} view of the values contained in this map. |
404 |
< |
* The collection is backed by the map, so changes to the map are |
405 |
< |
* reflected in the collection, and vice-versa. If the map is |
406 |
< |
* modified while an iteration over the collection is in progress |
407 |
< |
* (except through the iterator's own <tt>remove</tt> operation), |
408 |
< |
* the results of the iteration are undefined. The collection |
409 |
< |
* supports element removal, which removes the corresponding |
410 |
< |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
411 |
< |
* <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
412 |
< |
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not |
413 |
< |
* support the add or <tt>addAll</tt> operations. |
334 |
> |
* {@inheritDoc} |
335 |
|
* |
336 |
|
* <p>This implementation returns a collection that subclasses {@link |
337 |
|
* AbstractCollection}. The subclass's iterator method returns a |
344 |
|
* returned in response to all subsequent calls. No synchronization is |
345 |
|
* performed, so there is a slight chance that multiple calls to this |
346 |
|
* method will not all return the same collection. |
426 |
– |
* |
427 |
– |
* @return a collection view of the values contained in this map. |
347 |
|
*/ |
348 |
|
public Collection<V> values() { |
349 |
|
if (values == null) { |
386 |
|
/** |
387 |
|
* Compares the specified object with this map for equality. Returns |
388 |
|
* <tt>true</tt> if the given object is also a map and the two maps |
389 |
< |
* represent the same mappings. More formally, two maps <tt>t1</tt> and |
390 |
< |
* <tt>t2</tt> represent the same mappings if |
391 |
< |
* <tt>t1.keySet().equals(t2.keySet())</tt> and for every key <tt>k</tt> |
473 |
< |
* in <tt>t1.keySet()</tt>, <tt> (t1.get(k)==null ? t2.get(k)==null : |
474 |
< |
* t1.get(k).equals(t2.get(k))) </tt>. This ensures that the |
389 |
> |
* represent the same mappings. More formally, two maps <tt>m1</tt> and |
390 |
> |
* <tt>m2</tt> represent the same mappings if |
391 |
> |
* <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the |
392 |
|
* <tt>equals</tt> method works properly across different implementations |
393 |
< |
* of the map interface.<p> |
393 |
> |
* of the <tt>Map</tt> interface. |
394 |
|
* |
395 |
< |
* This implementation first checks if the specified object is this map; |
395 |
> |
* <p>This implementation first checks if the specified object is this map; |
396 |
|
* if so it returns <tt>true</tt>. Then, it checks if the specified |
397 |
|
* object is a map whose size is identical to the size of this map; if |
398 |
|
* not, it returns <tt>false</tt>. If so, it iterates over this map's |
401 |
|
* fails to contain such a mapping, <tt>false</tt> is returned. If the |
402 |
|
* iteration completes, <tt>true</tt> is returned. |
403 |
|
* |
404 |
< |
* @param o object to be compared for equality with this map. |
405 |
< |
* @return <tt>true</tt> if the specified object is equal to this map. |
404 |
> |
* @param o object to be compared for equality with this map |
405 |
> |
* @return <tt>true</tt> if the specified object is equal to this map |
406 |
|
*/ |
407 |
|
public boolean equals(Object o) { |
408 |
|
if (o == this) |
410 |
|
|
411 |
|
if (!(o instanceof Map)) |
412 |
|
return false; |
413 |
< |
Map<K,V> t = (Map<K,V>) o; |
414 |
< |
if (t.size() != size()) |
413 |
> |
Map<K,V> m = (Map<K,V>) o; |
414 |
> |
if (m.size() != size()) |
415 |
|
return false; |
416 |
|
|
417 |
|
try { |
421 |
|
K key = e.getKey(); |
422 |
|
V value = e.getValue(); |
423 |
|
if (value == null) { |
424 |
< |
if (!(t.get(key)==null && t.containsKey(key))) |
424 |
> |
if (!(m.get(key)==null && m.containsKey(key))) |
425 |
|
return false; |
426 |
|
} else { |
427 |
< |
if (!value.equals(t.get(key))) |
427 |
> |
if (!value.equals(m.get(key))) |
428 |
|
return false; |
429 |
|
} |
430 |
|
} |
440 |
|
/** |
441 |
|
* Returns the hash code value for this map. The hash code of a map is |
442 |
|
* defined to be the sum of the hash codes of each entry in the map's |
443 |
< |
* <tt>entrySet()</tt> view. This ensures that <tt>t1.equals(t2)</tt> |
444 |
< |
* implies that <tt>t1.hashCode()==t2.hashCode()</tt> for any two maps |
445 |
< |
* <tt>t1</tt> and <tt>t2</tt>, as required by the general contract of |
446 |
< |
* Object.hashCode.<p> |
447 |
< |
* |
448 |
< |
* This implementation iterates over <tt>entrySet()</tt>, calling |
449 |
< |
* <tt>hashCode</tt> on each element (entry) in the Collection, and adding |
450 |
< |
* up the results. |
443 |
> |
* <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt> |
444 |
> |
* implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps |
445 |
> |
* <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of |
446 |
> |
* {@link Object#hashCode}. |
447 |
> |
* |
448 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt>, calling |
449 |
> |
* {@link Map.Entry#hashCode hashCode()} on each element (entry) in the |
450 |
> |
* set, and adding up the results. |
451 |
|
* |
452 |
< |
* @return the hash code value for this map. |
452 |
> |
* @return the hash code value for this map |
453 |
|
* @see Map.Entry#hashCode() |
537 |
– |
* @see Object#hashCode() |
454 |
|
* @see Object#equals(Object) |
455 |
|
* @see Set#equals(Object) |
456 |
|
*/ |
470 |
|
* <tt>", "</tt> (comma and space). Each key-value mapping is rendered as |
471 |
|
* the key followed by an equals sign (<tt>"="</tt>) followed by the |
472 |
|
* associated value. Keys and values are converted to strings as by |
473 |
< |
* <tt>String.valueOf(Object)</tt>.<p> |
558 |
< |
* |
559 |
< |
* This implementation creates an empty string buffer, appends a left |
560 |
< |
* brace, and iterates over the map's <tt>entrySet</tt> view, appending |
561 |
< |
* the string representation of each <tt>map.entry</tt> in turn. After |
562 |
< |
* appending each entry except the last, the string <tt>", "</tt> is |
563 |
< |
* appended. Finally a right brace is appended. A string is obtained |
564 |
< |
* from the stringbuffer, and returned. |
473 |
> |
* {@link String#valueOf(Object)}. |
474 |
|
* |
475 |
< |
* @return a String representation of this map. |
475 |
> |
* @return a string representation of this map |
476 |
|
*/ |
477 |
|
public String toString() { |
569 |
– |
StringBuffer buf = new StringBuffer(); |
570 |
– |
buf.append("{"); |
571 |
– |
|
478 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
479 |
< |
boolean hasNext = i.hasNext(); |
480 |
< |
while (hasNext) { |
479 |
> |
if (! i.hasNext()) |
480 |
> |
return "{}"; |
481 |
> |
|
482 |
> |
StringBuilder sb = new StringBuilder(); |
483 |
> |
sb.append('{'); |
484 |
> |
for (;;) { |
485 |
|
Entry<K,V> e = i.next(); |
486 |
|
K key = e.getKey(); |
487 |
< |
V value = e.getValue(); |
488 |
< |
if (key == this) |
489 |
< |
buf.append("(this Map)"); |
490 |
< |
else |
491 |
< |
buf.append(key); |
492 |
< |
buf.append("="); |
493 |
< |
if (value == this) |
494 |
< |
buf.append("(this Map)"); |
585 |
< |
else |
586 |
< |
buf.append(value); |
587 |
< |
hasNext = i.hasNext(); |
588 |
< |
if (hasNext) |
589 |
< |
buf.append(", "); |
590 |
< |
} |
591 |
< |
|
592 |
< |
buf.append("}"); |
593 |
< |
return buf.toString(); |
487 |
> |
V value = e.getValue(); |
488 |
> |
sb.append(key == this ? "(this Map)" : key); |
489 |
> |
sb.append('='); |
490 |
> |
sb.append(value == this ? "(this Map)" : value); |
491 |
> |
if (! i.hasNext()) |
492 |
> |
return sb.append('}').toString(); |
493 |
> |
sb.append(", "); |
494 |
> |
} |
495 |
|
} |
496 |
|
|
497 |
|
/** |
498 |
|
* Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys |
499 |
|
* and values themselves are not cloned. |
500 |
|
* |
501 |
< |
* @return a shallow copy of this map. |
501 |
> |
* @return a shallow copy of this map |
502 |
|
*/ |
503 |
|
protected Object clone() throws CloneNotSupportedException { |
504 |
|
AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone(); |
512 |
|
* Test for equality, checking for nulls. |
513 |
|
*/ |
514 |
|
private static boolean eq(Object o1, Object o2) { |
515 |
< |
return (o1 == null ? o2 == null : o1.equals(o2)); |
515 |
> |
return o1 == null ? o2 == null : o1.equals(o2); |
516 |
|
} |
517 |
|
|
518 |
|
// Implementation Note: SimpleEntry and SimpleImmutableEntry |
529 |
|
* facilitates the process of building custom map |
530 |
|
* implementations. For example, it may be convenient to return |
531 |
|
* arrays of <tt>SimpleEntry</tt> instances in method |
532 |
< |
* <tt>Map.entrySet().toArray</tt> |
532 |
> |
* <tt>Map.entrySet().toArray</tt>. |
533 |
> |
* |
534 |
> |
* @since 1.6 |
535 |
|
*/ |
536 |
< |
public static class SimpleEntry<K,V> implements Entry<K,V> { |
536 |
> |
public static class SimpleEntry<K,V> |
537 |
> |
implements Entry<K,V>, java.io.Serializable |
538 |
> |
{ |
539 |
> |
private static final long serialVersionUID = -8499721149061103585L; |
540 |
> |
|
541 |
|
private final K key; |
542 |
|
private V value; |
543 |
|
|
557 |
|
* Creates an entry representing the same mapping as the |
558 |
|
* specified entry. |
559 |
|
* |
560 |
< |
* @param entry the entry to copy. |
560 |
> |
* @param entry the entry to copy |
561 |
|
*/ |
562 |
|
public SimpleEntry(Entry<? extends K, ? extends V> entry) { |
563 |
|
this.key = entry.getKey(); |
567 |
|
/** |
568 |
|
* Returns the key corresponding to this entry. |
569 |
|
* |
570 |
< |
* @return the key corresponding to this entry. |
570 |
> |
* @return the key corresponding to this entry |
571 |
|
*/ |
572 |
|
public K getKey() { |
573 |
|
return key; |
576 |
|
/** |
577 |
|
* Returns the value corresponding to this entry. |
578 |
|
* |
579 |
< |
* @return the value corresponding to this entry. |
579 |
> |
* @return the value corresponding to this entry |
580 |
|
*/ |
581 |
|
public V getValue() { |
582 |
|
return value; |
586 |
|
* Replaces the value corresponding to this entry with the specified |
587 |
|
* value. |
588 |
|
* |
589 |
< |
* @param value new value to be stored in this entry. |
590 |
< |
* @return the old value corresponding to the entry. |
589 |
> |
* @param value new value to be stored in this entry |
590 |
> |
* @return the old value corresponding to the entry |
591 |
|
*/ |
592 |
|
public V setValue(V value) { |
593 |
|
V oldValue = this.value; |
595 |
|
return oldValue; |
596 |
|
} |
597 |
|
|
598 |
+ |
/** |
599 |
+ |
* Compares the specified object with this entry for equality. |
600 |
+ |
* Returns {@code true} if the given object is also a map entry and |
601 |
+ |
* the two entries represent the same mapping. More formally, two |
602 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
603 |
+ |
* if<pre> |
604 |
+ |
* (e1.getKey()==null ? |
605 |
+ |
* e2.getKey()==null : |
606 |
+ |
* e1.getKey().equals(e2.getKey())) |
607 |
+ |
* && |
608 |
+ |
* (e1.getValue()==null ? |
609 |
+ |
* e2.getValue()==null : |
610 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
611 |
+ |
* This ensures that the {@code equals} method works properly across |
612 |
+ |
* different implementations of the {@code Map.Entry} interface. |
613 |
+ |
* |
614 |
+ |
* @param o object to be compared for equality with this map entry |
615 |
+ |
* @return {@code true} if the specified object is equal to this map |
616 |
+ |
* entry |
617 |
+ |
* @see #hashCode |
618 |
+ |
*/ |
619 |
|
public boolean equals(Object o) { |
620 |
|
if (!(o instanceof Map.Entry)) |
621 |
|
return false; |
623 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
624 |
|
} |
625 |
|
|
626 |
+ |
/** |
627 |
+ |
* Returns the hash code value for this map entry. The hash code |
628 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
629 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
630 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
631 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
632 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
633 |
+ |
* {@code e1} and {@code e2}, as required by the general |
634 |
+ |
* contract of {@link Object#hashCode}. |
635 |
+ |
* |
636 |
+ |
* @return the hash code value for this map entry |
637 |
+ |
* @see #equals |
638 |
+ |
*/ |
639 |
|
public int hashCode() { |
640 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
641 |
< |
((value == null) ? 0 : value.hashCode()); |
640 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
641 |
> |
(value == null ? 0 : value.hashCode()); |
642 |
|
} |
643 |
|
|
644 |
|
/** |
647 |
|
* entry's key followed by the equals character ("<tt>=</tt>") |
648 |
|
* followed by the string representation of this entry's value. |
649 |
|
* |
650 |
< |
* @return a String representation of this map entry. |
650 |
> |
* @return a String representation of this map entry |
651 |
|
*/ |
652 |
|
public String toString() { |
653 |
|
return key + "=" + value; |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* An Entry maintaining an immutable key and value, This class |
659 |
> |
* An Entry maintaining an immutable key and value. This class |
660 |
|
* does not support method <tt>setValue</tt>. This class may be |
661 |
|
* convenient in methods that return thread-safe snapshots of |
662 |
|
* key-value mappings. |
663 |
+ |
* |
664 |
+ |
* @since 1.6 |
665 |
|
*/ |
666 |
< |
public static class SimpleImmutableEntry<K,V> implements Entry<K,V> { |
666 |
> |
public static class SimpleImmutableEntry<K,V> |
667 |
> |
implements Entry<K,V>, java.io.Serializable |
668 |
> |
{ |
669 |
> |
private static final long serialVersionUID = 7138329143949025153L; |
670 |
> |
|
671 |
|
private final K key; |
672 |
|
private final V value; |
673 |
|
|
687 |
|
* Creates an entry representing the same mapping as the |
688 |
|
* specified entry. |
689 |
|
* |
690 |
< |
* @param entry the entry to copy. |
690 |
> |
* @param entry the entry to copy |
691 |
|
*/ |
692 |
|
public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) { |
693 |
|
this.key = entry.getKey(); |
697 |
|
/** |
698 |
|
* Returns the key corresponding to this entry. |
699 |
|
* |
700 |
< |
* @return the key corresponding to this entry. |
700 |
> |
* @return the key corresponding to this entry |
701 |
|
*/ |
702 |
|
public K getKey() { |
703 |
|
return key; |
706 |
|
/** |
707 |
|
* Returns the value corresponding to this entry. |
708 |
|
* |
709 |
< |
* @return the value corresponding to this entry. |
709 |
> |
* @return the value corresponding to this entry |
710 |
|
*/ |
711 |
|
public V getValue() { |
712 |
|
return value; |
718 |
|
* <tt>UnsupportedOperationException</tt>, as this class implements |
719 |
|
* an <i>immutable</i> map entry. |
720 |
|
* |
721 |
< |
* @param value new value to be stored in this entry. |
721 |
> |
* @param value new value to be stored in this entry |
722 |
|
* @return (Does not return) |
723 |
|
* @throws UnsupportedOperationException always |
724 |
|
*/ |
726 |
|
throw new UnsupportedOperationException(); |
727 |
|
} |
728 |
|
|
729 |
+ |
/** |
730 |
+ |
* Compares the specified object with this entry for equality. |
731 |
+ |
* Returns {@code true} if the given object is also a map entry and |
732 |
+ |
* the two entries represent the same mapping. More formally, two |
733 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
734 |
+ |
* if<pre> |
735 |
+ |
* (e1.getKey()==null ? |
736 |
+ |
* e2.getKey()==null : |
737 |
+ |
* e1.getKey().equals(e2.getKey())) |
738 |
+ |
* && |
739 |
+ |
* (e1.getValue()==null ? |
740 |
+ |
* e2.getValue()==null : |
741 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
742 |
+ |
* This ensures that the {@code equals} method works properly across |
743 |
+ |
* different implementations of the {@code Map.Entry} interface. |
744 |
+ |
* |
745 |
+ |
* @param o object to be compared for equality with this map entry |
746 |
+ |
* @return {@code true} if the specified object is equal to this map |
747 |
+ |
* entry |
748 |
+ |
* @see #hashCode |
749 |
+ |
*/ |
750 |
|
public boolean equals(Object o) { |
751 |
|
if (!(o instanceof Map.Entry)) |
752 |
|
return false; |
754 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
755 |
|
} |
756 |
|
|
757 |
+ |
/** |
758 |
+ |
* Returns the hash code value for this map entry. The hash code |
759 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
760 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
761 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
762 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
763 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
764 |
+ |
* {@code e1} and {@code e2}, as required by the general |
765 |
+ |
* contract of {@link Object#hashCode}. |
766 |
+ |
* |
767 |
+ |
* @return the hash code value for this map entry |
768 |
+ |
* @see #equals |
769 |
+ |
*/ |
770 |
|
public int hashCode() { |
771 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
772 |
< |
((value == null) ? 0 : value.hashCode()); |
771 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
772 |
> |
(value == null ? 0 : value.hashCode()); |
773 |
|
} |
774 |
|
|
775 |
|
/** |
778 |
|
* entry's key followed by the equals character ("<tt>=</tt>") |
779 |
|
* followed by the string representation of this entry's value. |
780 |
|
* |
781 |
< |
* @return a String representation of this map entry. |
781 |
> |
* @return a String representation of this map entry |
782 |
|
*/ |
783 |
|
public String toString() { |
784 |
|
return key + "=" + value; |