Source Home >> Java Source 1.6.0 >> java.util.Map V 0.09
  • 001/*
  • 002 * @(#)Map.java 1.56 06/04/21
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.util;
  • 009
  • 010/**
  • 011 * An object that maps keys to values. A map cannot contain duplicate keys;
  • 012 * each key can map to at most one value.
  • 013 *
  • 014 * <p>This interface takes the place of the <tt>Dictionary</tt> class, which
  • 015 * was a totally abstract class rather than an interface.
  • 016 *
  • 017 * <p>The <tt>Map</tt> interface provides three <i>collection views</i>, which
  • 018 * allow a map's contents to be viewed as a set of keys, collection of values,
  • 019 * or set of key-value mappings. The <i>order</i> of a map is defined as
  • 020 * the order in which the iterators on the map's collection views return their
  • 021 * elements. Some map implementations, like the <tt>TreeMap</tt> class, make
  • 022 * specific guarantees as to their order; others, like the <tt>HashMap</tt>
  • 023 * class, do not.
  • 024 *
  • 025 * <p>Note: great care must be exercised if mutable objects are used as map
  • 026 * keys. The behavior of a map is not specified if the value of an object is
  • 027 * changed in a manner that affects <tt>equals</tt> comparisons while the
  • 028 * object is a key in the map. A special case of this prohibition is that it
  • 029 * is not permissible for a map to contain itself as a key. While it is
  • 030 * permissible for a map to contain itself as a value, extreme caution is
  • 031 * advised: the <tt>equals</tt> and <tt>hashCode</tt> methods are no longer
  • 032 * well defined on such a map.
  • 033 *
  • 034 * <p>All general-purpose map implementation classes should provide two
  • 035 * "standard" constructors: a void (no arguments) constructor which creates an
  • 036 * empty map, and a constructor with a single argument of type <tt>Map</tt>,
  • 037 * which creates a new map with the same key-value mappings as its argument.
  • 038 * In effect, the latter constructor allows the user to copy any map,
  • 039 * producing an equivalent map of the desired class. There is no way to
  • 040 * enforce this recommendation (as interfaces cannot contain constructors) but
  • 041 * all of the general-purpose map implementations in the JDK comply.
  • 042 *
  • 043 * <p>The "destructive" methods contained in this interface, that is, the
  • 044 * methods that modify the map on which they operate, are specified to throw
  • 045 * <tt>UnsupportedOperationException</tt> if this map does not support the
  • 046 * operation. If this is the case, these methods may, but are not required
  • 047 * to, throw an <tt>UnsupportedOperationException</tt> if the invocation would
  • 048 * have no effect on the map. For example, invoking the {@link #putAll(Map)}
  • 049 * method on an unmodifiable map may, but is not required to, throw the
  • 050 * exception if the map whose mappings are to be "superimposed" is empty.
  • 051 *
  • 052 * <p>Some map implementations have restrictions on the keys and values they
  • 053 * may contain. For example, some implementations prohibit null keys and
  • 054 * values, and some have restrictions on the types of their keys. Attempting
  • 055 * to insert an ineligible key or value throws an unchecked exception,
  • 056 * typically <tt>NullPointerException</tt> or <tt>ClassCastException</tt>.
  • 057 * Attempting to query the presence of an ineligible key or value may throw an
  • 058 * exception, or it may simply return false; some implementations will exhibit
  • 059 * the former behavior and some will exhibit the latter. More generally,
  • 060 * attempting an operation on an ineligible key or value whose completion
  • 061 * would not result in the insertion of an ineligible element into the map may
  • 062 * throw an exception or it may succeed, at the option of the implementation.
  • 063 * Such exceptions are marked as "optional" in the specification for this
  • 064 * interface.
  • 065 *
  • 066 * <p>This interface is a member of the
  • 067 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  • 068 * Java Collections Framework</a>.
  • 069 *
  • 070 * <p>Many methods in Collections Framework interfaces are defined
  • 071 * in terms of the {@link Object#equals(Object) equals} method. For
  • 072 * example, the specification for the {@link #containsKey(Object)
  • 073 * containsKey(Object key)} method says: "returns <tt>true</tt> if and
  • 074 * only if this map contains a mapping for a key <tt>k</tt> such that
  • 075 * <tt>(key==null ? k==null : key.equals(k))</tt>." This specification should
  • 076 * <i>not</i> be construed to imply that invoking <tt>Map.containsKey</tt>
  • 077 * with a non-null argument <tt>key</tt> will cause <tt>key.equals(k)</tt> to
  • 078 * be invoked for any key <tt>k</tt>. Implementations are free to
  • 079 * implement optimizations whereby the <tt>equals</tt> invocation is avoided,
  • 080 * for example, by first comparing the hash codes of the two keys. (The
  • 081 * {@link Object#hashCode()} specification guarantees that two objects with
  • 082 * unequal hash codes cannot be equal.) More generally, implementations of
  • 083 * the various Collections Framework interfaces are free to take advantage of
  • 084 * the specified behavior of underlying {@link Object} methods wherever the
  • 085 * implementor deems it appropriate.
  • 086 *
  • 087 * @param <K> the type of keys maintained by this map
  • 088 * @param <V> the type of mapped values
  • 089 *
  • 090 * @author Josh Bloch
  • 091 * @version 1.56, 04/21/06
  • 092 * @see HashMap
  • 093 * @see TreeMap
  • 094 * @see Hashtable
  • 095 * @see SortedMap
  • 096 * @see Collection
  • 097 * @see Set
  • 098 * @since 1.2
  • 099 */
  • 100public interface Map<K,V> {
  • 101 // Query Operations
  • 102
  • 103 /**
  • 104 * Returns the number of key-value mappings in this map. If the
  • 105 * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  • 106 * <tt>Integer.MAX_VALUE</tt>.
  • 107 *
  • 108 * @return the number of key-value mappings in this map
  • 109 */
  • 110 int size();
  • 111
  • 112 /**
  • 113 * Returns <tt>true</tt> if this map contains no key-value mappings.
  • 114 *
  • 115 * @return <tt>true</tt> if this map contains no key-value mappings
  • 116 */
  • 117 boolean isEmpty();
  • 118
  • 119 /**
  • 120 * Returns <tt>true</tt> if this map contains a mapping for the specified
  • 121 * key. More formally, returns <tt>true</tt> if and only if
  • 122 * this map contains a mapping for a key <tt>k</tt> such that
  • 123 * <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
  • 124 * at most one such mapping.)
  • 125 *
  • 126 * @param key key whose presence in this map is to be tested
  • 127 * @return <tt>true</tt> if this map contains a mapping for the specified
  • 128 * key
  • 129 * @throws ClassCastException if the key is of an inappropriate type for
  • 130 * this map (optional)
  • 131 * @throws NullPointerException if the specified key is null and this map
  • 132 * does not permit null keys (optional)
  • 133 */
  • 134 boolean containsKey(Object key);
  • 135
  • 136 /**
  • 137 * Returns <tt>true</tt> if this map maps one or more keys to the
  • 138 * specified value. More formally, returns <tt>true</tt> if and only if
  • 139 * this map contains at least one mapping to a value <tt>v</tt> such that
  • 140 * <tt>(value==null ? v==null : value.equals(v))</tt>. This operation
  • 141 * will probably require time linear in the map size for most
  • 142 * implementations of the <tt>Map</tt> interface.
  • 143 *
  • 144 * @param value value whose presence in this map is to be tested
  • 145 * @return <tt>true</tt> if this map maps one or more keys to the
  • 146 * specified value
  • 147 * @throws ClassCastException if the value is of an inappropriate type for
  • 148 * this map (optional)
  • 149 * @throws NullPointerException if the specified value is null and this
  • 150 * map does not permit null values (optional)
  • 151 */
  • 152 boolean containsValue(Object value);
  • 153
  • 154 /**
  • 155 * Returns the value to which the specified key is mapped,
  • 156 * or {@code null} if this map contains no mapping for the key.
  • 157 *
  • 158 * <p>More formally, if this map contains a mapping from a key
  • 159 * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
  • 160 * key.equals(k))}, then this method returns {@code v}; otherwise
  • 161 * it returns {@code null}. (There can be at most one such mapping.)
  • 162 *
  • 163 * <p>If this map permits null values, then a return value of
  • 164 * {@code null} does not <i>necessarily</i> indicate that the map
  • 165 * contains no mapping for the key; it's also possible that the map
  • 166 * explicitly maps the key to {@code null}. The {@link #containsKey
  • 167 * containsKey} operation may be used to distinguish these two cases.
  • 168 *
  • 169 * @param key the key whose associated value is to be returned
  • 170 * @return the value to which the specified key is mapped, or
  • 171 * {@code null} if this map contains no mapping for the key
  • 172 * @throws ClassCastException if the key is of an inappropriate type for
  • 173 * this map (optional)
  • 174 * @throws NullPointerException if the specified key is null and this map
  • 175 * does not permit null keys (optional)
  • 176 */
  • 177 V get(Object key);
  • 178
  • 179 // Modification Operations
  • 180
  • 181 /**
  • 182 * Associates the specified value with the specified key in this map
  • 183 * (optional operation). If the map previously contained a mapping for
  • 184 * the key, the old value is replaced by the specified value. (A map
  • 185 * <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only
  • 186 * if {@link #containsKey(Object) m.containsKey(k)} would return
  • 187 * <tt>true</tt>.)
  • 188 *
  • 189 * @param key key with which the specified value is to be associated
  • 190 * @param value value to be associated with the specified key
  • 191 * @return the previous value associated with <tt>key</tt>, or
  • 192 * <tt>null</tt> if there was no mapping for <tt>key</tt>.
  • 193 * (A <tt>null</tt> return can also indicate that the map
  • 194 * previously associated <tt>null</tt> with <tt>key</tt>,
  • 195 * if the implementation supports <tt>null</tt> values.)
  • 196 * @throws UnsupportedOperationException if the <tt>put</tt> operation
  • 197 * is not supported by this map
  • 198 * @throws ClassCastException if the class of the specified key or value
  • 199 * prevents it from being stored in this map
  • 200 * @throws NullPointerException if the specified key or value is null
  • 201 * and this map does not permit null keys or values
  • 202 * @throws IllegalArgumentException if some property of the specified key
  • 203 * or value prevents it from being stored in this map
  • 204 */
  • 205 V put(K key, V value);
  • 206
  • 207 /**
  • 208 * Removes the mapping for a key from this map if it is present
  • 209 * (optional operation). More formally, if this map contains a mapping
  • 210 * from key <tt>k</tt> to value <tt>v</tt> such that
  • 211 * <code>(key==null ? k==null : key.equals(k))</code>, that mapping
  • 212 * is removed. (The map can contain at most one such mapping.)
  • 213 *
  • 214 * <p>Returns the value to which this map previously associated the key,
  • 215 * or <tt>null</tt> if the map contained no mapping for the key.
  • 216 *
  • 217 * <p>If this map permits null values, then a return value of
  • 218 * <tt>null</tt> does not <i>necessarily</i> indicate that the map
  • 219 * contained no mapping for the key; it's also possible that the map
  • 220 * explicitly mapped the key to <tt>null</tt>.
  • 221 *
  • 222 * <p>The map will not contain a mapping for the specified key once the
  • 223 * call returns.
  • 224 *
  • 225 * @param key key whose mapping is to be removed from the map
  • 226 * @return the previous value associated with <tt>key</tt>, or
  • 227 * <tt>null</tt> if there was no mapping for <tt>key</tt>.
  • 228 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
  • 229 * is not supported by this map
  • 230 * @throws ClassCastException if the key is of an inappropriate type for
  • 231 * this map (optional)
  • 232 * @throws NullPointerException if the specified key is null and this
  • 233 * map does not permit null keys (optional)
  • 234 */
  • 235 V remove(Object key);
  • 236
  • 237
  • 238 // Bulk Operations
  • 239
  • 240 /**
  • 241 * Copies all of the mappings from the specified map to this map
  • 242 * (optional operation). The effect of this call is equivalent to that
  • 243 * of calling {@link #put(Object,Object) put(k, v)} on this map once
  • 244 * for each mapping from key <tt>k</tt> to value <tt>v</tt> in the
  • 245 * specified map. The behavior of this operation is undefined if the
  • 246 * specified map is modified while the operation is in progress.
  • 247 *
  • 248 * @param m mappings to be stored in this map
  • 249 * @throws UnsupportedOperationException if the <tt>putAll</tt> operation
  • 250 * is not supported by this map
  • 251 * @throws ClassCastException if the class of a key or value in the
  • 252 * specified map prevents it from being stored in this map
  • 253 * @throws NullPointerException if the specified map is null, or if
  • 254 * this map does not permit null keys or values, and the
  • 255 * specified map contains null keys or values
  • 256 * @throws IllegalArgumentException if some property of a key or value in
  • 257 * the specified map prevents it from being stored in this map
  • 258 */
  • 259 void putAll(Map<? extends K, ? extends V> m);
  • 260
  • 261 /**
  • 262 * Removes all of the mappings from this map (optional operation).
  • 263 * The map will be empty after this call returns.
  • 264 *
  • 265 * @throws UnsupportedOperationException if the <tt>clear</tt> operation
  • 266 * is not supported by this map
  • 267 */
  • 268 void clear();
  • 269
  • 270
  • 271 // Views
  • 272
  • 273 /**
  • 274 * Returns a {@link Set} view of the keys contained in this map.
  • 275 * The set is backed by the map, so changes to the map are
  • 276 * reflected in the set, and vice-versa. If the map is modified
  • 277 * while an iteration over the set is in progress (except through
  • 278 * the iterator's own <tt>remove</tt> operation), the results of
  • 279 * the iteration are undefined. The set supports element removal,
  • 280 * which removes the corresponding mapping from the map, via the
  • 281 * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
  • 282 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
  • 283 * operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
  • 284 * operations.
  • 285 *
  • 286 * @return a set view of the keys contained in this map
  • 287 */
  • 288 Set<K> keySet();
  • 289
  • 290 /**
  • 291 * Returns a {@link Collection} view of the values contained in this map.
  • 292 * The collection is backed by the map, so changes to the map are
  • 293 * reflected in the collection, and vice-versa. If the map is
  • 294 * modified while an iteration over the collection is in progress
  • 295 * (except through the iterator's own <tt>remove</tt> operation),
  • 296 * the results of the iteration are undefined. The collection
  • 297 * supports element removal, which removes the corresponding
  • 298 * mapping from the map, via the <tt>Iterator.remove</tt>,
  • 299 * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
  • 300 * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
  • 301 * support the <tt>add</tt> or <tt>addAll</tt> operations.
  • 302 *
  • 303 * @return a collection view of the values contained in this map
  • 304 */
  • 305 Collection<V> values();
  • 306
  • 307 /**
  • 308 * Returns a {@link Set} view of the mappings contained in this map.
  • 309 * The set is backed by the map, so changes to the map are
  • 310 * reflected in the set, and vice-versa. If the map is modified
  • 311 * while an iteration over the set is in progress (except through
  • 312 * the iterator's own <tt>remove</tt> operation, or through the
  • 313 * <tt>setValue</tt> operation on a map entry returned by the
  • 314 * iterator) the results of the iteration are undefined. The set
  • 315 * supports element removal, which removes the corresponding
  • 316 * mapping from the map, via the <tt>Iterator.remove</tt>,
  • 317 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
  • 318 * <tt>clear</tt> operations. It does not support the
  • 319 * <tt>add</tt> or <tt>addAll</tt> operations.
  • 320 *
  • 321 * @return a set view of the mappings contained in this map
  • 322 */
  • 323 Set<Map.Entry<K, V>> entrySet();
  • 324
  • 325 /**
  • 326 * A map entry (key-value pair). The <tt>Map.entrySet</tt> method returns
  • 327 * a collection-view of the map, whose elements are of this class. The
  • 328 * <i>only</i> way to obtain a reference to a map entry is from the
  • 329 * iterator of this collection-view. These <tt>Map.Entry</tt> objects are
  • 330 * valid <i>only</i> for the duration of the iteration; more formally,
  • 331 * the behavior of a map entry is undefined if the backing map has been
  • 332 * modified after the entry was returned by the iterator, except through
  • 333 * the <tt>setValue</tt> operation on the map entry.
  • 334 *
  • 335 * @see Map#entrySet()
  • 336 * @since 1.2
  • 337 */
  • 338 interface Entry<K,V> {
  • 339 /**
  • 340 * Returns the key corresponding to this entry.
  • 341 *
  • 342 * @return the key corresponding to this entry
  • 343 * @throws IllegalStateException implementations may, but are not
  • 344 * required to, throw this exception if the entry has been
  • 345 * removed from the backing map.
  • 346 */
  • 347 K getKey();
  • 348
  • 349 /**
  • 350 * Returns the value corresponding to this entry. If the mapping
  • 351 * has been removed from the backing map (by the iterator's
  • 352 * <tt>remove</tt> operation), the results of this call are undefined.
  • 353 *
  • 354 * @return the value corresponding to this entry
  • 355 * @throws IllegalStateException implementations may, but are not
  • 356 * required to, throw this exception if the entry has been
  • 357 * removed from the backing map.
  • 358 */
  • 359 V getValue();
  • 360
  • 361 /**
  • 362 * Replaces the value corresponding to this entry with the specified
  • 363 * value (optional operation). (Writes through to the map.) The
  • 364 * behavior of this call is undefined if the mapping has already been
  • 365 * removed from the map (by the iterator's <tt>remove</tt> operation).
  • 366 *
  • 367 * @param value new value to be stored in this entry
  • 368 * @return old value corresponding to the entry
  • 369 * @throws UnsupportedOperationException if the <tt>put</tt> operation
  • 370 * is not supported by the backing map
  • 371 * @throws ClassCastException if the class of the specified value
  • 372 * prevents it from being stored in the backing map
  • 373 * @throws NullPointerException if the backing map does not permit
  • 374 * null values, and the specified value is null
  • 375 * @throws IllegalArgumentException if some property of this value
  • 376 * prevents it from being stored in the backing map
  • 377 * @throws IllegalStateException implementations may, but are not
  • 378 * required to, throw this exception if the entry has been
  • 379 * removed from the backing map.
  • 380 */
  • 381 V setValue(V value);
  • 382
  • 383 /**
  • 384 * Compares the specified object with this entry for equality.
  • 385 * Returns <tt>true</tt> if the given object is also a map entry and
  • 386 * the two entries represent the same mapping. More formally, two
  • 387 * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
  • 388 * if<pre>
  • 389 * (e1.getKey()==null ?
  • 390 * e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
  • 391 * (e1.getValue()==null ?
  • 392 * e2.getValue()==null : e1.getValue().equals(e2.getValue()))
  • 393 * </pre>
  • 394 * This ensures that the <tt>equals</tt> method works properly across
  • 395 * different implementations of the <tt>Map.Entry</tt> interface.
  • 396 *
  • 397 * @param o object to be compared for equality with this map entry
  • 398 * @return <tt>true</tt> if the specified object is equal to this map
  • 399 * entry
  • 400 */
  • 401 boolean equals(Object o);
  • 402
  • 403 /**
  • 404 * Returns the hash code value for this map entry. The hash code
  • 405 * of a map entry <tt>e</tt> is defined to be: <pre>
  • 406 * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
  • 407 * (e.getValue()==null ? 0 : e.getValue().hashCode())
  • 408 * </pre>
  • 409 * This ensures that <tt>e1.equals(e2)</tt> implies that
  • 410 * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
  • 411 * <tt>e1</tt> and <tt>e2</tt>, as required by the general
  • 412 * contract of <tt>Object.hashCode</tt>.
  • 413 *
  • 414 * @return the hash code value for this map entry
  • 415 * @see Object#hashCode()
  • 416 * @see Object#equals(Object)
  • 417 * @see #equals(Object)
  • 418 */
  • 419 int hashCode();
  • 420 }
  • 421
  • 422 // Comparison and hashing
  • 423
  • 424 /**
  • 425 * Compares the specified object with this map for equality. Returns
  • 426 * <tt>true</tt> if the given object is also a map and the two maps
  • 427 * represent the same mappings. More formally, two maps <tt>m1</tt> and
  • 428 * <tt>m2</tt> represent the same mappings if
  • 429 * <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the
  • 430 * <tt>equals</tt> method works properly across different implementations
  • 431 * of the <tt>Map</tt> interface.
  • 432 *
  • 433 * @param o object to be compared for equality with this map
  • 434 * @return <tt>true</tt> if the specified object is equal to this map
  • 435 */
  • 436 boolean equals(Object o);
  • 437
  • 438 /**
  • 439 * Returns the hash code value for this map. The hash code of a map is
  • 440 * defined to be the sum of the hash codes of each entry in the map's
  • 441 * <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt>
  • 442 * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
  • 443 * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
  • 444 * {@link Object#hashCode}.
  • 445 *
  • 446 * @return the hash code value for this map
  • 447 * @see Map.Entry#hashCode()
  • 448 * @see Object#equals(Object)
  • 449 * @see #equals(Object)
  • 450 */
  • 451 int hashCode();
  • 452}

文件:Map.java
包名:java.util
类名:Map
继承:
接口: