1 *java.util.Map* *Map* An object that maps keys to values.
3 public interface interface Map<K,V>
6 |java.util.Map_Description|
8 |java.util.Map_Constructors|
9 |java.util.Map_Methods|
11 ================================================================================
13 *java.util.Map_Methods*
14 |java.util.Map.clear()|Removes all of the mappings from this map (optional oper
15 |java.util.Map.containsKey(Object)|Returns true if this map contains a mapping
16 |java.util.Map.containsValue(Object)|Returns true if this map maps one or more
17 |java.util.Map.entrySet()|Returns aSetview of the mappings contained in this ma
18 |java.util.Map.equals(Object)|Compares the specified object with this map for e
19 |java.util.Map.get(Object)|Returns the value to which the specified key is mapp
20 |java.util.Map.hashCode()|Returns the hash code value for this map.
21 |java.util.Map.isEmpty()|Returns true if this map contains no key-value mapping
22 |java.util.Map.keySet()|Returns aSetview of the keys contained in this map.
23 |java.util.Map.put(K,V)|Associates the specified value with the specified key i
24 |java.util.Map.putAll(Map<?extendsK,?extendsV>)|Copies all of the mappings from
25 |java.util.Map.remove(Object)|Removes the mapping for a key from this map if it
26 |java.util.Map.size()|Returns the number of key-value mappings in this map.
27 |java.util.Map.values()|Returns aCollectionview of the values contained in this
29 *java.util.Map_Description*
31 An object that maps keys to values. A map cannot contain duplicate keys; each
32 key can map to at most one value.
34 This interface takes the place of the Dictionary class, which was a totally
35 abstract class rather than an interface.
37 The Map interface provides three collection views, which allow a map's contents
38 to be viewed as a set of keys, collection of values, or set of key-value
39 mappings. The order of a map is defined as the order in which the iterators on
40 the map's collection views return their elements. Some map implementations,
41 like the TreeMap class, make specific guarantees as to their order; others,
42 like the HashMap class, do not.
44 Note: great care must be exercised if mutable objects are used as map keys. The
45 behavior of a map is not specified if the value of an object is changed in a
46 manner that affects equals comparisons while the object is a key in the map. A
47 special case of this prohibition is that it is not permissible for a map to
48 contain itself as a key. While it is permissible for a map to contain itself as
49 a value, extreme caution is advised: the equals and hashCode methods are no
50 longer well defined on such a map.
52 All general-purpose map implementation classes should provide two "standard"
53 constructors: a void (no arguments) constructor which creates an empty map, and
54 a constructor with a single argument of type Map, which creates a new map with
55 the same key-value mappings as its argument. In effect, the latter constructor
56 allows the user to copy any map, producing an equivalent map of the desired
57 class. There is no way to enforce this recommendation (as interfaces cannot
58 contain constructors) but all of the general-purpose map implementations in the
61 The "destructive" methods contained in this interface, that is, the methods
62 that modify the map on which they operate, are specified to throw
63 UnsupportedOperationException if this map does not support the operation. If
64 this is the case, these methods may, but are not required to, throw an
65 UnsupportedOperationException if the invocation would have no effect on the
66 map. For example, invoking the (|java.util.Map|) method on an unmodifiable map
67 may, but is not required to, throw the exception if the map whose mappings are
68 to be "superimposed" is empty.
70 Some map implementations have restrictions on the keys and values they may
71 contain. For example, some implementations prohibit null keys and values, and
72 some have restrictions on the types of their keys. Attempting to insert an
73 ineligible key or value throws an unchecked exception, typically
74 NullPointerException or ClassCastException. Attempting to query the presence of
75 an ineligible key or value may throw an exception, or it may simply return
76 false; some implementations will exhibit the former behavior and some will
77 exhibit the latter. More generally, attempting an operation on an ineligible
78 key or value whose completion would not result in the insertion of an
79 ineligible element into the map may throw an exception or it may succeed, at
80 the option of the implementation. Such exceptions are marked as "optional" in
81 the specification for this interface.
83 This interface is a member of the <a
84 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
86 Many methods in Collections Framework interfaces are defined in terms of the
87 equals(|java.lang.Object|) method. For example, the specification for the
88 containsKey(Object key)(|java.util.Map|) method says: "returns true if and only
89 if this map contains a mapping for a key k such that (key==null ? k==null :
90 key.equals(k))." This specification should not be construed to imply that
91 invoking Map.containsKey with a non-null argument key will cause key.equals(k)
92 to be invoked for any key k. Implementations are free to implement
93 optimizations whereby the equals invocation is avoided, for example, by first
94 comparing the hash codes of the two keys. (The (|java.lang.Object|)
95 specification guarantees that two objects with unequal hash codes cannot be
96 equal.) More generally, implementations of the various Collections Framework
97 interfaces are free to take advantage of the specified behavior of underlying
98 (|java.lang.Object|) methods wherever the implementor deems it appropriate.
102 *java.util.Map.clear()*
106 Removes all of the mappings from this map (optional operation). The map will be
107 empty after this call returns.
111 *java.util.Map.containsKey(Object)*
113 public boolean containsKey(java.lang.Object key)
115 Returns true if this map contains a mapping for the specified key. More
116 formally, returns true if and only if this map contains a mapping for a key k
117 such that (key==null ? k==null : key.equals(k)). (There can be at most one such
121 key - key whose presence in this map is to be tested
123 Returns: true if this map contains a mapping for the specified key
125 *java.util.Map.containsValue(Object)*
127 public boolean containsValue(java.lang.Object value)
129 Returns true if this map maps one or more keys to the specified value. More
130 formally, returns true if and only if this map contains at least one mapping to
131 a value v such that (value==null ? v==null : value.equals(v)). This operation
132 will probably require time linear in the map size for most implementations of
136 value - value whose presence in this map is to be tested
138 Returns: true if this map maps one or more keys to the specified value
140 *java.util.Map.entrySet()*
142 public |java.util.Set|<Entry<K,V>> entrySet()
144 Returns a (|java.util.Set|) view of the mappings contained in this map. The set
145 is backed by the map, so changes to the map are reflected in the set, and
146 vice-versa. If the map is modified while an iteration over the set is in
147 progress (except through the iterator's own remove operation, or through the
148 setValue operation on a map entry returned by the iterator) the results of the
149 iteration are undefined. The set supports element removal, which removes the
150 corresponding mapping from the map, via the Iterator.remove, Set.remove,
151 removeAll, retainAll and clear operations. It does not support the add or
156 Returns: a set view of the mappings contained in this map
158 *java.util.Map.equals(Object)*
160 public boolean equals(java.lang.Object o)
162 Compares the specified object with this map for equality. Returns true if the
163 given object is also a map and the two maps represent the same mappings. More
164 formally, two maps m1 and m2 represent the same mappings if
165 m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works
166 properly across different implementations of the Map interface.
169 o - object to be compared for equality with this map
171 Returns: true if the specified object is equal to this map
173 *java.util.Map.get(Object)*
175 public |V| get(java.lang.Object key)
177 Returns the value to which the specified key is mapped, ornullif this map
178 contains no mapping for the key.
180 More formally, if this map contains a mapping from a keykto a valuevsuch
181 that(key==null ? k==null : key.equals(k)), then this method returnsv; otherwise
182 it returnsnull. (There can be at most one such mapping.)
184 If this map permits null values, then a return value ofnulldoes not necessarily
185 indicate that the map contains no mapping for the key; it's also possible that
186 the map explicitly maps the key tonull. The containsKey(|java.util.Map|)
187 operation may be used to distinguish these two cases.
190 key - the key whose associated value is to be returned
192 Returns: the value to which the specified key is mapped, or {@code null} if this map
193 contains no mapping for the key
195 *java.util.Map.hashCode()*
197 public int hashCode()
199 Returns the hash code value for this map. The hash code of a map is defined to
200 be the sum of the hash codes of each entry in the map's entrySet() view. This
201 ensures that m1.equals(m2) implies that m1.hashCode()==m2.hashCode() for any
202 two maps m1 and m2, as required by the general contract of (|java.lang.Object|)
207 Returns: the hash code value for this map
209 *java.util.Map.isEmpty()*
211 public boolean isEmpty()
213 Returns true if this map contains no key-value mappings.
217 Returns: true if this map contains no key-value mappings
219 *java.util.Map.keySet()*
221 public |java.util.Set|<K> keySet()
223 Returns a (|java.util.Set|) view of the keys contained in this map. The set is
224 backed by the map, so changes to the map are reflected in the set, and
225 vice-versa. If the map is modified while an iteration over the set is in
226 progress (except through the iterator's own remove operation), the results of
227 the iteration are undefined. The set supports element removal, which removes
228 the corresponding mapping from the map, via the Iterator.remove, Set.remove,
229 removeAll, retainAll, and clear operations. It does not support the add or
234 Returns: a set view of the keys contained in this map
236 *java.util.Map.put(K,V)*
242 Associates the specified value with the specified key in this map (optional
243 operation). If the map previously contained a mapping for the key, the old
244 value is replaced by the specified value. (A map m is said to contain a mapping
245 for a key k if and only if m.containsKey(k)(|java.util.Map|) would return
249 key - key with which the specified value is to be associated
250 value - value to be associated with the specified key
252 Returns: the previous value associated with key, or null if there was no mapping for
253 key. (A null return can also indicate that the map previously
254 associated null with key, if the implementation supports null
257 *java.util.Map.putAll(Map<?extendsK,?extendsV>)*
259 public void putAll(java.util.Map<? extends K, ? extends V> m)
261 Copies all of the mappings from the specified map to this map (optional
262 operation). The effect of this call is equivalent to that of calling put(k,
263 v)(|java.util.Map|) on this map once for each mapping from key k to value v in
264 the specified map. The behavior of this operation is undefined if the specified
265 map is modified while the operation is in progress.
268 m - mappings to be stored in this map
270 *java.util.Map.remove(Object)*
272 public |V| remove(java.lang.Object key)
274 Removes the mapping for a key from this map if it is present (optional
275 operation). More formally, if this map contains a mapping from key k to value v
276 such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The
277 map can contain at most one such mapping.)
279 Returns the value to which this map previously associated the key, or null if
280 the map contained no mapping for the key.
282 If this map permits null values, then a return value of null does not
283 necessarily indicate that the map contained no mapping for the key; it's also
284 possible that the map explicitly mapped the key to null.
286 The map will not contain a mapping for the specified key once the call returns.
289 key - key whose mapping is to be removed from the map
291 Returns: the previous value associated with key, or null if there was no mapping for
294 *java.util.Map.size()*
298 Returns the number of key-value mappings in this map. If the map contains more
299 than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
303 Returns: the number of key-value mappings in this map
305 *java.util.Map.values()*
307 public |java.util.Collection|<V> values()
309 Returns a (|java.util.Collection|) view of the values contained in this map.
310 The collection is backed by the map, so changes to the map are reflected in the
311 collection, and vice-versa. If the map is modified while an iteration over the
312 collection is in progress (except through the iterator's own remove operation),
313 the results of the iteration are undefined. The collection supports element
314 removal, which removes the corresponding mapping from the map, via the
315 Iterator.remove, Collection.remove, removeAll, retainAll and clear operations.
316 It does not support the add or addAll operations.
320 Returns: a collection view of the values contained in this map