1. /*
  2. * @(#)SortedMap.java 1.15 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. * A map that further guarantees that it will be in ascending key order,
  10. * sorted according to the <i>natural ordering</i> of its keys (see the
  11. * <tt>Comparable</tt> interface), or by a comparator provided at sorted map
  12. * creation time. This order is reflected when iterating over the sorted
  13. * map's collection views (returned by the <tt>entrySet</tt>, <tt>keySet</tt>
  14. * and <tt>values</tt> methods). Several additional operations are provided
  15. * to take advantage of the ordering. (This interface is the map analogue of
  16. * the <tt>SortedSet</tt> interface.)<p>
  17. *
  18. * All keys inserted into a sorted map must implement the <tt>Comparable</tt>
  19. * interface (or be accepted by the specified comparator). Furthermore, all
  20. * such keys must be <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> (or
  21. * <tt>comparator.compare(k1, k2)</tt>) must not throw a
  22. * <tt>ClassCastException</tt> for any elements <tt>k1</tt> and <tt>k2</tt> in
  23. * the sorted map. Attempts to violate this restriction will cause the
  24. * offending method or constructor invocation to throw a
  25. * <tt>ClassCastException</tt>.<p>
  26. *
  27. * Note that the ordering maintained by a sorted map (whether or not an
  28. * explicit comparator is provided) must be <i>consistent with equals</i> if
  29. * the sorted map is to correctly implement the <tt>Map</tt> interface. (See
  30. * the <tt>Comparable</tt> interface or <tt>Comparator</tt> interface for a
  31. * precise definition of <i>consistent with equals</i>.) This is so because
  32. * the <tt>Map</tt> interface is defined in terms of the <tt>equals</tt>
  33. * operation, but a sorted map performs all key comparisons using its
  34. * <tt>compareTo</tt> (or <tt>compare</tt>) method, so two keys that are
  35. * deemed equal by this method are, from the standpoint of the sorted map,
  36. * equal. The behavior of a tree map <i>is</i> well-defined even if its
  37. * ordering is inconsistent with equals; it just fails to obey the general
  38. * contract of the <tt>Map</tt> interface.<p>
  39. *
  40. * All general-purpose sorted map implementation classes should provide four
  41. * "standard" constructors: 1) A void (no arguments) constructor, which
  42. * creates an empty sorted map sorted according to the <i>natural order</i> of
  43. * its keys. 2) A constructor with a single argument of type
  44. * <tt>Comparator</tt>, which creates an empty sorted map sorted according to
  45. * the specified comparator. 3) A constructor with a single argument of type
  46. * <tt>Map</tt>, which creates a new map with the same key-value mappings as
  47. * its argument, sorted according to the keys' natural ordering. 4) A
  48. * constructor with a single argument of type sorted map, which creates a new
  49. * sorted map with the same key-value mappings and the same ordering as the
  50. * input sorted map. There is no way to enforce this recommendation (as
  51. * interfaces cannot contain constructors) but the SDK implementation
  52. * (TreeMap) complies.<p>
  53. *
  54. * This interface is a member of the
  55. * <a href="{@docRoot}/../guide/collections/index.html">
  56. * Java Collections Framework</a>.
  57. *
  58. * @author Josh Bloch
  59. * @version 1.15, 01/23/03
  60. * @see Map
  61. * @see TreeMap
  62. * @see SortedSet
  63. * @see Comparator
  64. * @see Comparable
  65. * @see Collection
  66. * @see ClassCastException
  67. * @since 1.2
  68. */
  69. public interface SortedMap extends Map {
  70. /**
  71. * Returns the comparator associated with this sorted map, or
  72. * <tt>null</tt> if it uses its keys' natural ordering.
  73. *
  74. * @return the comparator associated with this sorted map, or
  75. * <tt>null</tt> if it uses its keys' natural ordering.
  76. */
  77. Comparator comparator();
  78. /**
  79. * Returns a view of the portion of this sorted map whose keys range from
  80. * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. (If
  81. * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned sorted map
  82. * is empty.) The returned sorted map is backed by this sorted map, so
  83. * changes in the returned sorted map are reflected in this sorted map,
  84. * and vice-versa. The returned Map supports all optional map operations
  85. * that this sorted map supports.<p>
  86. *
  87. * The map returned by this method will throw an
  88. * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
  89. * outside the specified range.<p>
  90. *
  91. * Note: this method always returns a <i>half-open range</i> (which
  92. * includes its low endpoint but not its high endpoint). If you need a
  93. * <i>closed range</i> (which includes both endpoints), and the key type
  94. * allows for calculation of the successor a given key, merely request the
  95. * subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>.
  96. * For example, suppose that <tt>m</tt> is a map whose keys are strings.
  97. * The following idiom obtains a view containing all of the key-value
  98. * mappings in <tt>m</tt> whose keys are between <tt>low</tt> and
  99. * <tt>high</tt>, inclusive:
  100. *
  101. * <pre> Map sub = m.subMap(low, high+"\0");</pre>
  102. *
  103. * A similarly technique can be used to generate an <i>open range</i>
  104. * (which contains neither endpoint). The following idiom obtains a
  105. * view containing all of the key-value mappings in <tt>m</tt> whose keys
  106. * are between <tt>low</tt> and <tt>high</tt>, exclusive:
  107. *
  108. * <pre> Map sub = m.subMap(low+"\0", high);</pre>
  109. *
  110. * @param fromKey low endpoint (inclusive) of the subMap.
  111. * @param toKey high endpoint (exclusive) of the subMap.
  112. * @return a view of the specified range within this sorted map.
  113. *
  114. * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
  115. * cannot be compared to one another using this map's comparator
  116. * (or, if the map has no comparator, using natural ordering).
  117. * Implementations may, but are not required to, throw this
  118. * exception if <tt>fromKey</tt> or <tt>toKey</tt>
  119. * cannot be compared to keys currently in the map.
  120. * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
  121. * <tt>toKey</tt> or if this map is itself a subMap, headMap,
  122. * or tailMap, and <tt>fromKey</tt> or <tt>toKey</tt> are not
  123. * within the specified range of the subMap, headMap, or tailMap.
  124. * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
  125. * <tt>null</tt> and this sorted map does not tolerate
  126. * <tt>null</tt> keys.
  127. */
  128. SortedMap subMap(Object fromKey, Object toKey);
  129. /**
  130. * Returns a view of the portion of this sorted map whose keys are
  131. * strictly less than toKey. The returned sorted map is backed by this
  132. * sorted map, so changes in the returned sorted map are reflected in this
  133. * sorted map, and vice-versa. The returned map supports all optional map
  134. * operations that this sorted map supports.<p>
  135. *
  136. * The map returned by this method will throw an IllegalArgumentException
  137. * if the user attempts to insert a key outside the specified range.<p>
  138. *
  139. * Note: this method always returns a view that does not contain its
  140. * (high) endpoint. If you need a view that does contain this endpoint,
  141. * and the key type allows for calculation of the successor a given
  142. * key, merely request a headMap bounded by successor(highEndpoint).
  143. * For example, suppose that suppose that <tt>m</tt> is a map whose keys
  144. * are strings. The following idiom obtains a view containing all of the
  145. * key-value mappings in <tt>m</tt> whose keys are less than or equal to
  146. * <tt>high</tt>:
  147. *
  148. * <pre> Map head = m.headMap(high+"\0");</pre>
  149. *
  150. * @param toKey high endpoint (exclusive) of the subMap.
  151. * @return a view of the specified initial range of this sorted map.
  152. * @throws ClassCastException if <tt>toKey</tt> is not compatible
  153. * with this map's comparator (or, if the map has no comparator,
  154. * if <tt>toKey</tt> does not implement <tt>Comparable</tt>).
  155. * Implementations may, but are not required to, throw this
  156. * exception if <tt>toKey</tt> cannot be compared to keys
  157. * currently in the map.
  158. * @throws IllegalArgumentException if this map is itself a subMap,
  159. * headMap, or tailMap, and <tt>toKey</tt> is not within the
  160. * specified range of the subMap, headMap, or tailMap.
  161. * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and
  162. * this sorted map does not tolerate <tt>null</tt> keys.
  163. */
  164. SortedMap headMap(Object toKey);
  165. /**
  166. * Returns a view of the portion of this sorted map whose keys are greater
  167. * than or equal to <tt>fromKey</tt>. The returned sorted map is backed
  168. * by this sorted map, so changes in the returned sorted map are reflected
  169. * in this sorted map, and vice-versa. The returned map supports all
  170. * optional map operations that this sorted map supports.<p>
  171. *
  172. * The map returned by this method will throw an
  173. * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
  174. * outside the specified range.<p>
  175. *
  176. * Note: this method always returns a view that contains its (low)
  177. * endpoint. If you need a view that does not contain this endpoint, and
  178. * the element type allows for calculation of the successor a given value,
  179. * merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>.
  180. * For example, suppose that suppose that <tt>m</tt> is a map whose keys
  181. * are strings. The following idiom obtains a view containing all of the
  182. * key-value mappings in <tt>m</tt> whose keys are strictly greater than
  183. * <tt>low</tt>:
  184. *
  185. * <pre> Map tail = m.tailMap(low+"\0");</pre>
  186. *
  187. * @param fromKey low endpoint (inclusive) of the tailMap.
  188. * @return a view of the specified final range of this sorted map.
  189. * @throws ClassCastException if <tt>fromKey</tt> is not compatible
  190. * with this map's comparator (or, if the map has no comparator,
  191. * if <tt>fromKey</tt> does not implement <tt>Comparable</tt>).
  192. * Implementations may, but are not required to, throw this
  193. * exception if <tt>fromKey</tt> cannot be compared to keys
  194. * currently in the map.
  195. * @throws IllegalArgumentException if this map is itself a subMap,
  196. * headMap, or tailMap, and <tt>fromKey</tt> is not within the
  197. * specified range of the subMap, headMap, or tailMap.
  198. * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and
  199. * this sorted map does not tolerate <tt>null</tt> keys.
  200. */
  201. SortedMap tailMap(Object fromKey);
  202. /**
  203. * Returns the first (lowest) key currently in this sorted map.
  204. *
  205. * @return the first (lowest) key currently in this sorted map.
  206. * @throws NoSuchElementException if this map is empty.
  207. */
  208. Object firstKey();
  209. /**
  210. * Returns the last (highest) key currently in this sorted map.
  211. *
  212. * @return the last (highest) key currently in this sorted map.
  213. * @throws NoSuchElementException if this map is empty.
  214. */
  215. Object lastKey();
  216. }