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