1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
8 |
|
package java.util; |
9 |
+ |
import java.util.*; // for javadoc (till 6280605 is fixed) |
10 |
|
import java.util.Map.Entry; |
11 |
|
|
12 |
|
/** |
13 |
|
* This class provides a skeletal implementation of the <tt>Map</tt> |
14 |
< |
* interface, to minimize the effort required to implement this interface. <p> |
14 |
> |
* interface, to minimize the effort required to implement this interface. |
15 |
|
* |
16 |
< |
* To implement an unmodifiable map, the programmer needs only to extend this |
16 |
> |
* <p>To implement an unmodifiable map, the programmer needs only to extend this |
17 |
|
* class and provide an implementation for the <tt>entrySet</tt> method, which |
18 |
|
* returns a set-view of the map's mappings. Typically, the returned set |
19 |
|
* will, in turn, be implemented atop <tt>AbstractSet</tt>. This set should |
20 |
|
* not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator |
21 |
< |
* should not support the <tt>remove</tt> method.<p> |
21 |
> |
* should not support the <tt>remove</tt> method. |
22 |
|
* |
23 |
< |
* To implement a modifiable map, the programmer must additionally override |
23 |
> |
* <p>To implement a modifiable map, the programmer must additionally override |
24 |
|
* this class's <tt>put</tt> method (which otherwise throws an |
25 |
|
* <tt>UnsupportedOperationException</tt>), and the iterator returned by |
26 |
|
* <tt>entrySet().iterator()</tt> must additionally implement its |
27 |
< |
* <tt>remove</tt> method.<p> |
27 |
> |
* <tt>remove</tt> method. |
28 |
|
* |
29 |
< |
* The programmer should generally provide a void (no argument) and map |
29 |
> |
* <p>The programmer should generally provide a void (no argument) and map |
30 |
|
* constructor, as per the recommendation in the <tt>Map</tt> interface |
31 |
< |
* specification.<p> |
31 |
> |
* specification. |
32 |
|
* |
33 |
< |
* The documentation for each non-abstract methods in this class describes its |
33 |
> |
* <p>The documentation for each non-abstract methods in this class describes its |
34 |
|
* implementation in detail. Each of these methods may be overridden if the |
35 |
< |
* map being implemented admits a more efficient implementation.<p> |
35 |
> |
* map being implemented admits a more efficient implementation. |
36 |
|
* |
37 |
< |
* This class is a member of the |
37 |
> |
* <p>This class is a member of the |
38 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
39 |
|
* Java Collections Framework</a>. |
40 |
|
* |
41 |
+ |
* @param <K> the type of keys maintained by this map |
42 |
+ |
* @param <V> the type of mapped values |
43 |
+ |
* |
44 |
|
* @author Josh Bloch |
45 |
|
* @author Neal Gafter |
46 |
|
* @version %I%, %G% |
60 |
|
// Query Operations |
61 |
|
|
62 |
|
/** |
63 |
< |
* Returns the number of key-value mappings in this map. If the map |
60 |
< |
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
61 |
< |
* <tt>Integer.MAX_VALUE</tt>.<p> |
62 |
< |
* |
63 |
< |
* This implementation returns <tt>entrySet().size()</tt>. |
63 |
> |
* {@inheritDoc} |
64 |
|
* |
65 |
< |
* @return the number of key-value mappings in this map. |
65 |
> |
* <p>This implementation returns <tt>entrySet().size()</tt>. |
66 |
|
*/ |
67 |
|
public int size() { |
68 |
|
return entrySet().size(); |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
< |
* Returns <tt>true</tt> if this map contains no key-value mappings. <p> |
73 |
< |
* |
74 |
< |
* This implementation returns <tt>size() == 0</tt>. |
72 |
> |
* {@inheritDoc} |
73 |
|
* |
74 |
< |
* @return <tt>true</tt> if this map contains no key-value mappings. |
74 |
> |
* <p>This implementation returns <tt>size() == 0</tt>. |
75 |
|
*/ |
76 |
|
public boolean isEmpty() { |
77 |
|
return size() == 0; |
78 |
|
} |
79 |
|
|
80 |
|
/** |
81 |
< |
* Returns <tt>true</tt> if this map maps one or more keys to this value. |
82 |
< |
* More formally, returns <tt>true</tt> if and only if this map contains |
83 |
< |
* at least one mapping to a value <tt>v</tt> such that <tt>(value==null ? |
84 |
< |
* v==null : value.equals(v))</tt>. This operation will probably require |
85 |
< |
* time linear in the map size for most implementations of map.<p> |
86 |
< |
* |
87 |
< |
* This implementation iterates over entrySet() searching for an entry |
88 |
< |
* with the specified value. If such an entry is found, <tt>true</tt> is |
89 |
< |
* returned. If the iteration terminates without finding such an entry, |
90 |
< |
* <tt>false</tt> is returned. Note that this implementation requires |
93 |
< |
* linear time in the size of the map. |
94 |
< |
* |
95 |
< |
* @param value value whose presence in this map is to be tested. |
96 |
< |
* |
97 |
< |
* @return <tt>true</tt> if this map maps one or more keys to this value. |
81 |
> |
* {@inheritDoc} |
82 |
> |
* |
83 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching |
84 |
> |
* for an entry with the specified value. If such an entry is found, |
85 |
> |
* <tt>true</tt> is returned. If the iteration terminates without |
86 |
> |
* finding such an entry, <tt>false</tt> is returned. Note that this |
87 |
> |
* implementation requires linear time in the size of the map. |
88 |
> |
* |
89 |
> |
* @throws ClassCastException {@inheritDoc} |
90 |
> |
* @throws NullPointerException {@inheritDoc} |
91 |
|
*/ |
92 |
|
public boolean containsValue(Object value) { |
93 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
108 |
|
} |
109 |
|
|
110 |
|
/** |
111 |
< |
* Returns <tt>true</tt> if this map contains a mapping for the specified |
119 |
< |
* key. <p> |
111 |
> |
* {@inheritDoc} |
112 |
|
* |
113 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an |
114 |
< |
* entry with the specified key. If such an entry is found, <tt>true</tt> |
115 |
< |
* is returned. If the iteration terminates without finding such an |
116 |
< |
* entry, <tt>false</tt> is returned. Note that this implementation |
117 |
< |
* requires linear time in the size of the map; many implementations will |
118 |
< |
* override this method. |
119 |
< |
* |
120 |
< |
* @param key key whose presence in this map is to be tested. |
121 |
< |
* @return <tt>true</tt> if this map contains a mapping for the specified |
130 |
< |
* key. |
131 |
< |
* |
132 |
< |
* @throws NullPointerException if the key is <tt>null</tt> and this map |
133 |
< |
* does not permit <tt>null</tt> keys. |
113 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching |
114 |
> |
* for an entry with the specified key. If such an entry is found, |
115 |
> |
* <tt>true</tt> is returned. If the iteration terminates without |
116 |
> |
* finding such an entry, <tt>false</tt> is returned. Note that this |
117 |
> |
* implementation requires linear time in the size of the map; many |
118 |
> |
* implementations will override this method. |
119 |
> |
* |
120 |
> |
* @throws ClassCastException {@inheritDoc} |
121 |
> |
* @throws NullPointerException {@inheritDoc} |
122 |
|
*/ |
123 |
|
public boolean containsKey(Object key) { |
124 |
|
Iterator<Map.Entry<K,V>> i = entrySet().iterator(); |
139 |
|
} |
140 |
|
|
141 |
|
/** |
142 |
< |
* Returns the value to which this map maps the specified key. Returns |
143 |
< |
* <tt>null</tt> if the map contains no mapping for this key. A return |
144 |
< |
* value of <tt>null</tt> does not <i>necessarily</i> indicate that the |
145 |
< |
* map contains no mapping for the key; it's also possible that the map |
146 |
< |
* explicitly maps the key to <tt>null</tt>. The containsKey operation |
147 |
< |
* may be used to distinguish these two cases. <p> |
148 |
< |
* |
149 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an |
150 |
< |
* entry with the specified key. If such an entry is found, the entry's |
151 |
< |
* value is returned. If the iteration terminates without finding such an |
152 |
< |
* entry, <tt>null</tt> is returned. Note that this implementation |
165 |
< |
* requires linear time in the size of the map; many implementations will |
166 |
< |
* override this method. |
167 |
< |
* |
168 |
< |
* @param key key whose associated value is to be returned. |
169 |
< |
* @return the value to which this map maps the specified key. |
170 |
< |
* |
171 |
< |
* @throws NullPointerException if the key is <tt>null</tt> and this map |
172 |
< |
* does not permit <tt>null</tt> keys. |
173 |
< |
* |
174 |
< |
* @see #containsKey(Object) |
142 |
> |
* {@inheritDoc} |
143 |
> |
* |
144 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching |
145 |
> |
* for an entry with the specified key. If such an entry is found, |
146 |
> |
* the entry's value is returned. If the iteration terminates without |
147 |
> |
* finding such an entry, <tt>null</tt> is returned. Note that this |
148 |
> |
* implementation requires linear time in the size of the map; many |
149 |
> |
* implementations will override this method. |
150 |
> |
* |
151 |
> |
* @throws ClassCastException {@inheritDoc} |
152 |
> |
* @throws NullPointerException {@inheritDoc} |
153 |
|
*/ |
154 |
|
public V get(Object key) { |
155 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
173 |
|
// Modification Operations |
174 |
|
|
175 |
|
/** |
176 |
< |
* Associates the specified value with the specified key in this map |
199 |
< |
* (optional operation). If the map previously contained a mapping for |
200 |
< |
* this key, the old value is replaced.<p> |
176 |
> |
* {@inheritDoc} |
177 |
|
* |
178 |
< |
* This implementation always throws an |
178 |
> |
* <p>This implementation always throws an |
179 |
|
* <tt>UnsupportedOperationException</tt>. |
180 |
|
* |
181 |
< |
* @param key key with which the specified value is to be associated. |
182 |
< |
* @param value value to be associated with the specified key. |
183 |
< |
* |
184 |
< |
* @return previous value associated with specified key, or <tt>null</tt> |
209 |
< |
* if there was no mapping for key. (A <tt>null</tt> return can |
210 |
< |
* also indicate that the map previously associated <tt>null</tt> |
211 |
< |
* with the specified key, if the implementation supports |
212 |
< |
* <tt>null</tt> values.) |
213 |
< |
* |
214 |
< |
* @throws UnsupportedOperationException if the <tt>put</tt> operation is |
215 |
< |
* not supported by this map. |
216 |
< |
* |
217 |
< |
* @throws ClassCastException if the class of the specified key or value |
218 |
< |
* prevents it from being stored in this map. |
219 |
< |
* |
220 |
< |
* @throws IllegalArgumentException if some aspect of this key or value * |
221 |
< |
* prevents it from being stored in this map. |
222 |
< |
* |
223 |
< |
* @throws NullPointerException if this map does not permit <tt>null</tt> |
224 |
< |
* keys or values, and the specified key or value is |
225 |
< |
* <tt>null</tt>. |
181 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
182 |
> |
* @throws ClassCastException {@inheritDoc} |
183 |
> |
* @throws NullPointerException {@inheritDoc} |
184 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
185 |
|
*/ |
186 |
|
public V put(K key, V value) { |
187 |
|
throw new UnsupportedOperationException(); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* Removes the mapping for this key from this map if present (optional |
233 |
< |
* operation). <p> |
191 |
> |
* {@inheritDoc} |
192 |
|
* |
193 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an |
193 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching for an |
194 |
|
* entry with the specified key. If such an entry is found, its value is |
195 |
|
* obtained with its <tt>getValue</tt> operation, the entry is removed |
196 |
< |
* from the Collection (and the backing map) with the iterator's |
196 |
> |
* from the collection (and the backing map) with the iterator's |
197 |
|
* <tt>remove</tt> operation, and the saved value is returned. If the |
198 |
|
* iteration terminates without finding such an entry, <tt>null</tt> is |
199 |
|
* returned. Note that this implementation requires linear time in the |
200 |
< |
* size of the map; many implementations will override this method.<p> |
200 |
> |
* size of the map; many implementations will override this method. |
201 |
> |
* |
202 |
> |
* <p>Note that this implementation throws an |
203 |
> |
* <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> |
204 |
> |
* iterator does not support the <tt>remove</tt> method and this map |
205 |
> |
* contains a mapping for the specified key. |
206 |
|
* |
207 |
< |
* Note that this implementation throws an |
208 |
< |
* <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> iterator |
209 |
< |
* does not support the <tt>remove</tt> method and this map contains a |
247 |
< |
* mapping for the specified key. |
248 |
< |
* |
249 |
< |
* @param key key whose mapping is to be removed from the map. |
250 |
< |
* @return previous value associated with specified key, or <tt>null</tt> |
251 |
< |
* if there was no entry for key. (A <tt>null</tt> return can |
252 |
< |
* also indicate that the map previously associated <tt>null</tt> |
253 |
< |
* with the specified key, if the implementation supports |
254 |
< |
* <tt>null</tt> values.) |
255 |
< |
* @throws UnsupportedOperationException if the <tt>remove</tt> operation |
256 |
< |
* is not supported by this map. |
207 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
208 |
> |
* @throws ClassCastException {@inheritDoc} |
209 |
> |
* @throws NullPointerException {@inheritDoc} |
210 |
|
*/ |
211 |
|
public V remove(Object key) { |
212 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
237 |
|
// Bulk Operations |
238 |
|
|
239 |
|
/** |
240 |
< |
* Copies all of the mappings from the specified map to this map |
288 |
< |
* (optional operation). These mappings will replace any mappings that |
289 |
< |
* this map had for any of the keys currently in the specified map.<p> |
240 |
> |
* {@inheritDoc} |
241 |
|
* |
242 |
< |
* This implementation iterates over the specified map's |
242 |
> |
* <p>This implementation iterates over the specified map's |
243 |
|
* <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt> |
244 |
< |
* operation once for each entry returned by the iteration.<p> |
244 |
> |
* operation once for each entry returned by the iteration. |
245 |
|
* |
246 |
< |
* Note that this implementation throws an |
246 |
> |
* <p>Note that this implementation throws an |
247 |
|
* <tt>UnsupportedOperationException</tt> if this map does not support |
248 |
|
* the <tt>put</tt> operation and the specified map is nonempty. |
249 |
|
* |
250 |
< |
* @param t mappings to be stored in this map. |
251 |
< |
* |
252 |
< |
* @throws UnsupportedOperationException if the <tt>putAll</tt> operation |
253 |
< |
* is not supported by this map. |
303 |
< |
* |
304 |
< |
* @throws ClassCastException if the class of a key or value in the |
305 |
< |
* specified map prevents it from being stored in this map. |
306 |
< |
* |
307 |
< |
* @throws IllegalArgumentException if some aspect of a key or value in |
308 |
< |
* the specified map prevents it from being stored in this map. |
309 |
< |
* @throws NullPointerException if the specified map is <tt>null</tt>, or if |
310 |
< |
* this map does not permit <tt>null</tt> keys or values, and the |
311 |
< |
* specified map contains <tt>null</tt> keys or values. |
250 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
251 |
> |
* @throws ClassCastException {@inheritDoc} |
252 |
> |
* @throws NullPointerException {@inheritDoc} |
253 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
254 |
|
*/ |
255 |
< |
public void putAll(Map<? extends K, ? extends V> t) { |
256 |
< |
Iterator<? extends Entry<? extends K, ? extends V>> i = t.entrySet().iterator(); |
255 |
> |
public void putAll(Map<? extends K, ? extends V> m) { |
256 |
> |
Iterator<? extends Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); |
257 |
|
while (i.hasNext()) { |
258 |
|
Entry<? extends K, ? extends V> e = i.next(); |
259 |
|
put(e.getKey(), e.getValue()); |
261 |
|
} |
262 |
|
|
263 |
|
/** |
264 |
< |
* Removes all mappings from this map (optional operation). <p> |
264 |
> |
* {@inheritDoc} |
265 |
|
* |
266 |
< |
* This implementation calls <tt>entrySet().clear()</tt>. |
266 |
> |
* <p>This implementation calls <tt>entrySet().clear()</tt>. |
267 |
|
* |
268 |
< |
* Note that this implementation throws an |
268 |
> |
* <p>Note that this implementation throws an |
269 |
|
* <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> |
270 |
|
* does not support the <tt>clear</tt> operation. |
271 |
|
* |
272 |
< |
* @throws UnsupportedOperationException clear is not supported |
331 |
< |
* by this map. |
272 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
273 |
|
*/ |
274 |
|
public void clear() { |
275 |
|
entrySet().clear(); |
287 |
|
transient volatile Collection<V> values = null; |
288 |
|
|
289 |
|
/** |
290 |
< |
* Returns a Set view of the keys contained in this map. The Set is |
350 |
< |
* backed by the map, so changes to the map are reflected in the Set, |
351 |
< |
* and vice-versa. (If the map is modified while an iteration over |
352 |
< |
* the Set is in progress, the results of the iteration are undefined.) |
353 |
< |
* The Set supports element removal, which removes the corresponding entry |
354 |
< |
* from the map, via the Iterator.remove, Set.remove, removeAll |
355 |
< |
* retainAll, and clear operations. It does not support the add or |
356 |
< |
* addAll operations.<p> |
357 |
< |
* |
358 |
< |
* This implementation returns a Set that subclasses |
359 |
< |
* AbstractSet. The subclass's iterator method returns a "wrapper |
360 |
< |
* object" over this map's entrySet() iterator. The size method delegates |
361 |
< |
* to this map's size method and the contains method delegates to this |
362 |
< |
* map's containsKey method.<p> |
290 |
> |
* {@inheritDoc} |
291 |
|
* |
292 |
< |
* The Set is created the first time this method is called, |
292 |
> |
* <p>This implementation returns a set that subclasses {@link AbstractSet}. |
293 |
> |
* The subclass's iterator method returns a "wrapper object" over this |
294 |
> |
* map's <tt>entrySet()</tt> iterator. The <tt>size</tt> method |
295 |
> |
* delegates to this map's <tt>size</tt> method and the |
296 |
> |
* <tt>contains</tt> method delegates to this map's |
297 |
> |
* <tt>containsKey</tt> method. |
298 |
> |
* |
299 |
> |
* <p>The set is created the first time this method is called, |
300 |
|
* and returned in response to all subsequent calls. No synchronization |
301 |
|
* is performed, so there is a slight chance that multiple calls to this |
302 |
< |
* method will not all return the same Set. |
368 |
< |
* |
369 |
< |
* @return a Set view of the keys contained in this map. |
302 |
> |
* method will not all return the same set. |
303 |
|
*/ |
304 |
|
public Set<K> keySet() { |
305 |
|
if (keySet == null) { |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
< |
* Returns a collection view of the values contained in this map. The |
339 |
< |
* collection is backed by the map, so changes to the map are reflected in |
340 |
< |
* the collection, and vice-versa. (If the map is modified while an |
341 |
< |
* iteration over the collection is in progress, the results of the |
342 |
< |
* iteration are undefined.) The collection supports element removal, |
343 |
< |
* which removes the corresponding entry from the map, via the |
344 |
< |
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
345 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> operations. |
413 |
< |
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations.<p> |
414 |
< |
* |
415 |
< |
* This implementation returns a collection that subclasses abstract |
416 |
< |
* collection. The subclass's iterator method returns a "wrapper object" |
417 |
< |
* over this map's <tt>entrySet()</tt> iterator. The size method |
418 |
< |
* delegates to this map's size method and the contains method delegates |
419 |
< |
* to this map's containsValue method.<p> |
338 |
> |
* {@inheritDoc} |
339 |
> |
* |
340 |
> |
* <p>This implementation returns a collection that subclasses {@link |
341 |
> |
* AbstractCollection}. The subclass's iterator method returns a |
342 |
> |
* "wrapper object" over this map's <tt>entrySet()</tt> iterator. |
343 |
> |
* The <tt>size</tt> method delegates to this map's <tt>size</tt> |
344 |
> |
* method and the <tt>contains</tt> method delegates to this map's |
345 |
> |
* <tt>containsValue</tt> method. |
346 |
|
* |
347 |
< |
* The collection is created the first time this method is called, and |
347 |
> |
* <p>The collection is created the first time this method is called, and |
348 |
|
* returned in response to all subsequent calls. No synchronization is |
349 |
|
* performed, so there is a slight chance that multiple calls to this |
350 |
< |
* method will not all return the same Collection. |
425 |
< |
* |
426 |
< |
* @return a collection view of the values contained in this map. |
350 |
> |
* method will not all return the same collection. |
351 |
|
*/ |
352 |
|
public Collection<V> values() { |
353 |
|
if (values == null) { |
382 |
|
return values; |
383 |
|
} |
384 |
|
|
461 |
– |
/** |
462 |
– |
* Returns a set view of the mappings contained in this map. Each element |
463 |
– |
* in this set is a Map.Entry. The set is backed by the map, so changes |
464 |
– |
* to the map are reflected in the set, and vice-versa. (If the map is |
465 |
– |
* modified while an iteration over the set is in progress, the results of |
466 |
– |
* the iteration are undefined.) The set supports element removal, which |
467 |
– |
* removes the corresponding entry from the map, via the |
468 |
– |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>, |
469 |
– |
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support |
470 |
– |
* the <tt>add</tt> or <tt>addAll</tt> operations. |
471 |
– |
* |
472 |
– |
* @return a set view of the mappings contained in this map. |
473 |
– |
*/ |
385 |
|
public abstract Set<Entry<K,V>> entrySet(); |
386 |
|
|
387 |
|
|
390 |
|
/** |
391 |
|
* Compares the specified object with this map for equality. Returns |
392 |
|
* <tt>true</tt> if the given object is also a map and the two maps |
393 |
< |
* represent the same mappings. More formally, two maps <tt>t1</tt> and |
394 |
< |
* <tt>t2</tt> represent the same mappings if |
395 |
< |
* <tt>t1.keySet().equals(t2.keySet())</tt> and for every key <tt>k</tt> |
485 |
< |
* in <tt>t1.keySet()</tt>, <tt> (t1.get(k)==null ? t2.get(k)==null : |
486 |
< |
* t1.get(k).equals(t2.get(k))) </tt>. This ensures that the |
393 |
> |
* represent the same mappings. More formally, two maps <tt>m1</tt> and |
394 |
> |
* <tt>m2</tt> represent the same mappings if |
395 |
> |
* <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the |
396 |
|
* <tt>equals</tt> method works properly across different implementations |
397 |
< |
* of the map interface.<p> |
397 |
> |
* of the <tt>Map</tt> interface. |
398 |
|
* |
399 |
< |
* This implementation first checks if the specified object is this map; |
399 |
> |
* <p>This implementation first checks if the specified object is this map; |
400 |
|
* if so it returns <tt>true</tt>. Then, it checks if the specified |
401 |
< |
* object is a map whose size is identical to the size of this set; if |
401 |
> |
* object is a map whose size is identical to the size of this map; if |
402 |
|
* not, it returns <tt>false</tt>. If so, it iterates over this map's |
403 |
|
* <tt>entrySet</tt> collection, and checks that the specified map |
404 |
|
* contains each mapping that this map contains. If the specified map |
405 |
|
* fails to contain such a mapping, <tt>false</tt> is returned. If the |
406 |
|
* iteration completes, <tt>true</tt> is returned. |
407 |
|
* |
408 |
< |
* @param o object to be compared for equality with this map. |
409 |
< |
* @return <tt>true</tt> if the specified object is equal to this map. |
408 |
> |
* @param o object to be compared for equality with this map |
409 |
> |
* @return <tt>true</tt> if the specified object is equal to this map |
410 |
|
*/ |
411 |
|
public boolean equals(Object o) { |
412 |
|
if (o == this) |
414 |
|
|
415 |
|
if (!(o instanceof Map)) |
416 |
|
return false; |
417 |
< |
Map<K,V> t = (Map<K,V>) o; |
418 |
< |
if (t.size() != size()) |
417 |
> |
Map<K,V> m = (Map<K,V>) o; |
418 |
> |
if (m.size() != size()) |
419 |
|
return false; |
420 |
|
|
421 |
|
try { |
425 |
|
K key = e.getKey(); |
426 |
|
V value = e.getValue(); |
427 |
|
if (value == null) { |
428 |
< |
if (!(t.get(key)==null && t.containsKey(key))) |
428 |
> |
if (!(m.get(key)==null && m.containsKey(key))) |
429 |
|
return false; |
430 |
|
} else { |
431 |
< |
if (!value.equals(t.get(key))) |
431 |
> |
if (!value.equals(m.get(key))) |
432 |
|
return false; |
433 |
|
} |
434 |
|
} |
435 |
< |
} catch(ClassCastException unused) { |
435 |
> |
} catch (ClassCastException unused) { |
436 |
|
return false; |
437 |
< |
} catch(NullPointerException unused) { |
437 |
> |
} catch (NullPointerException unused) { |
438 |
|
return false; |
439 |
|
} |
440 |
|
|
444 |
|
/** |
445 |
|
* Returns the hash code value for this map. The hash code of a map is |
446 |
|
* defined to be the sum of the hash codes of each entry in the map's |
447 |
< |
* <tt>entrySet()</tt> view. This ensures that <tt>t1.equals(t2)</tt> |
448 |
< |
* implies that <tt>t1.hashCode()==t2.hashCode()</tt> for any two maps |
449 |
< |
* <tt>t1</tt> and <tt>t2</tt>, as required by the general contract of |
450 |
< |
* Object.hashCode.<p> |
451 |
< |
* |
452 |
< |
* This implementation iterates over <tt>entrySet()</tt>, calling |
453 |
< |
* <tt>hashCode</tt> on each element (entry) in the Collection, and adding |
454 |
< |
* up the results. |
447 |
> |
* <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt> |
448 |
> |
* implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps |
449 |
> |
* <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of |
450 |
> |
* {@link Object#hashCode}. |
451 |
> |
* |
452 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt>, calling |
453 |
> |
* {@link Map.Entry#hashCode hashCode()} on each element (entry) in the |
454 |
> |
* set, and adding up the results. |
455 |
|
* |
456 |
< |
* @return the hash code value for this map. |
456 |
> |
* @return the hash code value for this map |
457 |
|
* @see Map.Entry#hashCode() |
549 |
– |
* @see Object#hashCode() |
458 |
|
* @see Object#equals(Object) |
459 |
|
* @see Set#equals(Object) |
460 |
|
*/ |
483 |
|
* appended. Finally a right brace is appended. A string is obtained |
484 |
|
* from the stringbuffer, and returned. |
485 |
|
* |
486 |
< |
* @return a String representation of this map. |
486 |
> |
* @return a String representation of this map |
487 |
|
*/ |
488 |
|
public String toString() { |
489 |
< |
StringBuffer buf = new StringBuffer(); |
490 |
< |
buf.append("{"); |
489 |
> |
StringBuilder sb = new StringBuilder(); |
490 |
> |
sb.append("{"); |
491 |
|
|
492 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
493 |
|
boolean hasNext = i.hasNext(); |
496 |
|
K key = e.getKey(); |
497 |
|
V value = e.getValue(); |
498 |
|
if (key == this) |
499 |
< |
buf.append("(this Map)"); |
499 |
> |
sb.append("(this Map)"); |
500 |
|
else |
501 |
< |
buf.append(key); |
502 |
< |
buf.append("="); |
501 |
> |
sb.append(key); |
502 |
> |
sb.append("="); |
503 |
|
if (value == this) |
504 |
< |
buf.append("(this Map)"); |
504 |
> |
sb.append("(this Map)"); |
505 |
|
else |
506 |
< |
buf.append(value); |
506 |
> |
sb.append(value); |
507 |
|
hasNext = i.hasNext(); |
508 |
|
if (hasNext) |
509 |
< |
buf.append(", "); |
509 |
> |
sb.append(", "); |
510 |
|
} |
511 |
|
|
512 |
< |
buf.append("}"); |
513 |
< |
return buf.toString(); |
512 |
> |
sb.append("}"); |
513 |
> |
return sb.toString(); |
514 |
|
} |
515 |
< |
|
515 |
> |
|
516 |
|
/** |
517 |
|
* Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys |
518 |
|
* and values themselves are not cloned. |
519 |
|
* |
520 |
< |
* @return a shallow copy of this map. |
520 |
> |
* @return a shallow copy of this map |
521 |
|
*/ |
522 |
|
protected Object clone() throws CloneNotSupportedException { |
523 |
|
AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone(); |
549 |
|
* implementations. For example, it may be convenient to return |
550 |
|
* arrays of <tt>SimpleEntry</tt> instances in method |
551 |
|
* <tt>Map.entrySet().toArray</tt> |
552 |
+ |
* |
553 |
+ |
* @since 1.6 |
554 |
|
*/ |
555 |
|
public static class SimpleEntry<K,V> implements Entry<K,V> { |
556 |
|
private final K key; |
572 |
|
* Creates an entry representing the same mapping as the |
573 |
|
* specified entry. |
574 |
|
* |
575 |
< |
* @param entry the entry to copy. |
575 |
> |
* @param entry the entry to copy |
576 |
|
*/ |
577 |
|
public SimpleEntry(Entry<? extends K, ? extends V> entry) { |
578 |
|
this.key = entry.getKey(); |
582 |
|
/** |
583 |
|
* Returns the key corresponding to this entry. |
584 |
|
* |
585 |
< |
* @return the key corresponding to this entry. |
585 |
> |
* @return the key corresponding to this entry |
586 |
|
*/ |
587 |
|
public K getKey() { |
588 |
|
return key; |
591 |
|
/** |
592 |
|
* Returns the value corresponding to this entry. |
593 |
|
* |
594 |
< |
* @return the value corresponding to this entry. |
594 |
> |
* @return the value corresponding to this entry |
595 |
|
*/ |
596 |
|
public V getValue() { |
597 |
|
return value; |
601 |
|
* Replaces the value corresponding to this entry with the specified |
602 |
|
* value. |
603 |
|
* |
604 |
< |
* @param value new value to be stored in this entry. |
605 |
< |
* @return old value corresponding to the entry. |
604 |
> |
* @param value new value to be stored in this entry |
605 |
> |
* @return the old value corresponding to the entry |
606 |
|
*/ |
607 |
|
public V setValue(V value) { |
608 |
|
V oldValue = this.value; |
628 |
|
* entry's key followed by the equals character ("<tt>=</tt>") |
629 |
|
* followed by the string representation of this entry's value. |
630 |
|
* |
631 |
< |
* @return a String representation of this map entry. |
631 |
> |
* @return a String representation of this map entry |
632 |
|
*/ |
633 |
|
public String toString() { |
634 |
|
return key + "=" + value; |
641 |
|
* does not support method <tt>setValue</tt>. This class may be |
642 |
|
* convenient in methods that return thread-safe snapshots of |
643 |
|
* key-value mappings. |
644 |
+ |
* |
645 |
+ |
* @since 1.6 |
646 |
|
*/ |
647 |
|
public static class SimpleImmutableEntry<K,V> implements Entry<K,V> { |
648 |
|
private final K key; |
664 |
|
* Creates an entry representing the same mapping as the |
665 |
|
* specified entry. |
666 |
|
* |
667 |
< |
* @param entry the entry to copy. |
667 |
> |
* @param entry the entry to copy |
668 |
|
*/ |
669 |
|
public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) { |
670 |
|
this.key = entry.getKey(); |
674 |
|
/** |
675 |
|
* Returns the key corresponding to this entry. |
676 |
|
* |
677 |
< |
* @return the key corresponding to this entry. |
677 |
> |
* @return the key corresponding to this entry |
678 |
|
*/ |
679 |
|
public K getKey() { |
680 |
|
return key; |
683 |
|
/** |
684 |
|
* Returns the value corresponding to this entry. |
685 |
|
* |
686 |
< |
* @return the value corresponding to this entry. |
686 |
> |
* @return the value corresponding to this entry |
687 |
|
*/ |
688 |
|
public V getValue() { |
689 |
|
return value; |
695 |
|
* <tt>UnsupportedOperationException</tt>, as this class implements |
696 |
|
* an <i>immutable</i> map entry. |
697 |
|
* |
698 |
< |
* @param value new value to be stored in this entry. |
698 |
> |
* @param value new value to be stored in this entry |
699 |
|
* @return (Does not return) |
700 |
|
* @throws UnsupportedOperationException always |
701 |
|
*/ |
721 |
|
* entry's key followed by the equals character ("<tt>=</tt>") |
722 |
|
* followed by the string representation of this entry's value. |
723 |
|
* |
724 |
< |
* @return a String representation of this map entry. |
724 |
> |
* @return a String representation of this map entry |
725 |
|
*/ |
726 |
|
public String toString() { |
727 |
|
return key + "=" + value; |