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