jaroslav@557: /* jaroslav@557: * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@557: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@557: * jaroslav@557: * This code is free software; you can redistribute it and/or modify it jaroslav@557: * under the terms of the GNU General Public License version 2 only, as jaroslav@557: * published by the Free Software Foundation. Oracle designates this jaroslav@557: * particular file as subject to the "Classpath" exception as provided jaroslav@557: * by Oracle in the LICENSE file that accompanied this code. jaroslav@557: * jaroslav@557: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@557: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@557: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@557: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@557: * accompanied this code). jaroslav@557: * jaroslav@557: * You should have received a copy of the GNU General Public License version jaroslav@557: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@557: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@557: * jaroslav@557: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@557: * or visit www.oracle.com if you need additional information or have any jaroslav@557: * questions. jaroslav@557: */ jaroslav@557: jaroslav@557: package java.util; jaroslav@557: jaroslav@557: /** jaroslav@557: * An object that maps keys to values. A map cannot contain duplicate keys; jaroslav@557: * each key can map to at most one value. jaroslav@557: * jaroslav@557: *
This interface takes the place of the Dictionary class, which jaroslav@557: * was a totally abstract class rather than an interface. jaroslav@557: * jaroslav@557: *
The Map interface provides three collection views, which jaroslav@557: * allow a map's contents to be viewed as a set of keys, collection of values, jaroslav@557: * or set of key-value mappings. The order of a map is defined as jaroslav@557: * the order in which the iterators on the map's collection views return their jaroslav@557: * elements. Some map implementations, like the TreeMap class, make jaroslav@557: * specific guarantees as to their order; others, like the HashMap jaroslav@557: * class, do not. jaroslav@557: * jaroslav@557: *
Note: great care must be exercised if mutable objects are used as map jaroslav@557: * keys. The behavior of a map is not specified if the value of an object is jaroslav@557: * changed in a manner that affects equals comparisons while the jaroslav@557: * object is a key in the map. A special case of this prohibition is that it jaroslav@557: * is not permissible for a map to contain itself as a key. While it is jaroslav@557: * permissible for a map to contain itself as a value, extreme caution is jaroslav@557: * advised: the equals and hashCode methods are no longer jaroslav@557: * well defined on such a map. jaroslav@557: * jaroslav@557: *
All general-purpose map implementation classes should provide two jaroslav@557: * "standard" constructors: a void (no arguments) constructor which creates an jaroslav@557: * empty map, and a constructor with a single argument of type Map, jaroslav@557: * which creates a new map with the same key-value mappings as its argument. jaroslav@557: * In effect, the latter constructor allows the user to copy any map, jaroslav@557: * producing an equivalent map of the desired class. There is no way to jaroslav@557: * enforce this recommendation (as interfaces cannot contain constructors) but jaroslav@557: * all of the general-purpose map implementations in the JDK comply. jaroslav@557: * jaroslav@557: *
The "destructive" methods contained in this interface, that is, the jaroslav@557: * methods that modify the map on which they operate, are specified to throw jaroslav@557: * UnsupportedOperationException if this map does not support the jaroslav@557: * operation. If this is the case, these methods may, but are not required jaroslav@557: * to, throw an UnsupportedOperationException if the invocation would jaroslav@557: * have no effect on the map. For example, invoking the {@link #putAll(Map)} jaroslav@557: * method on an unmodifiable map may, but is not required to, throw the jaroslav@557: * exception if the map whose mappings are to be "superimposed" is empty. jaroslav@557: * jaroslav@557: *
Some map implementations have restrictions on the keys and values they jaroslav@557: * may contain. For example, some implementations prohibit null keys and jaroslav@557: * values, and some have restrictions on the types of their keys. Attempting jaroslav@557: * to insert an ineligible key or value throws an unchecked exception, jaroslav@557: * typically NullPointerException or ClassCastException. jaroslav@557: * Attempting to query the presence of an ineligible key or value may throw an jaroslav@557: * exception, or it may simply return false; some implementations will exhibit jaroslav@557: * the former behavior and some will exhibit the latter. More generally, jaroslav@557: * attempting an operation on an ineligible key or value whose completion jaroslav@557: * would not result in the insertion of an ineligible element into the map may jaroslav@557: * throw an exception or it may succeed, at the option of the implementation. jaroslav@557: * Such exceptions are marked as "optional" in the specification for this jaroslav@557: * interface. jaroslav@557: * jaroslav@557: *
This interface is a member of the jaroslav@557: * jaroslav@557: * Java Collections Framework. jaroslav@557: * jaroslav@557: *
Many methods in Collections Framework interfaces are defined
jaroslav@557: * in terms of the {@link Object#equals(Object) equals} method. For
jaroslav@557: * example, the specification for the {@link #containsKey(Object)
jaroslav@557: * containsKey(Object key)} method says: "returns true if and
jaroslav@557: * only if this map contains a mapping for a key k such that
jaroslav@557: * (key==null ? k==null : key.equals(k))." This specification should
jaroslav@557: * not be construed to imply that invoking Map.containsKey
jaroslav@557: * with a non-null argument key will cause key.equals(k) to
jaroslav@557: * be invoked for any key k. Implementations are free to
jaroslav@557: * implement optimizations whereby the equals invocation is avoided,
jaroslav@557: * for example, by first comparing the hash codes of the two keys. (The
jaroslav@557: * {@link Object#hashCode()} specification guarantees that two objects with
jaroslav@557: * unequal hash codes cannot be equal.) More generally, implementations of
jaroslav@557: * the various Collections Framework interfaces are free to take advantage of
jaroslav@557: * the specified behavior of underlying {@link Object} methods wherever the
jaroslav@557: * implementor deems it appropriate.
jaroslav@557: *
jaroslav@557: * @param More formally, if this map contains a mapping from a key
jaroslav@557: * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
jaroslav@557: * key.equals(k))}, then this method returns {@code v}; otherwise
jaroslav@557: * it returns {@code null}. (There can be at most one such mapping.)
jaroslav@557: *
jaroslav@557: * If this map permits null values, then a return value of
jaroslav@557: * {@code null} does not necessarily indicate that the map
jaroslav@557: * contains no mapping for the key; it's also possible that the map
jaroslav@557: * explicitly maps the key to {@code null}. The {@link #containsKey
jaroslav@557: * containsKey} operation may be used to distinguish these two cases.
jaroslav@557: *
jaroslav@557: * @param key the key whose associated value is to be returned
jaroslav@557: * @return the value to which the specified key is mapped, or
jaroslav@557: * {@code null} if this map contains no mapping for the key
jaroslav@557: * @throws ClassCastException if the key is of an inappropriate type for
jaroslav@557: * this map
jaroslav@557: * (optional)
jaroslav@557: * @throws NullPointerException if the specified key is null and this map
jaroslav@557: * does not permit null keys
jaroslav@557: * (optional)
jaroslav@557: */
jaroslav@557: V get(Object key);
jaroslav@557:
jaroslav@557: // Modification Operations
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Associates the specified value with the specified key in this map
jaroslav@557: * (optional operation). If the map previously contained a mapping for
jaroslav@557: * the key, the old value is replaced by the specified value. (A map
jaroslav@557: * m is said to contain a mapping for a key k if and only
jaroslav@557: * if {@link #containsKey(Object) m.containsKey(k)} would return
jaroslav@557: * true.)
jaroslav@557: *
jaroslav@557: * @param key key with which the specified value is to be associated
jaroslav@557: * @param value value to be associated with the specified key
jaroslav@557: * @return the previous value associated with key, or
jaroslav@557: * null if there was no mapping for key.
jaroslav@557: * (A null return can also indicate that the map
jaroslav@557: * previously associated null with key,
jaroslav@557: * if the implementation supports null values.)
jaroslav@557: * @throws UnsupportedOperationException if the put operation
jaroslav@557: * is not supported by this map
jaroslav@557: * @throws ClassCastException if the class of the specified key or value
jaroslav@557: * prevents it from being stored in this map
jaroslav@557: * @throws NullPointerException if the specified key or value is null
jaroslav@557: * and this map does not permit null keys or values
jaroslav@557: * @throws IllegalArgumentException if some property of the specified key
jaroslav@557: * or value prevents it from being stored in this map
jaroslav@557: */
jaroslav@557: V put(K key, V value);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Removes the mapping for a key from this map if it is present
jaroslav@557: * (optional operation). More formally, if this map contains a mapping
jaroslav@557: * from key k to value v such that
jaroslav@557: * Returns the value to which this map previously associated the key,
jaroslav@557: * or null if the map contained no mapping for the key.
jaroslav@557: *
jaroslav@557: * If this map permits null values, then a return value of
jaroslav@557: * null does not necessarily indicate that the map
jaroslav@557: * contained no mapping for the key; it's also possible that the map
jaroslav@557: * explicitly mapped the key to null.
jaroslav@557: *
jaroslav@557: * The map will not contain a mapping for the specified key once the
jaroslav@557: * call returns.
jaroslav@557: *
jaroslav@557: * @param key key whose mapping is to be removed from the map
jaroslav@557: * @return the previous value associated with key, or
jaroslav@557: * null if there was no mapping for key.
jaroslav@557: * @throws UnsupportedOperationException if the remove operation
jaroslav@557: * is not supported by this map
jaroslav@557: * @throws ClassCastException if the key is of an inappropriate type for
jaroslav@557: * this map
jaroslav@557: * (optional)
jaroslav@557: * @throws NullPointerException if the specified key is null and this
jaroslav@557: * map does not permit null keys
jaroslav@557: * (optional)
jaroslav@557: */
jaroslav@557: V remove(Object key);
jaroslav@557:
jaroslav@557:
jaroslav@557: // Bulk Operations
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Copies all of the mappings from the specified map to this map
jaroslav@557: * (optional operation). The effect of this call is equivalent to that
jaroslav@557: * of calling {@link #put(Object,Object) put(k, v)} on this map once
jaroslav@557: * for each mapping from key k to value v in the
jaroslav@557: * specified map. The behavior of this operation is undefined if the
jaroslav@557: * specified map is modified while the operation is in progress.
jaroslav@557: *
jaroslav@557: * @param m mappings to be stored in this map
jaroslav@557: * @throws UnsupportedOperationException if the putAll operation
jaroslav@557: * is not supported by this map
jaroslav@557: * @throws ClassCastException if the class of a key or value in the
jaroslav@557: * specified map prevents it from being stored in this map
jaroslav@557: * @throws NullPointerException if the specified map is null, or if
jaroslav@557: * this map does not permit null keys or values, and the
jaroslav@557: * specified map contains null keys or values
jaroslav@557: * @throws IllegalArgumentException if some property of a key or value in
jaroslav@557: * the specified map prevents it from being stored in this map
jaroslav@557: */
jaroslav@557: void putAll(Map extends K, ? extends V> m);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Removes all of the mappings from this map (optional operation).
jaroslav@557: * The map will be empty after this call returns.
jaroslav@557: *
jaroslav@557: * @throws UnsupportedOperationException if the clear operation
jaroslav@557: * is not supported by this map
jaroslav@557: */
jaroslav@557: void clear();
jaroslav@557:
jaroslav@557:
jaroslav@557: // Views
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns a {@link Set} view of the keys contained in this map.
jaroslav@557: * The set is backed by the map, so changes to the map are
jaroslav@557: * reflected in the set, and vice-versa. If the map is modified
jaroslav@557: * while an iteration over the set is in progress (except through
jaroslav@557: * the iterator's own remove operation), the results of
jaroslav@557: * the iteration are undefined. The set supports element removal,
jaroslav@557: * which removes the corresponding mapping from the map, via the
jaroslav@557: * Iterator.remove, Set.remove,
jaroslav@557: * removeAll, retainAll, and clear
jaroslav@557: * operations. It does not support the add or addAll
jaroslav@557: * operations.
jaroslav@557: *
jaroslav@557: * @return a set view of the keys contained in this map
jaroslav@557: */
jaroslav@557: Set(key==null ? k==null : key.equals(k))
, that mapping
jaroslav@557: * is removed. (The map can contain at most one such mapping.)
jaroslav@557: *
jaroslav@557: *
jaroslav@557: * (e1.getKey()==null ?
jaroslav@557: * e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
jaroslav@557: * (e1.getValue()==null ?
jaroslav@557: * e2.getValue()==null : e1.getValue().equals(e2.getValue()))
jaroslav@557: *
jaroslav@557: * This ensures that the equals method works properly across
jaroslav@557: * different implementations of the Map.Entry interface.
jaroslav@557: *
jaroslav@557: * @param o object to be compared for equality with this map entry
jaroslav@557: * @return true if the specified object is equal to this map
jaroslav@557: * entry
jaroslav@557: */
jaroslav@557: boolean equals(Object o);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns the hash code value for this map entry. The hash code
jaroslav@557: * of a map entry e is defined to be:
jaroslav@557: * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
jaroslav@557: * (e.getValue()==null ? 0 : e.getValue().hashCode())
jaroslav@557: *
jaroslav@557: * This ensures that e1.equals(e2) implies that
jaroslav@557: * e1.hashCode()==e2.hashCode() for any two Entries
jaroslav@557: * e1 and e2, as required by the general
jaroslav@557: * contract of Object.hashCode.
jaroslav@557: *
jaroslav@557: * @return the hash code value for this map entry
jaroslav@557: * @see Object#hashCode()
jaroslav@557: * @see Object#equals(Object)
jaroslav@557: * @see #equals(Object)
jaroslav@557: */
jaroslav@557: int hashCode();
jaroslav@557: }
jaroslav@557:
jaroslav@557: // Comparison and hashing
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Compares the specified object with this map for equality. Returns
jaroslav@557: * true if the given object is also a map and the two maps
jaroslav@557: * represent the same mappings. More formally, two maps m1 and
jaroslav@557: * m2 represent the same mappings if
jaroslav@557: * m1.entrySet().equals(m2.entrySet()). This ensures that the
jaroslav@557: * equals method works properly across different implementations
jaroslav@557: * of the Map interface.
jaroslav@557: *
jaroslav@557: * @param o object to be compared for equality with this map
jaroslav@557: * @return true if the specified object is equal to this map
jaroslav@557: */
jaroslav@557: boolean equals(Object o);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns the hash code value for this map. The hash code of a map is
jaroslav@557: * defined to be the sum of the hash codes of each entry in the map's
jaroslav@557: * entrySet() view. This ensures that m1.equals(m2)
jaroslav@557: * implies that m1.hashCode()==m2.hashCode() for any two maps
jaroslav@557: * m1 and m2, as required by the general contract of
jaroslav@557: * {@link Object#hashCode}.
jaroslav@557: *
jaroslav@557: * @return the hash code value for this map
jaroslav@557: * @see Map.Entry#hashCode()
jaroslav@557: * @see Object#equals(Object)
jaroslav@557: * @see #equals(Object)
jaroslav@557: */
jaroslav@557: int hashCode();
jaroslav@557:
jaroslav@557: }