1 *java.util.Map* *Map* An object that maps keys to values.
3 public interface interface Map
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 mappings from this map (optional operation).
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 a set view of the mappings contained in this
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 this map maps the specifi
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 a set view 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)|Copies all of the mappings from the specified map to
25 |java.util.Map.remove(Object)|Removes the mapping for this key from this map if
26 |java.util.Map.size()|Returns the number of key-value mappings in this map.
27 |java.util.Map.values()|Returns a collection view of the values contained in th
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 a 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 href="/../guide/collections/index.html">
84 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 contains(Object key)(|java.util.Map|) method says: "returns true if and only if
89 this map contain 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.
101 *java.util.Map.clear()*
105 Removes all mappings from this map (optional operation).
108 *java.util.Map.containsKey(Object)*
110 public boolean containsKey(java.lang.Object key)
112 Returns true if this map contains a mapping for the specified key. More
113 formally, returns true if and only if this map contains a mapping for a key k
114 such that (key==null ? k==null : key.equals(k)). (There can be at most one such
117 key - key whose presence in this map is to be tested.
119 Returns: true if this map contains a mapping for the specified key.
120 *java.util.Map.containsValue(Object)*
122 public boolean containsValue(java.lang.Object value)
124 Returns true if this map maps one or more keys to the specified value. More
125 formally, returns true if and only if this map contains at least one mapping to
126 a value v such that (value==null ? v==null : value.equals(v)). This operation
127 will probably require time linear in the map size for most implementations of
130 value - value whose presence in this map is to be tested.
132 Returns: true if this map maps one or more keys to the specified value.
133 *java.util.Map.entrySet()*
135 public |java.util.Set| entrySet()
137 Returns a set view of the mappings contained in this map. Each element in the
138 returned set is a (|java.util.Map.Entry|) . The set is backed by the map, so
139 changes to the map are reflected in the set, and vice-versa. If the map is
140 modified while an iteration over the set is in progress (except through the
141 iterator's own remove operation, or through the setValue operation on a map
142 entry returned by the iterator) the results of the iteration are undefined. The
143 set supports element removal, which removes the corresponding mapping from the
144 map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear
145 operations. It does not support the add or addAll operations.
148 Returns: a set view of the mappings contained in this map.
149 *java.util.Map.equals(Object)*
151 public boolean equals(java.lang.Object o)
153 Compares the specified object with this map for equality. Returns true if the
154 given object is also a map and the two Maps represent the same mappings. More
155 formally, two maps t1 and t2 represent the same mappings if
156 t1.entrySet().equals(t2.entrySet()). This ensures that the equals method works
157 properly across different implementations of the Map interface.
159 o - object to be compared for equality with this map.
161 Returns: true if the specified object is equal to this map.
162 *java.util.Map.get(Object)*
164 public |java.lang.Object| get(java.lang.Object key)
166 Returns the value to which this map maps the specified key. Returns null if the
167 map contains no mapping for this key. A return value of null does not
168 necessarily indicate that the map contains no mapping for the key; it's also
169 possible that the map explicitly maps the key to null. The containsKey
170 operation may be used to distinguish these two cases.
172 More formally, if this map contains a mapping from a key k to a value v such
173 that (key==null ? k==null : key.equals(k)), then this method returns v;
174 otherwise it returns null. (There can be at most one such mapping.)
176 key - key whose associated value is to be returned.
178 Returns: the value to which this map maps the specified key, or null if the map contains
179 no mapping for this key.
180 *java.util.Map.hashCode()*
182 public int hashCode()
184 Returns the hash code value for this map. The hash code of a map is defined to
185 be the sum of the hashCodes of each entry in the map's entrySet view. This
186 ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any
187 two maps t1 and t2, as required by the general contract of Object.hashCode.
190 Returns: the hash code value for this map.
191 *java.util.Map.isEmpty()*
193 public boolean isEmpty()
195 Returns true if this map contains no key-value mappings.
198 Returns: true if this map contains no key-value mappings.
199 *java.util.Map.keySet()*
201 public |java.util.Set| keySet()
203 Returns a set view of the keys contained in this map. The set is backed by the
204 map, so changes to the map are reflected in the set, and vice-versa. If the map
205 is modified while an iteration over the set is in progress (except through the
206 iterator's own remove operation), the results of the iteration are undefined.
207 The set supports element removal, which removes the corresponding mapping from
208 the map, via the Iterator.remove, Set.remove, removeAll retainAll, and clear
209 operations. It does not support the add or addAll operations.
212 Returns: a set view of the keys contained in this map.
213 *java.util.Map.put(K,V)*
215 public |java.lang.Object| put(
216 java.lang.Object key,
217 java.lang.Object value)
219 Associates the specified value with the specified key in this map (optional
220 operation). If the map previously contained a mapping for this key, the old
221 value is replaced by the specified value. (A map m is said to contain a mapping
222 for a key k if and only if m.containsKey(k)(|java.util.Map|) would return
225 key - key with which the specified value is to be associated.
226 value - value to be associated with the specified key.
228 Returns: previous value associated with specified key, or null if there was no mapping
229 for key. A null return can also indicate that the map previously
230 associated null with the specified key, if the implementation
231 supports null values.
232 *java.util.Map.putAll(Map)*
234 public void putAll(java.util.Map t)
236 Copies all of the mappings from the specified map to this map (optional
237 operation). The effect of this call is equivalent to that of calling put(k,
238 v)(|java.util.Map|) on this map once for each mapping from key k to value v in
239 the specified map. The behavior of this operation is unspecified if the
240 specified map is modified while the operation is in progress.
242 t - Mappings to be stored in this map.
244 *java.util.Map.remove(Object)*
246 public |java.lang.Object| remove(java.lang.Object key)
248 Removes the mapping for this key from this map if it is present (optional
249 operation). More formally, if this map contains a mapping from key k to value v
250 such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The
251 map can contain at most one such mapping.)
253 Returns the value to which the map previously associated the key, or null if
254 the map contained no mapping for this key. (A null return can also indicate
255 that the map previously associated null with the specified key if the
256 implementation supports null values.) The map will not contain a mapping for
257 the specified key once the call returns.
259 key - key whose mapping is to be removed from the map.
261 Returns: previous value associated with specified key, or null if there was no mapping
263 *java.util.Map.size()*
267 Returns the number of key-value mappings in this map. If the map contains more
268 than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
271 Returns: the number of key-value mappings in this map.
272 *java.util.Map.values()*
274 public |java.util.Collection| values()
276 Returns a collection view of the values contained in this map. The collection
277 is backed by the map, so changes to the map are reflected in the collection,
278 and vice-versa. If the map is modified while an iteration over the collection
279 is in progress (except through the iterator's own remove operation), the
280 results of the iteration are undefined. The collection supports element
281 removal, which removes the corresponding mapping from the map, via the
282 Iterator.remove, Collection.remove, removeAll, retainAll and clear operations.
283 It does not support the add or addAll operations.
286 Returns: a collection view of the values contained in this map.