1. /*
  2. * @(#)Map.java 1.39 03/01/23
  3. *
  4. * Copyright 2003 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 SDK 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. * @author Josh Bloch
  68. * @version 1.39, 01/23/03
  69. * @see HashMap
  70. * @see TreeMap
  71. * @see Hashtable
  72. * @see SortedMap
  73. * @see Collection
  74. * @see Set
  75. * @since 1.2
  76. */
  77. public interface Map {
  78. // Query Operations
  79. /**
  80. * Returns the number of key-value mappings in this map. If the
  81. * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  82. * <tt>Integer.MAX_VALUE</tt>.
  83. *
  84. * @return the number of key-value mappings in this map.
  85. */
  86. int size();
  87. /**
  88. * Returns <tt>true</tt> if this map contains no key-value mappings.
  89. *
  90. * @return <tt>true</tt> if this map contains no key-value mappings.
  91. */
  92. boolean isEmpty();
  93. /**
  94. * Returns <tt>true</tt> if this map contains a mapping for the specified
  95. * key. More formally, returns <tt>true</tt> if and only if
  96. * this map contains at a mapping for a key <tt>k</tt> such that
  97. * <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
  98. * at most one such mapping.)
  99. *
  100. * @param key key whose presence in this map is to be tested.
  101. * @return <tt>true</tt> if this map contains a mapping for the specified
  102. * key.
  103. *
  104. * @throws ClassCastException if the key is of an inappropriate type for
  105. * this map (optional).
  106. * @throws NullPointerException if the key is <tt>null</tt> and this map
  107. * does not not permit <tt>null</tt> keys (optional).
  108. */
  109. boolean containsKey(Object key);
  110. /**
  111. * Returns <tt>true</tt> if this map maps one or more keys to the
  112. * specified value. More formally, returns <tt>true</tt> if and only if
  113. * this map contains at least one mapping to a value <tt>v</tt> such that
  114. * <tt>(value==null ? v==null : value.equals(v))</tt>. This operation
  115. * will probably require time linear in the map size for most
  116. * implementations of the <tt>Map</tt> interface.
  117. *
  118. * @param value value whose presence in this map is to be tested.
  119. * @return <tt>true</tt> if this map maps one or more keys to the
  120. * specified value.
  121. * @throws ClassCastException if the value is of an inappropriate type for
  122. * this map (optional).
  123. * @throws NullPointerException if the value is <tt>null</tt> and this map
  124. * does not not permit <tt>null</tt> values (optional).
  125. */
  126. boolean containsValue(Object value);
  127. /**
  128. * Returns the value to which this map maps the specified key. Returns
  129. * <tt>null</tt> if the map contains no mapping for this key. A return
  130. * value of <tt>null</tt> does not <i>necessarily</i> indicate that the
  131. * map contains no mapping for the key; it's also possible that the map
  132. * explicitly maps the key to <tt>null</tt>. The <tt>containsKey</tt>
  133. * operation may be used to distinguish these two cases.
  134. *
  135. * <p>More formally, if this map contains a mapping from a key
  136. * <tt>k</tt> to a value <tt>v</tt> such that <tt>(key==null ? k==null :
  137. * key.equals(k))</tt>, then this method returns <tt>v</tt> otherwise
  138. * it returns <tt>null</tt>. (There can be at most one such mapping.)
  139. *
  140. * @param key key whose associated value is to be returned.
  141. * @return the value to which this map maps the specified key, or
  142. * <tt>null</tt> if the map contains no mapping for this key.
  143. *
  144. * @throws ClassCastException if the key is of an inappropriate type for
  145. * this map (optional).
  146. * @throws NullPointerException key is <tt>null</tt> and this map does not
  147. * not permit <tt>null</tt> keys (optional).
  148. *
  149. * @see #containsKey(Object)
  150. */
  151. Object get(Object key);
  152. // Modification Operations
  153. /**
  154. * Associates the specified value with the specified key in this map
  155. * (optional operation). If the map previously contained a mapping for
  156. * this key, the old value is replaced by the specified value. (A map
  157. * <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only
  158. * if {@link #containsKey(Object) m.containsKey(k)} would return
  159. * <tt>true</tt>.))
  160. *
  161. * @param key key with which the specified value is to be associated.
  162. * @param value value to be associated with the specified key.
  163. * @return previous value associated with specified key, or <tt>null</tt>
  164. * if there was no mapping for key. A <tt>null</tt> return can
  165. * also indicate that the map previously associated <tt>null</tt>
  166. * with the specified key, if the implementation supports
  167. * <tt>null</tt> values.
  168. *
  169. * @throws UnsupportedOperationException if the <tt>put</tt> operation is
  170. * not supported by this map.
  171. * @throws ClassCastException if the class of the specified key or value
  172. * prevents it from being stored in this map.
  173. * @throws IllegalArgumentException if some aspect of this key or value
  174. * prevents it from being stored in this map.
  175. * @throws NullPointerException this map does not permit <tt>null</tt>
  176. * keys or values, and the specified key or value is
  177. * <tt>null</tt>.
  178. */
  179. Object put(Object key, Object value);
  180. /**
  181. * Removes the mapping for this key from this map if it is present
  182. * (optional operation). More formally, if this map contains a mapping
  183. * from key <tt>k</tt> to value <tt>v</tt> such that
  184. * <code>(key==null ? k==null : key.equals(k))</code>, that mapping
  185. * is removed. (The map can contain at most one such mapping.)
  186. *
  187. * <p>Returns the value to which the map previously associated the key, or
  188. * <tt>null</tt> if the map contained no mapping for this key. (A
  189. * <tt>null</tt> return can also indicate that the map previously
  190. * associated <tt>null</tt> with the specified key if the implementation
  191. * supports <tt>null</tt> values.) The map will not contain a mapping for
  192. * the specified key once the call returns.
  193. *
  194. * @param key key whose mapping is to be removed from the map.
  195. * @return previous value associated with specified key, or <tt>null</tt>
  196. * if there was no mapping for key.
  197. *
  198. * @throws ClassCastException if the key is of an inappropriate type for
  199. * this map (optional).
  200. * @throws NullPointerException if the key is <tt>null</tt> and this map
  201. * does not not permit <tt>null</tt> keys (optional).
  202. * @throws UnsupportedOperationException if the <tt>remove</tt> method is
  203. * not supported by this map.
  204. */
  205. Object remove(Object key);
  206. // Bulk Operations
  207. /**
  208. * Copies all of the mappings from the specified map to this map
  209. * (optional operation). The effect of this call is equivalent to that
  210. * of calling {@link #put(Object,Object) put(k, v)} on this map once
  211. * for each mapping from key <tt>k</tt> to value <tt>v</tt> in the
  212. * specified map. The behavior of this operation is unspecified if the
  213. * specified map is modified while the operation is in progress.
  214. *
  215. * @param t Mappings to be stored in this map.
  216. *
  217. * @throws UnsupportedOperationException if the <tt>putAll</tt> method is
  218. * not supported by this map.
  219. *
  220. * @throws ClassCastException if the class of a key or value in the
  221. * specified map prevents it from being stored in this map.
  222. *
  223. * @throws IllegalArgumentException some aspect of a key or value in the
  224. * specified map prevents it from being stored in this map.
  225. * @throws NullPointerException the specified map is <tt>null</tt>, or if
  226. * this map does not permit <tt>null</tt> keys or values, and the
  227. * specified map contains <tt>null</tt> keys or values.
  228. */
  229. void putAll(Map t);
  230. /**
  231. * Removes all mappings from this map (optional operation).
  232. *
  233. * @throws UnsupportedOperationException clear is not supported by this
  234. * map.
  235. */
  236. void clear();
  237. // Views
  238. /**
  239. * Returns a set view of the keys contained in this map. The set is
  240. * backed by the map, so changes to the map are reflected in the set, and
  241. * vice-versa. If the map is modified while an iteration over the set is
  242. * in progress, the results of the iteration are undefined. The set
  243. * supports element removal, which removes the corresponding mapping from
  244. * the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
  245. * <tt>removeAll</tt> <tt>retainAll</tt>, and <tt>clear</tt> operations.
  246. * It does not support the add or <tt>addAll</tt> operations.
  247. *
  248. * @return a set view of the keys contained in this map.
  249. */
  250. Set keySet();
  251. /**
  252. * Returns a collection view of the values contained in this map. The
  253. * collection is backed by the map, so changes to the map are reflected in
  254. * the collection, and vice-versa. If the map is modified while an
  255. * iteration over the collection is in progress, the results of the
  256. * iteration are undefined. The collection supports element removal,
  257. * which removes the corresponding mapping from the map, via the
  258. * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
  259. * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> operations.
  260. * It does not support the add or <tt>addAll</tt> operations.
  261. *
  262. * @return a collection view of the values contained in this map.
  263. */
  264. Collection values();
  265. /**
  266. * Returns a set view of the mappings contained in this map. Each element
  267. * in the returned set is a {@link Map.Entry}. The set is backed by the
  268. * map, so changes to the map are reflected in the set, and vice-versa.
  269. * If the map is modified while an iteration over the set is in progress,
  270. * the results of the iteration are undefined. The set supports element
  271. * removal, which removes the corresponding mapping from the map, via the
  272. * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
  273. * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support
  274. * the <tt>add</tt> or <tt>addAll</tt> operations.
  275. *
  276. * @return a set view of the mappings contained in this map.
  277. */
  278. Set entrySet();
  279. /**
  280. * A map entry (key-value pair). The <tt>Map.entrySet</tt> method returns
  281. * a collection-view of the map, whose elements are of this class. The
  282. * <i>only</i> way to obtain a reference to a map entry is from the
  283. * iterator of this collection-view. These <tt>Map.Entry</tt> objects are
  284. * valid <i>only</i> for the duration of the iteration; more formally,
  285. * the behavior of a map entry is undefined if the backing map has been
  286. * modified after the entry was returned by the iterator, except through
  287. * the iterator's own <tt>remove</tt> operation, or through the
  288. * <tt>setValue</tt> operation on a map entry returned by the iterator.
  289. *
  290. * @see Map#entrySet()
  291. * @since 1.2
  292. */
  293. interface Entry {
  294. /**
  295. * Returns the key corresponding to this entry.
  296. *
  297. * @return the key corresponding to this entry.
  298. */
  299. Object getKey();
  300. /**
  301. * Returns the value corresponding to this entry. If the mapping
  302. * has been removed from the backing map (by the iterator's
  303. * <tt>remove</tt> operation), the results of this call are undefined.
  304. *
  305. * @return the value corresponding to this entry.
  306. */
  307. Object getValue();
  308. /**
  309. * Replaces the value corresponding to this entry with the specified
  310. * value (optional operation). (Writes through to the map.) The
  311. * behavior of this call is undefined if the mapping has already been
  312. * removed from the map (by the iterator's <tt>remove</tt> operation).
  313. *
  314. * @param value new value to be stored in this entry.
  315. * @return old value corresponding to the entry.
  316. *
  317. * @throws UnsupportedOperationException if the <tt>put</tt> operation
  318. * is not supported by the backing map.
  319. * @throws ClassCastException if the class of the specified value
  320. * prevents it from being stored in the backing map.
  321. * @throws IllegalArgumentException if some aspect of this value
  322. * prevents it from being stored in the backing map.
  323. * @throws NullPointerException the backing map does not permit
  324. * <tt>null</tt> values, and the specified value is
  325. * <tt>null</tt>.
  326. */
  327. Object setValue(Object value);
  328. /**
  329. * Compares the specified object with this entry for equality.
  330. * Returns <tt>true</tt> if the given object is also a map entry and
  331. * the two entries represent the same mapping. More formally, two
  332. * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
  333. * if<pre>
  334. * (e1.getKey()==null ?
  335. * e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
  336. * (e1.getValue()==null ?
  337. * e2.getValue()==null : e1.getValue().equals(e2.getValue()))
  338. * </pre>
  339. * This ensures that the <tt>equals</tt> method works properly across
  340. * different implementations of the <tt>Map.Entry</tt> interface.
  341. *
  342. * @param o object to be compared for equality with this map entry.
  343. * @return <tt>true</tt> if the specified object is equal to this map
  344. * entry.
  345. */
  346. boolean equals(Object o);
  347. /**
  348. * Returns the hash code value for this map entry. The hash code
  349. * of a map entry <tt>e</tt> is defined to be: <pre>
  350. * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
  351. * (e.getValue()==null ? 0 : e.getValue().hashCode())
  352. * </pre>
  353. * This ensures that <tt>e1.equals(e2)</tt> implies that
  354. * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
  355. * <tt>e1</tt> and <tt>e2</tt>, as required by the general
  356. * contract of <tt>Object.hashCode</tt>.
  357. *
  358. * @return the hash code value for this map entry.
  359. * @see Object#hashCode()
  360. * @see Object#equals(Object)
  361. * @see #equals(Object)
  362. */
  363. int hashCode();
  364. }
  365. // Comparison and hashing
  366. /**
  367. * Compares the specified object with this map for equality. Returns
  368. * <tt>true</tt> if the given object is also a map and the two Maps
  369. * represent the same mappings. More formally, two maps <tt>t1</tt> and
  370. * <tt>t2</tt> represent the same mappings if
  371. * <tt>t1.entrySet().equals(t2.entrySet())</tt>. This ensures that the
  372. * <tt>equals</tt> method works properly across different implementations
  373. * of the <tt>Map</tt> interface.
  374. *
  375. * @param o object to be compared for equality with this map.
  376. * @return <tt>true</tt> if the specified object is equal to this map.
  377. */
  378. boolean equals(Object o);
  379. /**
  380. * Returns the hash code value for this map. The hash code of a map
  381. * is defined to be the sum of the hashCodes of each entry in the map's
  382. * entrySet view. This ensures that <tt>t1.equals(t2)</tt> implies
  383. * that <tt>t1.hashCode()==t2.hashCode()</tt> for any two maps
  384. * <tt>t1</tt> and <tt>t2</tt>, as required by the general
  385. * contract of Object.hashCode.
  386. *
  387. * @return the hash code value for this map.
  388. * @see Map.Entry#hashCode()
  389. * @see Object#hashCode()
  390. * @see Object#equals(Object)
  391. * @see #equals(Object)
  392. */
  393. int hashCode();
  394. }