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