1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
6 |
< |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
5 |
> |
* This code is free software; you can redistribute it and/or modify it |
6 |
> |
* under the terms of the GNU General Public License version 2 only, as |
7 |
> |
* published by the Free Software Foundation. Sun designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Sun in the LICENSE file that accompanied this code. |
10 |
> |
* |
11 |
> |
* This code is distributed in the hope that it will be useful, but WITHOUT |
12 |
> |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 |
> |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
> |
* version 2 for more details (a copy is included in the LICENSE file that |
15 |
> |
* accompanied this code). |
16 |
> |
* |
17 |
> |
* You should have received a copy of the GNU General Public License version |
18 |
> |
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
> |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
> |
* |
21 |
> |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
> |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
> |
* have any questions. |
24 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
28 |
|
|
29 |
|
/** |
30 |
|
* This class provides a skeletal implementation of the <tt>Map</tt> |
31 |
< |
* interface, to minimize the effort required to implement this interface. <p> |
31 |
> |
* interface, to minimize the effort required to implement this interface. |
32 |
|
* |
33 |
< |
* To implement an unmodifiable map, the programmer needs only to extend this |
33 |
> |
* <p>To implement an unmodifiable map, the programmer needs only to extend this |
34 |
|
* class and provide an implementation for the <tt>entrySet</tt> method, which |
35 |
|
* returns a set-view of the map's mappings. Typically, the returned set |
36 |
|
* will, in turn, be implemented atop <tt>AbstractSet</tt>. This set should |
37 |
|
* not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator |
38 |
< |
* should not support the <tt>remove</tt> method.<p> |
38 |
> |
* should not support the <tt>remove</tt> method. |
39 |
|
* |
40 |
< |
* To implement a modifiable map, the programmer must additionally override |
40 |
> |
* <p>To implement a modifiable map, the programmer must additionally override |
41 |
|
* this class's <tt>put</tt> method (which otherwise throws an |
42 |
|
* <tt>UnsupportedOperationException</tt>), and the iterator returned by |
43 |
|
* <tt>entrySet().iterator()</tt> must additionally implement its |
44 |
< |
* <tt>remove</tt> method.<p> |
44 |
> |
* <tt>remove</tt> method. |
45 |
|
* |
46 |
< |
* The programmer should generally provide a void (no argument) and map |
46 |
> |
* <p>The programmer should generally provide a void (no argument) and map |
47 |
|
* constructor, as per the recommendation in the <tt>Map</tt> interface |
48 |
< |
* specification.<p> |
48 |
> |
* specification. |
49 |
|
* |
50 |
< |
* The documentation for each non-abstract methods in this class describes its |
50 |
> |
* <p>The documentation for each non-abstract method in this class describes its |
51 |
|
* implementation in detail. Each of these methods may be overridden if the |
52 |
< |
* map being implemented admits a more efficient implementation.<p> |
52 |
> |
* map being implemented admits a more efficient implementation. |
53 |
|
* |
54 |
< |
* This class is a member of the |
55 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
54 |
> |
* <p>This class is a member of the |
55 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
56 |
|
* Java Collections Framework</a>. |
57 |
|
* |
58 |
+ |
* @param <K> the type of keys maintained by this map |
59 |
+ |
* @param <V> the type of mapped values |
60 |
+ |
* |
61 |
|
* @author Josh Bloch |
62 |
|
* @author Neal Gafter |
63 |
|
* @version %I%, %G% |
77 |
|
// Query Operations |
78 |
|
|
79 |
|
/** |
80 |
< |
* 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> |
80 |
> |
* {@inheritDoc} |
81 |
|
* |
82 |
< |
* This implementation returns <tt>entrySet().size()</tt>. |
64 |
< |
* |
65 |
< |
* @return the number of key-value mappings in this map. |
82 |
> |
* <p>This implementation returns <tt>entrySet().size()</tt>. |
83 |
|
*/ |
84 |
|
public int size() { |
85 |
|
return entrySet().size(); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* Returns <tt>true</tt> if this map contains no key-value mappings. <p> |
73 |
< |
* |
74 |
< |
* This implementation returns <tt>size() == 0</tt>. |
89 |
> |
* {@inheritDoc} |
90 |
|
* |
91 |
< |
* @return <tt>true</tt> if this map contains no key-value mappings. |
91 |
> |
* <p>This implementation returns <tt>size() == 0</tt>. |
92 |
|
*/ |
93 |
|
public boolean isEmpty() { |
94 |
|
return size() == 0; |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* Returns <tt>true</tt> if this map maps one or more keys to this value. |
99 |
< |
* More formally, returns <tt>true</tt> if and only if this map contains |
100 |
< |
* at least one mapping to a value <tt>v</tt> such that <tt>(value==null ? |
101 |
< |
* v==null : value.equals(v))</tt>. This operation will probably require |
102 |
< |
* time linear in the map size for most implementations of map.<p> |
103 |
< |
* |
104 |
< |
* This implementation iterates over entrySet() searching for an entry |
105 |
< |
* with the specified value. If such an entry is found, <tt>true</tt> is |
106 |
< |
* returned. If the iteration terminates without finding such an entry, |
107 |
< |
* <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. |
98 |
> |
* {@inheritDoc} |
99 |
> |
* |
100 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching |
101 |
> |
* for an entry with the specified value. If such an entry is found, |
102 |
> |
* <tt>true</tt> is returned. If the iteration terminates without |
103 |
> |
* finding such an entry, <tt>false</tt> is returned. Note that this |
104 |
> |
* implementation requires linear time in the size of the map. |
105 |
> |
* |
106 |
> |
* @throws ClassCastException {@inheritDoc} |
107 |
> |
* @throws NullPointerException {@inheritDoc} |
108 |
|
*/ |
109 |
|
public boolean containsValue(Object value) { |
110 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* Returns <tt>true</tt> if this map contains a mapping for the specified |
129 |
< |
* key. <p> |
128 |
> |
* {@inheritDoc} |
129 |
> |
* |
130 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching |
131 |
> |
* for an entry with the specified key. If such an entry is found, |
132 |
> |
* <tt>true</tt> is returned. If the iteration terminates without |
133 |
> |
* finding such an entry, <tt>false</tt> is returned. Note that this |
134 |
> |
* implementation requires linear time in the size of the map; many |
135 |
> |
* implementations will override this method. |
136 |
|
* |
137 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an |
138 |
< |
* entry with the specified key. If such an entry is found, <tt>true</tt> |
123 |
< |
* is returned. If the iteration terminates without finding such an |
124 |
< |
* entry, <tt>false</tt> is returned. Note that this implementation |
125 |
< |
* requires linear time in the size of the map; many implementations will |
126 |
< |
* override this method. |
127 |
< |
* |
128 |
< |
* @param key key whose presence in this map is to be tested. |
129 |
< |
* @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. |
137 |
> |
* @throws ClassCastException {@inheritDoc} |
138 |
> |
* @throws NullPointerException {@inheritDoc} |
139 |
|
*/ |
140 |
|
public boolean containsKey(Object key) { |
141 |
|
Iterator<Map.Entry<K,V>> i = entrySet().iterator(); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
< |
* Returns the value to which this map maps the specified key. Returns |
160 |
< |
* <tt>null</tt> if the map contains no mapping for this key. A return |
161 |
< |
* value of <tt>null</tt> does not <i>necessarily</i> indicate that the |
162 |
< |
* map contains no mapping for the key; it's also possible that the map |
163 |
< |
* explicitly maps the key to <tt>null</tt>. The containsKey operation |
164 |
< |
* may be used to distinguish these two cases. <p> |
165 |
< |
* |
166 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an |
167 |
< |
* entry with the specified key. If such an entry is found, the entry's |
168 |
< |
* value is returned. If the iteration terminates without finding such an |
169 |
< |
* 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) |
159 |
> |
* {@inheritDoc} |
160 |
> |
* |
161 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching |
162 |
> |
* for an entry with the specified key. If such an entry is found, |
163 |
> |
* the entry's value is returned. If the iteration terminates without |
164 |
> |
* finding such an entry, <tt>null</tt> is returned. Note that this |
165 |
> |
* implementation requires linear time in the size of the map; many |
166 |
> |
* implementations will override this method. |
167 |
> |
* |
168 |
> |
* @throws ClassCastException {@inheritDoc} |
169 |
> |
* @throws NullPointerException {@inheritDoc} |
170 |
|
*/ |
171 |
|
public V get(Object key) { |
172 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
190 |
|
// Modification Operations |
191 |
|
|
192 |
|
/** |
193 |
< |
* 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> |
193 |
> |
* {@inheritDoc} |
194 |
|
* |
195 |
< |
* This implementation always throws an |
195 |
> |
* <p>This implementation always throws an |
196 |
|
* <tt>UnsupportedOperationException</tt>. |
197 |
|
* |
198 |
< |
* @param key key with which the specified value is to be associated. |
199 |
< |
* @param value value to be associated with the specified key. |
200 |
< |
* |
201 |
< |
* @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>. |
198 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
199 |
> |
* @throws ClassCastException {@inheritDoc} |
200 |
> |
* @throws NullPointerException {@inheritDoc} |
201 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
202 |
|
*/ |
203 |
|
public V put(K key, V value) { |
204 |
|
throw new UnsupportedOperationException(); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
< |
* Removes the mapping for this key from this map if present (optional |
233 |
< |
* operation). <p> |
208 |
> |
* {@inheritDoc} |
209 |
|
* |
210 |
< |
* This implementation iterates over <tt>entrySet()</tt> searching for an |
210 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt> searching for an |
211 |
|
* entry with the specified key. If such an entry is found, its value is |
212 |
|
* obtained with its <tt>getValue</tt> operation, the entry is removed |
213 |
< |
* from the Collection (and the backing map) with the iterator's |
213 |
> |
* from the collection (and the backing map) with the iterator's |
214 |
|
* <tt>remove</tt> operation, and the saved value is returned. If the |
215 |
|
* iteration terminates without finding such an entry, <tt>null</tt> is |
216 |
|
* returned. Note that this implementation requires linear time in the |
217 |
< |
* size of the map; many implementations will override this method.<p> |
217 |
> |
* size of the map; many implementations will override this method. |
218 |
> |
* |
219 |
> |
* <p>Note that this implementation throws an |
220 |
> |
* <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> |
221 |
> |
* iterator does not support the <tt>remove</tt> method and this map |
222 |
> |
* contains a mapping for the specified key. |
223 |
|
* |
224 |
< |
* Note that this implementation throws an |
225 |
< |
* <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> iterator |
226 |
< |
* 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. |
224 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
225 |
> |
* @throws ClassCastException {@inheritDoc} |
226 |
> |
* @throws NullPointerException {@inheritDoc} |
227 |
|
*/ |
228 |
|
public V remove(Object key) { |
229 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
254 |
|
// Bulk Operations |
255 |
|
|
256 |
|
/** |
257 |
< |
* 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> |
257 |
> |
* {@inheritDoc} |
258 |
|
* |
259 |
< |
* This implementation iterates over the specified map's |
259 |
> |
* <p>This implementation iterates over the specified map's |
260 |
|
* <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt> |
261 |
< |
* operation once for each entry returned by the iteration.<p> |
261 |
> |
* operation once for each entry returned by the iteration. |
262 |
|
* |
263 |
< |
* Note that this implementation throws an |
263 |
> |
* <p>Note that this implementation throws an |
264 |
|
* <tt>UnsupportedOperationException</tt> if this map does not support |
265 |
|
* the <tt>put</tt> operation and the specified map is nonempty. |
266 |
|
* |
267 |
< |
* @param t mappings to be stored in this map. |
268 |
< |
* |
269 |
< |
* @throws UnsupportedOperationException if the <tt>putAll</tt> operation |
270 |
< |
* is not supported by this map. |
271 |
< |
* |
272 |
< |
* @throws ClassCastException if the class of a key or value in the |
273 |
< |
* specified map prevents it from being stored in this map. |
274 |
< |
* |
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. |
312 |
< |
*/ |
313 |
< |
public void putAll(Map<? extends K, ? extends V> t) { |
314 |
< |
Iterator<? extends Entry<? extends K, ? extends V>> i = t.entrySet().iterator(); |
315 |
< |
while (i.hasNext()) { |
316 |
< |
Entry<? extends K, ? extends V> e = i.next(); |
317 |
< |
put(e.getKey(), e.getValue()); |
318 |
< |
} |
267 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
268 |
> |
* @throws ClassCastException {@inheritDoc} |
269 |
> |
* @throws NullPointerException {@inheritDoc} |
270 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
271 |
> |
*/ |
272 |
> |
public void putAll(Map<? extends K, ? extends V> m) { |
273 |
> |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
274 |
> |
put(e.getKey(), e.getValue()); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
< |
* Removes all mappings from this map (optional operation). <p> |
278 |
> |
* {@inheritDoc} |
279 |
|
* |
280 |
< |
* This implementation calls <tt>entrySet().clear()</tt>. |
280 |
> |
* <p>This implementation calls <tt>entrySet().clear()</tt>. |
281 |
|
* |
282 |
< |
* Note that this implementation throws an |
282 |
> |
* <p>Note that this implementation throws an |
283 |
|
* <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> |
284 |
|
* does not support the <tt>clear</tt> operation. |
285 |
|
* |
286 |
< |
* @throws UnsupportedOperationException clear is not supported |
331 |
< |
* by this map. |
286 |
> |
* @throws UnsupportedOperationException {@inheritDoc} |
287 |
|
*/ |
288 |
|
public void clear() { |
289 |
|
entrySet().clear(); |
301 |
|
transient volatile Collection<V> values = null; |
302 |
|
|
303 |
|
/** |
304 |
< |
* Returns a Set view of the keys contained in this map. The Set is |
305 |
< |
* backed by the map, so changes to the map are reflected in the Set, |
306 |
< |
* and vice-versa. (If the map is modified while an iteration over |
307 |
< |
* the Set is in progress, the results of the iteration are undefined.) |
308 |
< |
* The Set supports element removal, which removes the corresponding entry |
309 |
< |
* from the map, via the Iterator.remove, Set.remove, removeAll |
310 |
< |
* retainAll, and clear operations. It does not support the add or |
311 |
< |
* 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> |
304 |
> |
* {@inheritDoc} |
305 |
> |
* |
306 |
> |
* <p>This implementation returns a set that subclasses {@link AbstractSet}. |
307 |
> |
* The subclass's iterator method returns a "wrapper object" over this |
308 |
> |
* map's <tt>entrySet()</tt> iterator. The <tt>size</tt> method |
309 |
> |
* delegates to this map's <tt>size</tt> method and the |
310 |
> |
* <tt>contains</tt> method delegates to this map's |
311 |
> |
* <tt>containsKey</tt> method. |
312 |
|
* |
313 |
< |
* The Set is created the first time this method is called, |
313 |
> |
* <p>The set is created the first time this method is called, |
314 |
|
* and returned in response to all subsequent calls. No synchronization |
315 |
|
* is performed, so there is a slight chance that multiple calls to this |
316 |
< |
* method will not all return the same Set. |
368 |
< |
* |
369 |
< |
* @return a Set view of the keys contained in this map. |
316 |
> |
* method will not all return the same set. |
317 |
|
*/ |
318 |
|
public Set<K> keySet() { |
319 |
|
if (keySet == null) { |
340 |
|
return AbstractMap.this.size(); |
341 |
|
} |
342 |
|
|
343 |
+ |
public boolean isEmpty() { |
344 |
+ |
return AbstractMap.this.isEmpty(); |
345 |
+ |
} |
346 |
+ |
|
347 |
+ |
public void clear() { |
348 |
+ |
AbstractMap.this.clear(); |
349 |
+ |
} |
350 |
+ |
|
351 |
|
public boolean contains(Object k) { |
352 |
|
return AbstractMap.this.containsKey(k); |
353 |
|
} |
357 |
|
} |
358 |
|
|
359 |
|
/** |
360 |
< |
* Returns a collection view of the values contained in this map. The |
361 |
< |
* collection is backed by the map, so changes to the map are reflected in |
362 |
< |
* the collection, and vice-versa. (If the map is modified while an |
363 |
< |
* iteration over the collection is in progress, the results of the |
364 |
< |
* iteration are undefined.) The collection supports element removal, |
365 |
< |
* which removes the corresponding entry from the map, via the |
366 |
< |
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
367 |
< |
* <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> |
360 |
> |
* {@inheritDoc} |
361 |
> |
* |
362 |
> |
* <p>This implementation returns a collection that subclasses {@link |
363 |
> |
* AbstractCollection}. The subclass's iterator method returns a |
364 |
> |
* "wrapper object" over this map's <tt>entrySet()</tt> iterator. |
365 |
> |
* The <tt>size</tt> method delegates to this map's <tt>size</tt> |
366 |
> |
* method and the <tt>contains</tt> method delegates to this map's |
367 |
> |
* <tt>containsValue</tt> method. |
368 |
|
* |
369 |
< |
* The collection is created the first time this method is called, and |
369 |
> |
* <p>The collection is created the first time this method is called, and |
370 |
|
* returned in response to all subsequent calls. No synchronization is |
371 |
|
* performed, so there is a slight chance that multiple calls to this |
372 |
< |
* method will not all return the same Collection. |
425 |
< |
* |
426 |
< |
* @return a collection view of the values contained in this map. |
372 |
> |
* method will not all return the same collection. |
373 |
|
*/ |
374 |
|
public Collection<V> values() { |
375 |
|
if (values == null) { |
396 |
|
return AbstractMap.this.size(); |
397 |
|
} |
398 |
|
|
399 |
+ |
public boolean isEmpty() { |
400 |
+ |
return AbstractMap.this.isEmpty(); |
401 |
+ |
} |
402 |
+ |
|
403 |
+ |
public void clear() { |
404 |
+ |
AbstractMap.this.clear(); |
405 |
+ |
} |
406 |
+ |
|
407 |
|
public boolean contains(Object v) { |
408 |
|
return AbstractMap.this.containsValue(v); |
409 |
|
} |
412 |
|
return values; |
413 |
|
} |
414 |
|
|
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 |
– |
*/ |
415 |
|
public abstract Set<Entry<K,V>> entrySet(); |
416 |
|
|
417 |
|
|
420 |
|
/** |
421 |
|
* Compares the specified object with this map for equality. Returns |
422 |
|
* <tt>true</tt> if the given object is also a map and the two maps |
423 |
< |
* represent the same mappings. More formally, two maps <tt>t1</tt> and |
424 |
< |
* <tt>t2</tt> represent the same mappings if |
425 |
< |
* <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 |
423 |
> |
* represent the same mappings. More formally, two maps <tt>m1</tt> and |
424 |
> |
* <tt>m2</tt> represent the same mappings if |
425 |
> |
* <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the |
426 |
|
* <tt>equals</tt> method works properly across different implementations |
427 |
< |
* of the map interface.<p> |
427 |
> |
* of the <tt>Map</tt> interface. |
428 |
|
* |
429 |
< |
* This implementation first checks if the specified object is this map; |
429 |
> |
* <p>This implementation first checks if the specified object is this map; |
430 |
|
* if so it returns <tt>true</tt>. Then, it checks if the specified |
431 |
< |
* object is a map whose size is identical to the size of this set; if |
431 |
> |
* object is a map whose size is identical to the size of this map; if |
432 |
|
* not, it returns <tt>false</tt>. If so, it iterates over this map's |
433 |
|
* <tt>entrySet</tt> collection, and checks that the specified map |
434 |
|
* contains each mapping that this map contains. If the specified map |
435 |
|
* fails to contain such a mapping, <tt>false</tt> is returned. If the |
436 |
|
* iteration completes, <tt>true</tt> is returned. |
437 |
|
* |
438 |
< |
* @param o object to be compared for equality with this map. |
439 |
< |
* @return <tt>true</tt> if the specified object is equal to this map. |
438 |
> |
* @param o object to be compared for equality with this map |
439 |
> |
* @return <tt>true</tt> if the specified object is equal to this map |
440 |
|
*/ |
441 |
|
public boolean equals(Object o) { |
442 |
|
if (o == this) |
444 |
|
|
445 |
|
if (!(o instanceof Map)) |
446 |
|
return false; |
447 |
< |
Map<K,V> t = (Map<K,V>) o; |
448 |
< |
if (t.size() != size()) |
447 |
> |
Map<K,V> m = (Map<K,V>) o; |
448 |
> |
if (m.size() != size()) |
449 |
|
return false; |
450 |
|
|
451 |
|
try { |
455 |
|
K key = e.getKey(); |
456 |
|
V value = e.getValue(); |
457 |
|
if (value == null) { |
458 |
< |
if (!(t.get(key)==null && t.containsKey(key))) |
458 |
> |
if (!(m.get(key)==null && m.containsKey(key))) |
459 |
|
return false; |
460 |
|
} else { |
461 |
< |
if (!value.equals(t.get(key))) |
461 |
> |
if (!value.equals(m.get(key))) |
462 |
|
return false; |
463 |
|
} |
464 |
|
} |
465 |
< |
} catch(ClassCastException unused) { |
465 |
> |
} catch (ClassCastException unused) { |
466 |
|
return false; |
467 |
< |
} catch(NullPointerException unused) { |
467 |
> |
} catch (NullPointerException unused) { |
468 |
|
return false; |
469 |
|
} |
470 |
|
|
474 |
|
/** |
475 |
|
* Returns the hash code value for this map. The hash code of a map is |
476 |
|
* defined to be the sum of the hash codes of each entry in the map's |
477 |
< |
* <tt>entrySet()</tt> view. This ensures that <tt>t1.equals(t2)</tt> |
478 |
< |
* implies that <tt>t1.hashCode()==t2.hashCode()</tt> for any two maps |
479 |
< |
* <tt>t1</tt> and <tt>t2</tt>, as required by the general contract of |
480 |
< |
* Object.hashCode.<p> |
481 |
< |
* |
482 |
< |
* This implementation iterates over <tt>entrySet()</tt>, calling |
483 |
< |
* <tt>hashCode</tt> on each element (entry) in the Collection, and adding |
484 |
< |
* up the results. |
477 |
> |
* <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt> |
478 |
> |
* implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps |
479 |
> |
* <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of |
480 |
> |
* {@link Object#hashCode}. |
481 |
> |
* |
482 |
> |
* <p>This implementation iterates over <tt>entrySet()</tt>, calling |
483 |
> |
* {@link Map.Entry#hashCode hashCode()} on each element (entry) in the |
484 |
> |
* set, and adding up the results. |
485 |
|
* |
486 |
< |
* @return the hash code value for this map. |
486 |
> |
* @return the hash code value for this map |
487 |
|
* @see Map.Entry#hashCode() |
549 |
– |
* @see Object#hashCode() |
488 |
|
* @see Object#equals(Object) |
489 |
|
* @see Set#equals(Object) |
490 |
|
*/ |
504 |
|
* <tt>", "</tt> (comma and space). Each key-value mapping is rendered as |
505 |
|
* the key followed by an equals sign (<tt>"="</tt>) followed by the |
506 |
|
* associated value. Keys and values are converted to strings as by |
507 |
< |
* <tt>String.valueOf(Object)</tt>.<p> |
570 |
< |
* |
571 |
< |
* This implementation creates an empty string buffer, appends a left |
572 |
< |
* brace, and iterates over the map's <tt>entrySet</tt> view, appending |
573 |
< |
* the string representation of each <tt>map.entry</tt> in turn. After |
574 |
< |
* appending each entry except the last, the string <tt>", "</tt> is |
575 |
< |
* appended. Finally a right brace is appended. A string is obtained |
576 |
< |
* from the stringbuffer, and returned. |
507 |
> |
* {@link String#valueOf(Object)}. |
508 |
|
* |
509 |
< |
* @return a String representation of this map. |
509 |
> |
* @return a string representation of this map |
510 |
|
*/ |
511 |
|
public String toString() { |
581 |
– |
StringBuffer buf = new StringBuffer(); |
582 |
– |
buf.append("{"); |
583 |
– |
|
512 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
513 |
< |
boolean hasNext = i.hasNext(); |
514 |
< |
while (hasNext) { |
513 |
> |
if (! i.hasNext()) |
514 |
> |
return "{}"; |
515 |
> |
|
516 |
> |
StringBuilder sb = new StringBuilder(); |
517 |
> |
sb.append('{'); |
518 |
> |
for (;;) { |
519 |
|
Entry<K,V> e = i.next(); |
520 |
|
K key = e.getKey(); |
521 |
< |
V value = e.getValue(); |
522 |
< |
if (key == this) |
523 |
< |
buf.append("(this Map)"); |
524 |
< |
else |
525 |
< |
buf.append(key); |
526 |
< |
buf.append("="); |
527 |
< |
if (value == this) |
528 |
< |
buf.append("(this Map)"); |
597 |
< |
else |
598 |
< |
buf.append(value); |
599 |
< |
hasNext = i.hasNext(); |
600 |
< |
if (hasNext) |
601 |
< |
buf.append(", "); |
602 |
< |
} |
603 |
< |
|
604 |
< |
buf.append("}"); |
605 |
< |
return buf.toString(); |
521 |
> |
V value = e.getValue(); |
522 |
> |
sb.append(key == this ? "(this Map)" : key); |
523 |
> |
sb.append('='); |
524 |
> |
sb.append(value == this ? "(this Map)" : value); |
525 |
> |
if (! i.hasNext()) |
526 |
> |
return sb.append('}').toString(); |
527 |
> |
sb.append(", "); |
528 |
> |
} |
529 |
|
} |
530 |
< |
|
530 |
> |
|
531 |
|
/** |
532 |
|
* Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys |
533 |
|
* and values themselves are not cloned. |
534 |
|
* |
535 |
< |
* @return a shallow copy of this map. |
535 |
> |
* @return a shallow copy of this map |
536 |
|
*/ |
537 |
|
protected Object clone() throws CloneNotSupportedException { |
538 |
|
AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone(); |
546 |
|
* Test for equality, checking for nulls. |
547 |
|
*/ |
548 |
|
private static boolean eq(Object o1, Object o2) { |
549 |
< |
return (o1 == null ? o2 == null : o1.equals(o2)); |
549 |
> |
return o1 == null ? o2 == null : o1.equals(o2); |
550 |
|
} |
551 |
|
|
552 |
|
// Implementation Note: SimpleEntry and SimpleImmutableEntry |
563 |
|
* facilitates the process of building custom map |
564 |
|
* implementations. For example, it may be convenient to return |
565 |
|
* arrays of <tt>SimpleEntry</tt> instances in method |
566 |
< |
* <tt>Map.entrySet().toArray</tt> |
566 |
> |
* <tt>Map.entrySet().toArray</tt>. |
567 |
> |
* |
568 |
> |
* @since 1.6 |
569 |
|
*/ |
570 |
< |
public static class SimpleEntry<K,V> implements Entry<K,V> { |
570 |
> |
public static class SimpleEntry<K,V> |
571 |
> |
implements Entry<K,V>, java.io.Serializable |
572 |
> |
{ |
573 |
> |
private static final long serialVersionUID = -8499721149061103585L; |
574 |
> |
|
575 |
|
private final K key; |
576 |
|
private V value; |
577 |
|
|
591 |
|
* Creates an entry representing the same mapping as the |
592 |
|
* specified entry. |
593 |
|
* |
594 |
< |
* @param entry the entry to copy. |
594 |
> |
* @param entry the entry to copy |
595 |
|
*/ |
596 |
|
public SimpleEntry(Entry<? extends K, ? extends V> entry) { |
597 |
|
this.key = entry.getKey(); |
601 |
|
/** |
602 |
|
* Returns the key corresponding to this entry. |
603 |
|
* |
604 |
< |
* @return the key corresponding to this entry. |
604 |
> |
* @return the key corresponding to this entry |
605 |
|
*/ |
606 |
|
public K getKey() { |
607 |
|
return key; |
610 |
|
/** |
611 |
|
* Returns the value corresponding to this entry. |
612 |
|
* |
613 |
< |
* @return the value corresponding to this entry. |
613 |
> |
* @return the value corresponding to this entry |
614 |
|
*/ |
615 |
|
public V getValue() { |
616 |
|
return value; |
620 |
|
* Replaces the value corresponding to this entry with the specified |
621 |
|
* value. |
622 |
|
* |
623 |
< |
* @param value new value to be stored in this entry. |
624 |
< |
* @return old value corresponding to the entry. |
623 |
> |
* @param value new value to be stored in this entry |
624 |
> |
* @return the old value corresponding to the entry |
625 |
|
*/ |
626 |
|
public V setValue(V value) { |
627 |
|
V oldValue = this.value; |
629 |
|
return oldValue; |
630 |
|
} |
631 |
|
|
632 |
+ |
/** |
633 |
+ |
* Compares the specified object with this entry for equality. |
634 |
+ |
* Returns {@code true} if the given object is also a map entry and |
635 |
+ |
* the two entries represent the same mapping. More formally, two |
636 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
637 |
+ |
* if<pre> |
638 |
+ |
* (e1.getKey()==null ? |
639 |
+ |
* e2.getKey()==null : |
640 |
+ |
* e1.getKey().equals(e2.getKey())) |
641 |
+ |
* && |
642 |
+ |
* (e1.getValue()==null ? |
643 |
+ |
* e2.getValue()==null : |
644 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
645 |
+ |
* This ensures that the {@code equals} method works properly across |
646 |
+ |
* different implementations of the {@code Map.Entry} interface. |
647 |
+ |
* |
648 |
+ |
* @param o object to be compared for equality with this map entry |
649 |
+ |
* @return {@code true} if the specified object is equal to this map |
650 |
+ |
* entry |
651 |
+ |
* @see #hashCode |
652 |
+ |
*/ |
653 |
|
public boolean equals(Object o) { |
654 |
|
if (!(o instanceof Map.Entry)) |
655 |
|
return false; |
657 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
658 |
|
} |
659 |
|
|
660 |
+ |
/** |
661 |
+ |
* Returns the hash code value for this map entry. The hash code |
662 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
663 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
664 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
665 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
666 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
667 |
+ |
* {@code e1} and {@code e2}, as required by the general |
668 |
+ |
* contract of {@link Object#hashCode}. |
669 |
+ |
* |
670 |
+ |
* @return the hash code value for this map entry |
671 |
+ |
* @see #equals |
672 |
+ |
*/ |
673 |
|
public int hashCode() { |
674 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
675 |
< |
((value == null) ? 0 : value.hashCode()); |
674 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
675 |
> |
(value == null ? 0 : value.hashCode()); |
676 |
|
} |
677 |
|
|
678 |
|
/** |
681 |
|
* entry's key followed by the equals character ("<tt>=</tt>") |
682 |
|
* followed by the string representation of this entry's value. |
683 |
|
* |
684 |
< |
* @return a String representation of this map entry. |
684 |
> |
* @return a String representation of this map entry |
685 |
|
*/ |
686 |
|
public String toString() { |
687 |
|
return key + "=" + value; |
690 |
|
} |
691 |
|
|
692 |
|
/** |
693 |
< |
* An Entry maintaining an immutable key and value, This class |
693 |
> |
* An Entry maintaining an immutable key and value. This class |
694 |
|
* does not support method <tt>setValue</tt>. This class may be |
695 |
|
* convenient in methods that return thread-safe snapshots of |
696 |
|
* key-value mappings. |
697 |
+ |
* |
698 |
+ |
* @since 1.6 |
699 |
|
*/ |
700 |
< |
public static class SimpleImmutableEntry<K,V> implements Entry<K,V> { |
700 |
> |
public static class SimpleImmutableEntry<K,V> |
701 |
> |
implements Entry<K,V>, java.io.Serializable |
702 |
> |
{ |
703 |
> |
private static final long serialVersionUID = 7138329143949025153L; |
704 |
> |
|
705 |
|
private final K key; |
706 |
|
private final V value; |
707 |
|
|
721 |
|
* Creates an entry representing the same mapping as the |
722 |
|
* specified entry. |
723 |
|
* |
724 |
< |
* @param entry the entry to copy. |
724 |
> |
* @param entry the entry to copy |
725 |
|
*/ |
726 |
|
public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) { |
727 |
|
this.key = entry.getKey(); |
731 |
|
/** |
732 |
|
* Returns the key corresponding to this entry. |
733 |
|
* |
734 |
< |
* @return the key corresponding to this entry. |
734 |
> |
* @return the key corresponding to this entry |
735 |
|
*/ |
736 |
|
public K getKey() { |
737 |
|
return key; |
740 |
|
/** |
741 |
|
* Returns the value corresponding to this entry. |
742 |
|
* |
743 |
< |
* @return the value corresponding to this entry. |
743 |
> |
* @return the value corresponding to this entry |
744 |
|
*/ |
745 |
|
public V getValue() { |
746 |
|
return value; |
752 |
|
* <tt>UnsupportedOperationException</tt>, as this class implements |
753 |
|
* an <i>immutable</i> map entry. |
754 |
|
* |
755 |
< |
* @param value new value to be stored in this entry. |
755 |
> |
* @param value new value to be stored in this entry |
756 |
|
* @return (Does not return) |
757 |
|
* @throws UnsupportedOperationException always |
758 |
|
*/ |
760 |
|
throw new UnsupportedOperationException(); |
761 |
|
} |
762 |
|
|
763 |
+ |
/** |
764 |
+ |
* Compares the specified object with this entry for equality. |
765 |
+ |
* Returns {@code true} if the given object is also a map entry and |
766 |
+ |
* the two entries represent the same mapping. More formally, two |
767 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
768 |
+ |
* if<pre> |
769 |
+ |
* (e1.getKey()==null ? |
770 |
+ |
* e2.getKey()==null : |
771 |
+ |
* e1.getKey().equals(e2.getKey())) |
772 |
+ |
* && |
773 |
+ |
* (e1.getValue()==null ? |
774 |
+ |
* e2.getValue()==null : |
775 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
776 |
+ |
* This ensures that the {@code equals} method works properly across |
777 |
+ |
* different implementations of the {@code Map.Entry} interface. |
778 |
+ |
* |
779 |
+ |
* @param o object to be compared for equality with this map entry |
780 |
+ |
* @return {@code true} if the specified object is equal to this map |
781 |
+ |
* entry |
782 |
+ |
* @see #hashCode |
783 |
+ |
*/ |
784 |
|
public boolean equals(Object o) { |
785 |
|
if (!(o instanceof Map.Entry)) |
786 |
|
return false; |
788 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
789 |
|
} |
790 |
|
|
791 |
+ |
/** |
792 |
+ |
* Returns the hash code value for this map entry. The hash code |
793 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
794 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
795 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
796 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
797 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
798 |
+ |
* {@code e1} and {@code e2}, as required by the general |
799 |
+ |
* contract of {@link Object#hashCode}. |
800 |
+ |
* |
801 |
+ |
* @return the hash code value for this map entry |
802 |
+ |
* @see #equals |
803 |
+ |
*/ |
804 |
|
public int hashCode() { |
805 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
806 |
< |
((value == null) ? 0 : value.hashCode()); |
805 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
806 |
> |
(value == null ? 0 : value.hashCode()); |
807 |
|
} |
808 |
|
|
809 |
|
/** |
812 |
|
* entry's key followed by the equals character ("<tt>=</tt>") |
813 |
|
* followed by the string representation of this entry's value. |
814 |
|
* |
815 |
< |
* @return a String representation of this map entry. |
815 |
> |
* @return a String representation of this map entry |
816 |
|
*/ |
817 |
|
public String toString() { |
818 |
|
return key + "=" + value; |