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