rt/emul/compact/src/main/java/java/util/Map.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 557 emul/compact/src/main/java/java/util/Map.java@5be31d9fa455
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@557
     1
/*
jaroslav@557
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@557
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@557
     4
 *
jaroslav@557
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@557
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@557
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@557
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@557
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@557
    10
 *
jaroslav@557
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@557
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@557
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@557
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@557
    15
 * accompanied this code).
jaroslav@557
    16
 *
jaroslav@557
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@557
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@557
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@557
    20
 *
jaroslav@557
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@557
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@557
    23
 * questions.
jaroslav@557
    24
 */
jaroslav@557
    25
jaroslav@557
    26
package java.util;
jaroslav@557
    27
jaroslav@557
    28
/**
jaroslav@557
    29
 * An object that maps keys to values.  A map cannot contain duplicate keys;
jaroslav@557
    30
 * each key can map to at most one value.
jaroslav@557
    31
 *
jaroslav@557
    32
 * <p>This interface takes the place of the <tt>Dictionary</tt> class, which
jaroslav@557
    33
 * was a totally abstract class rather than an interface.
jaroslav@557
    34
 *
jaroslav@557
    35
 * <p>The <tt>Map</tt> interface provides three <i>collection views</i>, which
jaroslav@557
    36
 * allow a map's contents to be viewed as a set of keys, collection of values,
jaroslav@557
    37
 * or set of key-value mappings.  The <i>order</i> of a map is defined as
jaroslav@557
    38
 * the order in which the iterators on the map's collection views return their
jaroslav@557
    39
 * elements.  Some map implementations, like the <tt>TreeMap</tt> class, make
jaroslav@557
    40
 * specific guarantees as to their order; others, like the <tt>HashMap</tt>
jaroslav@557
    41
 * class, do not.
jaroslav@557
    42
 *
jaroslav@557
    43
 * <p>Note: great care must be exercised if mutable objects are used as map
jaroslav@557
    44
 * keys.  The behavior of a map is not specified if the value of an object is
jaroslav@557
    45
 * changed in a manner that affects <tt>equals</tt> comparisons while the
jaroslav@557
    46
 * object is a key in the map.  A special case of this prohibition is that it
jaroslav@557
    47
 * is not permissible for a map to contain itself as a key.  While it is
jaroslav@557
    48
 * permissible for a map to contain itself as a value, extreme caution is
jaroslav@557
    49
 * advised: the <tt>equals</tt> and <tt>hashCode</tt> methods are no longer
jaroslav@557
    50
 * well defined on such a map.
jaroslav@557
    51
 *
jaroslav@557
    52
 * <p>All general-purpose map implementation classes should provide two
jaroslav@557
    53
 * "standard" constructors: a void (no arguments) constructor which creates an
jaroslav@557
    54
 * empty map, and a constructor with a single argument of type <tt>Map</tt>,
jaroslav@557
    55
 * which creates a new map with the same key-value mappings as its argument.
jaroslav@557
    56
 * In effect, the latter constructor allows the user to copy any map,
jaroslav@557
    57
 * producing an equivalent map of the desired class.  There is no way to
jaroslav@557
    58
 * enforce this recommendation (as interfaces cannot contain constructors) but
jaroslav@557
    59
 * all of the general-purpose map implementations in the JDK comply.
jaroslav@557
    60
 *
jaroslav@557
    61
 * <p>The "destructive" methods contained in this interface, that is, the
jaroslav@557
    62
 * methods that modify the map on which they operate, are specified to throw
jaroslav@557
    63
 * <tt>UnsupportedOperationException</tt> if this map does not support the
jaroslav@557
    64
 * operation.  If this is the case, these methods may, but are not required
jaroslav@557
    65
 * to, throw an <tt>UnsupportedOperationException</tt> if the invocation would
jaroslav@557
    66
 * have no effect on the map.  For example, invoking the {@link #putAll(Map)}
jaroslav@557
    67
 * method on an unmodifiable map may, but is not required to, throw the
jaroslav@557
    68
 * exception if the map whose mappings are to be "superimposed" is empty.
jaroslav@557
    69
 *
jaroslav@557
    70
 * <p>Some map implementations have restrictions on the keys and values they
jaroslav@557
    71
 * may contain.  For example, some implementations prohibit null keys and
jaroslav@557
    72
 * values, and some have restrictions on the types of their keys.  Attempting
jaroslav@557
    73
 * to insert an ineligible key or value throws an unchecked exception,
jaroslav@557
    74
 * typically <tt>NullPointerException</tt> or <tt>ClassCastException</tt>.
jaroslav@557
    75
 * Attempting to query the presence of an ineligible key or value may throw an
jaroslav@557
    76
 * exception, or it may simply return false; some implementations will exhibit
jaroslav@557
    77
 * the former behavior and some will exhibit the latter.  More generally,
jaroslav@557
    78
 * attempting an operation on an ineligible key or value whose completion
jaroslav@557
    79
 * would not result in the insertion of an ineligible element into the map may
jaroslav@557
    80
 * throw an exception or it may succeed, at the option of the implementation.
jaroslav@557
    81
 * Such exceptions are marked as "optional" in the specification for this
jaroslav@557
    82
 * interface.
jaroslav@557
    83
 *
jaroslav@557
    84
 * <p>This interface is a member of the
jaroslav@557
    85
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@557
    86
 * Java Collections Framework</a>.
jaroslav@557
    87
 *
jaroslav@557
    88
 * <p>Many methods in Collections Framework interfaces are defined
jaroslav@557
    89
 * in terms of the {@link Object#equals(Object) equals} method.  For
jaroslav@557
    90
 * example, the specification for the {@link #containsKey(Object)
jaroslav@557
    91
 * containsKey(Object key)} method says: "returns <tt>true</tt> if and
jaroslav@557
    92
 * only if this map contains a mapping for a key <tt>k</tt> such that
jaroslav@557
    93
 * <tt>(key==null ? k==null : key.equals(k))</tt>." This specification should
jaroslav@557
    94
 * <i>not</i> be construed to imply that invoking <tt>Map.containsKey</tt>
jaroslav@557
    95
 * with a non-null argument <tt>key</tt> will cause <tt>key.equals(k)</tt> to
jaroslav@557
    96
 * be invoked for any key <tt>k</tt>.  Implementations are free to
jaroslav@557
    97
 * implement optimizations whereby the <tt>equals</tt> invocation is avoided,
jaroslav@557
    98
 * for example, by first comparing the hash codes of the two keys.  (The
jaroslav@557
    99
 * {@link Object#hashCode()} specification guarantees that two objects with
jaroslav@557
   100
 * unequal hash codes cannot be equal.)  More generally, implementations of
jaroslav@557
   101
 * the various Collections Framework interfaces are free to take advantage of
jaroslav@557
   102
 * the specified behavior of underlying {@link Object} methods wherever the
jaroslav@557
   103
 * implementor deems it appropriate.
jaroslav@557
   104
 *
jaroslav@557
   105
 * @param <K> the type of keys maintained by this map
jaroslav@557
   106
 * @param <V> the type of mapped values
jaroslav@557
   107
 *
jaroslav@557
   108
 * @author  Josh Bloch
jaroslav@557
   109
 * @see HashMap
jaroslav@557
   110
 * @see TreeMap
jaroslav@557
   111
 * @see Hashtable
jaroslav@557
   112
 * @see SortedMap
jaroslav@557
   113
 * @see Collection
jaroslav@557
   114
 * @see Set
jaroslav@557
   115
 * @since 1.2
jaroslav@557
   116
 */
jaroslav@557
   117
public interface Map<K,V> {
jaroslav@557
   118
    // Query Operations
jaroslav@557
   119
jaroslav@557
   120
    /**
jaroslav@557
   121
     * Returns the number of key-value mappings in this map.  If the
jaroslav@557
   122
     * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
jaroslav@557
   123
     * <tt>Integer.MAX_VALUE</tt>.
jaroslav@557
   124
     *
jaroslav@557
   125
     * @return the number of key-value mappings in this map
jaroslav@557
   126
     */
jaroslav@557
   127
    int size();
jaroslav@557
   128
jaroslav@557
   129
    /**
jaroslav@557
   130
     * Returns <tt>true</tt> if this map contains no key-value mappings.
jaroslav@557
   131
     *
jaroslav@557
   132
     * @return <tt>true</tt> if this map contains no key-value mappings
jaroslav@557
   133
     */
jaroslav@557
   134
    boolean isEmpty();
jaroslav@557
   135
jaroslav@557
   136
    /**
jaroslav@557
   137
     * Returns <tt>true</tt> if this map contains a mapping for the specified
jaroslav@557
   138
     * key.  More formally, returns <tt>true</tt> if and only if
jaroslav@557
   139
     * this map contains a mapping for a key <tt>k</tt> such that
jaroslav@557
   140
     * <tt>(key==null ? k==null : key.equals(k))</tt>.  (There can be
jaroslav@557
   141
     * at most one such mapping.)
jaroslav@557
   142
     *
jaroslav@557
   143
     * @param key key whose presence in this map is to be tested
jaroslav@557
   144
     * @return <tt>true</tt> if this map contains a mapping for the specified
jaroslav@557
   145
     *         key
jaroslav@557
   146
     * @throws ClassCastException if the key is of an inappropriate type for
jaroslav@557
   147
     *         this map
jaroslav@557
   148
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@557
   149
     * @throws NullPointerException if the specified key is null and this map
jaroslav@557
   150
     *         does not permit null keys
jaroslav@557
   151
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@557
   152
     */
jaroslav@557
   153
    boolean containsKey(Object key);
jaroslav@557
   154
jaroslav@557
   155
    /**
jaroslav@557
   156
     * Returns <tt>true</tt> if this map maps one or more keys to the
jaroslav@557
   157
     * specified value.  More formally, returns <tt>true</tt> if and only if
jaroslav@557
   158
     * this map contains at least one mapping to a value <tt>v</tt> such that
jaroslav@557
   159
     * <tt>(value==null ? v==null : value.equals(v))</tt>.  This operation
jaroslav@557
   160
     * will probably require time linear in the map size for most
jaroslav@557
   161
     * implementations of the <tt>Map</tt> interface.
jaroslav@557
   162
     *
jaroslav@557
   163
     * @param value value whose presence in this map is to be tested
jaroslav@557
   164
     * @return <tt>true</tt> if this map maps one or more keys to the
jaroslav@557
   165
     *         specified value
jaroslav@557
   166
     * @throws ClassCastException if the value is of an inappropriate type for
jaroslav@557
   167
     *         this map
jaroslav@557
   168
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@557
   169
     * @throws NullPointerException if the specified value is null and this
jaroslav@557
   170
     *         map does not permit null values
jaroslav@557
   171
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@557
   172
     */
jaroslav@557
   173
    boolean containsValue(Object value);
jaroslav@557
   174
jaroslav@557
   175
    /**
jaroslav@557
   176
     * Returns the value to which the specified key is mapped,
jaroslav@557
   177
     * or {@code null} if this map contains no mapping for the key.
jaroslav@557
   178
     *
jaroslav@557
   179
     * <p>More formally, if this map contains a mapping from a key
jaroslav@557
   180
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
jaroslav@557
   181
     * key.equals(k))}, then this method returns {@code v}; otherwise
jaroslav@557
   182
     * it returns {@code null}.  (There can be at most one such mapping.)
jaroslav@557
   183
     *
jaroslav@557
   184
     * <p>If this map permits null values, then a return value of
jaroslav@557
   185
     * {@code null} does not <i>necessarily</i> indicate that the map
jaroslav@557
   186
     * contains no mapping for the key; it's also possible that the map
jaroslav@557
   187
     * explicitly maps the key to {@code null}.  The {@link #containsKey
jaroslav@557
   188
     * containsKey} operation may be used to distinguish these two cases.
jaroslav@557
   189
     *
jaroslav@557
   190
     * @param key the key whose associated value is to be returned
jaroslav@557
   191
     * @return the value to which the specified key is mapped, or
jaroslav@557
   192
     *         {@code null} if this map contains no mapping for the key
jaroslav@557
   193
     * @throws ClassCastException if the key is of an inappropriate type for
jaroslav@557
   194
     *         this map
jaroslav@557
   195
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@557
   196
     * @throws NullPointerException if the specified key is null and this map
jaroslav@557
   197
     *         does not permit null keys
jaroslav@557
   198
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@557
   199
     */
jaroslav@557
   200
    V get(Object key);
jaroslav@557
   201
jaroslav@557
   202
    // Modification Operations
jaroslav@557
   203
jaroslav@557
   204
    /**
jaroslav@557
   205
     * Associates the specified value with the specified key in this map
jaroslav@557
   206
     * (optional operation).  If the map previously contained a mapping for
jaroslav@557
   207
     * the key, the old value is replaced by the specified value.  (A map
jaroslav@557
   208
     * <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only
jaroslav@557
   209
     * if {@link #containsKey(Object) m.containsKey(k)} would return
jaroslav@557
   210
     * <tt>true</tt>.)
jaroslav@557
   211
     *
jaroslav@557
   212
     * @param key key with which the specified value is to be associated
jaroslav@557
   213
     * @param value value to be associated with the specified key
jaroslav@557
   214
     * @return the previous value associated with <tt>key</tt>, or
jaroslav@557
   215
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
jaroslav@557
   216
     *         (A <tt>null</tt> return can also indicate that the map
jaroslav@557
   217
     *         previously associated <tt>null</tt> with <tt>key</tt>,
jaroslav@557
   218
     *         if the implementation supports <tt>null</tt> values.)
jaroslav@557
   219
     * @throws UnsupportedOperationException if the <tt>put</tt> operation
jaroslav@557
   220
     *         is not supported by this map
jaroslav@557
   221
     * @throws ClassCastException if the class of the specified key or value
jaroslav@557
   222
     *         prevents it from being stored in this map
jaroslav@557
   223
     * @throws NullPointerException if the specified key or value is null
jaroslav@557
   224
     *         and this map does not permit null keys or values
jaroslav@557
   225
     * @throws IllegalArgumentException if some property of the specified key
jaroslav@557
   226
     *         or value prevents it from being stored in this map
jaroslav@557
   227
     */
jaroslav@557
   228
    V put(K key, V value);
jaroslav@557
   229
jaroslav@557
   230
    /**
jaroslav@557
   231
     * Removes the mapping for a key from this map if it is present
jaroslav@557
   232
     * (optional operation).   More formally, if this map contains a mapping
jaroslav@557
   233
     * from key <tt>k</tt> to value <tt>v</tt> such that
jaroslav@557
   234
     * <code>(key==null ?  k==null : key.equals(k))</code>, that mapping
jaroslav@557
   235
     * is removed.  (The map can contain at most one such mapping.)
jaroslav@557
   236
     *
jaroslav@557
   237
     * <p>Returns the value to which this map previously associated the key,
jaroslav@557
   238
     * or <tt>null</tt> if the map contained no mapping for the key.
jaroslav@557
   239
     *
jaroslav@557
   240
     * <p>If this map permits null values, then a return value of
jaroslav@557
   241
     * <tt>null</tt> does not <i>necessarily</i> indicate that the map
jaroslav@557
   242
     * contained no mapping for the key; it's also possible that the map
jaroslav@557
   243
     * explicitly mapped the key to <tt>null</tt>.
jaroslav@557
   244
     *
jaroslav@557
   245
     * <p>The map will not contain a mapping for the specified key once the
jaroslav@557
   246
     * call returns.
jaroslav@557
   247
     *
jaroslav@557
   248
     * @param key key whose mapping is to be removed from the map
jaroslav@557
   249
     * @return the previous value associated with <tt>key</tt>, or
jaroslav@557
   250
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
jaroslav@557
   251
     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
jaroslav@557
   252
     *         is not supported by this map
jaroslav@557
   253
     * @throws ClassCastException if the key is of an inappropriate type for
jaroslav@557
   254
     *         this map
jaroslav@557
   255
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@557
   256
     * @throws NullPointerException if the specified key is null and this
jaroslav@557
   257
     *         map does not permit null keys
jaroslav@557
   258
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@557
   259
     */
jaroslav@557
   260
    V remove(Object key);
jaroslav@557
   261
jaroslav@557
   262
jaroslav@557
   263
    // Bulk Operations
jaroslav@557
   264
jaroslav@557
   265
    /**
jaroslav@557
   266
     * Copies all of the mappings from the specified map to this map
jaroslav@557
   267
     * (optional operation).  The effect of this call is equivalent to that
jaroslav@557
   268
     * of calling {@link #put(Object,Object) put(k, v)} on this map once
jaroslav@557
   269
     * for each mapping from key <tt>k</tt> to value <tt>v</tt> in the
jaroslav@557
   270
     * specified map.  The behavior of this operation is undefined if the
jaroslav@557
   271
     * specified map is modified while the operation is in progress.
jaroslav@557
   272
     *
jaroslav@557
   273
     * @param m mappings to be stored in this map
jaroslav@557
   274
     * @throws UnsupportedOperationException if the <tt>putAll</tt> operation
jaroslav@557
   275
     *         is not supported by this map
jaroslav@557
   276
     * @throws ClassCastException if the class of a key or value in the
jaroslav@557
   277
     *         specified map prevents it from being stored in this map
jaroslav@557
   278
     * @throws NullPointerException if the specified map is null, or if
jaroslav@557
   279
     *         this map does not permit null keys or values, and the
jaroslav@557
   280
     *         specified map contains null keys or values
jaroslav@557
   281
     * @throws IllegalArgumentException if some property of a key or value in
jaroslav@557
   282
     *         the specified map prevents it from being stored in this map
jaroslav@557
   283
     */
jaroslav@557
   284
    void putAll(Map<? extends K, ? extends V> m);
jaroslav@557
   285
jaroslav@557
   286
    /**
jaroslav@557
   287
     * Removes all of the mappings from this map (optional operation).
jaroslav@557
   288
     * The map will be empty after this call returns.
jaroslav@557
   289
     *
jaroslav@557
   290
     * @throws UnsupportedOperationException if the <tt>clear</tt> operation
jaroslav@557
   291
     *         is not supported by this map
jaroslav@557
   292
     */
jaroslav@557
   293
    void clear();
jaroslav@557
   294
jaroslav@557
   295
jaroslav@557
   296
    // Views
jaroslav@557
   297
jaroslav@557
   298
    /**
jaroslav@557
   299
     * Returns a {@link Set} view of the keys contained in this map.
jaroslav@557
   300
     * The set is backed by the map, so changes to the map are
jaroslav@557
   301
     * reflected in the set, and vice-versa.  If the map is modified
jaroslav@557
   302
     * while an iteration over the set is in progress (except through
jaroslav@557
   303
     * the iterator's own <tt>remove</tt> operation), the results of
jaroslav@557
   304
     * the iteration are undefined.  The set supports element removal,
jaroslav@557
   305
     * which removes the corresponding mapping from the map, via the
jaroslav@557
   306
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
jaroslav@557
   307
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
jaroslav@557
   308
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
jaroslav@557
   309
     * operations.
jaroslav@557
   310
     *
jaroslav@557
   311
     * @return a set view of the keys contained in this map
jaroslav@557
   312
     */
jaroslav@557
   313
    Set<K> keySet();
jaroslav@557
   314
jaroslav@557
   315
    /**
jaroslav@557
   316
     * Returns a {@link Collection} view of the values contained in this map.
jaroslav@557
   317
     * The collection is backed by the map, so changes to the map are
jaroslav@557
   318
     * reflected in the collection, and vice-versa.  If the map is
jaroslav@557
   319
     * modified while an iteration over the collection is in progress
jaroslav@557
   320
     * (except through the iterator's own <tt>remove</tt> operation),
jaroslav@557
   321
     * the results of the iteration are undefined.  The collection
jaroslav@557
   322
     * supports element removal, which removes the corresponding
jaroslav@557
   323
     * mapping from the map, via the <tt>Iterator.remove</tt>,
jaroslav@557
   324
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
jaroslav@557
   325
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
jaroslav@557
   326
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
jaroslav@557
   327
     *
jaroslav@557
   328
     * @return a collection view of the values contained in this map
jaroslav@557
   329
     */
jaroslav@557
   330
    Collection<V> values();
jaroslav@557
   331
jaroslav@557
   332
    /**
jaroslav@557
   333
     * Returns a {@link Set} view of the mappings contained in this map.
jaroslav@557
   334
     * The set is backed by the map, so changes to the map are
jaroslav@557
   335
     * reflected in the set, and vice-versa.  If the map is modified
jaroslav@557
   336
     * while an iteration over the set is in progress (except through
jaroslav@557
   337
     * the iterator's own <tt>remove</tt> operation, or through the
jaroslav@557
   338
     * <tt>setValue</tt> operation on a map entry returned by the
jaroslav@557
   339
     * iterator) the results of the iteration are undefined.  The set
jaroslav@557
   340
     * supports element removal, which removes the corresponding
jaroslav@557
   341
     * mapping from the map, via the <tt>Iterator.remove</tt>,
jaroslav@557
   342
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
jaroslav@557
   343
     * <tt>clear</tt> operations.  It does not support the
jaroslav@557
   344
     * <tt>add</tt> or <tt>addAll</tt> operations.
jaroslav@557
   345
     *
jaroslav@557
   346
     * @return a set view of the mappings contained in this map
jaroslav@557
   347
     */
jaroslav@557
   348
    Set<Map.Entry<K, V>> entrySet();
jaroslav@557
   349
jaroslav@557
   350
    /**
jaroslav@557
   351
     * A map entry (key-value pair).  The <tt>Map.entrySet</tt> method returns
jaroslav@557
   352
     * a collection-view of the map, whose elements are of this class.  The
jaroslav@557
   353
     * <i>only</i> way to obtain a reference to a map entry is from the
jaroslav@557
   354
     * iterator of this collection-view.  These <tt>Map.Entry</tt> objects are
jaroslav@557
   355
     * valid <i>only</i> for the duration of the iteration; more formally,
jaroslav@557
   356
     * the behavior of a map entry is undefined if the backing map has been
jaroslav@557
   357
     * modified after the entry was returned by the iterator, except through
jaroslav@557
   358
     * the <tt>setValue</tt> operation on the map entry.
jaroslav@557
   359
     *
jaroslav@557
   360
     * @see Map#entrySet()
jaroslav@557
   361
     * @since 1.2
jaroslav@557
   362
     */
jaroslav@557
   363
    interface Entry<K,V> {
jaroslav@557
   364
        /**
jaroslav@557
   365
         * Returns the key corresponding to this entry.
jaroslav@557
   366
         *
jaroslav@557
   367
         * @return the key corresponding to this entry
jaroslav@557
   368
         * @throws IllegalStateException implementations may, but are not
jaroslav@557
   369
         *         required to, throw this exception if the entry has been
jaroslav@557
   370
         *         removed from the backing map.
jaroslav@557
   371
         */
jaroslav@557
   372
        K getKey();
jaroslav@557
   373
jaroslav@557
   374
        /**
jaroslav@557
   375
         * Returns the value corresponding to this entry.  If the mapping
jaroslav@557
   376
         * has been removed from the backing map (by the iterator's
jaroslav@557
   377
         * <tt>remove</tt> operation), the results of this call are undefined.
jaroslav@557
   378
         *
jaroslav@557
   379
         * @return the value corresponding to this entry
jaroslav@557
   380
         * @throws IllegalStateException implementations may, but are not
jaroslav@557
   381
         *         required to, throw this exception if the entry has been
jaroslav@557
   382
         *         removed from the backing map.
jaroslav@557
   383
         */
jaroslav@557
   384
        V getValue();
jaroslav@557
   385
jaroslav@557
   386
        /**
jaroslav@557
   387
         * Replaces the value corresponding to this entry with the specified
jaroslav@557
   388
         * value (optional operation).  (Writes through to the map.)  The
jaroslav@557
   389
         * behavior of this call is undefined if the mapping has already been
jaroslav@557
   390
         * removed from the map (by the iterator's <tt>remove</tt> operation).
jaroslav@557
   391
         *
jaroslav@557
   392
         * @param value new value to be stored in this entry
jaroslav@557
   393
         * @return old value corresponding to the entry
jaroslav@557
   394
         * @throws UnsupportedOperationException if the <tt>put</tt> operation
jaroslav@557
   395
         *         is not supported by the backing map
jaroslav@557
   396
         * @throws ClassCastException if the class of the specified value
jaroslav@557
   397
         *         prevents it from being stored in the backing map
jaroslav@557
   398
         * @throws NullPointerException if the backing map does not permit
jaroslav@557
   399
         *         null values, and the specified value is null
jaroslav@557
   400
         * @throws IllegalArgumentException if some property of this value
jaroslav@557
   401
         *         prevents it from being stored in the backing map
jaroslav@557
   402
         * @throws IllegalStateException implementations may, but are not
jaroslav@557
   403
         *         required to, throw this exception if the entry has been
jaroslav@557
   404
         *         removed from the backing map.
jaroslav@557
   405
         */
jaroslav@557
   406
        V setValue(V value);
jaroslav@557
   407
jaroslav@557
   408
        /**
jaroslav@557
   409
         * Compares the specified object with this entry for equality.
jaroslav@557
   410
         * Returns <tt>true</tt> if the given object is also a map entry and
jaroslav@557
   411
         * the two entries represent the same mapping.  More formally, two
jaroslav@557
   412
         * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
jaroslav@557
   413
         * if<pre>
jaroslav@557
   414
         *     (e1.getKey()==null ?
jaroslav@557
   415
         *      e2.getKey()==null : e1.getKey().equals(e2.getKey()))  &amp;&amp;
jaroslav@557
   416
         *     (e1.getValue()==null ?
jaroslav@557
   417
         *      e2.getValue()==null : e1.getValue().equals(e2.getValue()))
jaroslav@557
   418
         * </pre>
jaroslav@557
   419
         * This ensures that the <tt>equals</tt> method works properly across
jaroslav@557
   420
         * different implementations of the <tt>Map.Entry</tt> interface.
jaroslav@557
   421
         *
jaroslav@557
   422
         * @param o object to be compared for equality with this map entry
jaroslav@557
   423
         * @return <tt>true</tt> if the specified object is equal to this map
jaroslav@557
   424
         *         entry
jaroslav@557
   425
         */
jaroslav@557
   426
        boolean equals(Object o);
jaroslav@557
   427
jaroslav@557
   428
        /**
jaroslav@557
   429
         * Returns the hash code value for this map entry.  The hash code
jaroslav@557
   430
         * of a map entry <tt>e</tt> is defined to be: <pre>
jaroslav@557
   431
         *     (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
jaroslav@557
   432
         *     (e.getValue()==null ? 0 : e.getValue().hashCode())
jaroslav@557
   433
         * </pre>
jaroslav@557
   434
         * This ensures that <tt>e1.equals(e2)</tt> implies that
jaroslav@557
   435
         * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
jaroslav@557
   436
         * <tt>e1</tt> and <tt>e2</tt>, as required by the general
jaroslav@557
   437
         * contract of <tt>Object.hashCode</tt>.
jaroslav@557
   438
         *
jaroslav@557
   439
         * @return the hash code value for this map entry
jaroslav@557
   440
         * @see Object#hashCode()
jaroslav@557
   441
         * @see Object#equals(Object)
jaroslav@557
   442
         * @see #equals(Object)
jaroslav@557
   443
         */
jaroslav@557
   444
        int hashCode();
jaroslav@557
   445
    }
jaroslav@557
   446
jaroslav@557
   447
    // Comparison and hashing
jaroslav@557
   448
jaroslav@557
   449
    /**
jaroslav@557
   450
     * Compares the specified object with this map for equality.  Returns
jaroslav@557
   451
     * <tt>true</tt> if the given object is also a map and the two maps
jaroslav@557
   452
     * represent the same mappings.  More formally, two maps <tt>m1</tt> and
jaroslav@557
   453
     * <tt>m2</tt> represent the same mappings if
jaroslav@557
   454
     * <tt>m1.entrySet().equals(m2.entrySet())</tt>.  This ensures that the
jaroslav@557
   455
     * <tt>equals</tt> method works properly across different implementations
jaroslav@557
   456
     * of the <tt>Map</tt> interface.
jaroslav@557
   457
     *
jaroslav@557
   458
     * @param o object to be compared for equality with this map
jaroslav@557
   459
     * @return <tt>true</tt> if the specified object is equal to this map
jaroslav@557
   460
     */
jaroslav@557
   461
    boolean equals(Object o);
jaroslav@557
   462
jaroslav@557
   463
    /**
jaroslav@557
   464
     * Returns the hash code value for this map.  The hash code of a map is
jaroslav@557
   465
     * defined to be the sum of the hash codes of each entry in the map's
jaroslav@557
   466
     * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
jaroslav@557
   467
     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
jaroslav@557
   468
     * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
jaroslav@557
   469
     * {@link Object#hashCode}.
jaroslav@557
   470
     *
jaroslav@557
   471
     * @return the hash code value for this map
jaroslav@557
   472
     * @see Map.Entry#hashCode()
jaroslav@557
   473
     * @see Object#equals(Object)
jaroslav@557
   474
     * @see #equals(Object)
jaroslav@557
   475
     */
jaroslav@557
   476
    int hashCode();
jaroslav@557
   477
jaroslav@557
   478
}