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