1. /*
  2. * @(#)SortedMap.java 1.8 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. * 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 JDK implementation
  52. * (TreeMap) complies.
  53. *
  54. * @author Josh Bloch
  55. * @version 1.8 11/29/01
  56. * @see Map
  57. * @see TreeMap
  58. * @see SortedSet
  59. * @see Comparator
  60. * @see Comparable
  61. * @see Collection
  62. * @see ClassCastException
  63. * @since JDK1.2
  64. */
  65. public interface SortedMap extends Map {
  66. /**
  67. * Returns the comparator associated with this sorted map, or
  68. * <tt>null</tt> if it uses its keys' natural ordering.
  69. *
  70. * @return the comparator associated with this sorted map, or
  71. * <tt>null</tt> if it uses its keys' natural ordering.
  72. */
  73. Comparator comparator();
  74. /**
  75. * Returns a view of the portion of this sorted map whose keys range from
  76. * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. (If
  77. * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned sorted map
  78. * is empty.) The returned sorted map is backed by this sorted map, so
  79. * changes in the returned sorted map are reflected in this sorted map,
  80. * and vice-versa. The returned Map supports all optional map operations
  81. * that this sorted map supports.<p>
  82. *
  83. * The map returned by this method will throw an
  84. * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
  85. * outside the specified range.<p>
  86. *
  87. * Note: this method always returns a <i>half-open range</i> (which
  88. * includes its low endpoint but not its high endpoint). If you need a
  89. * <i>closed range</i> (which includes both endpoints), and the key type
  90. * allows for calculation of the successor a given key, merely request the
  91. * subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>.
  92. * For example, suppose that <tt>m</tt> is a map whose keys are strings.
  93. * The following idiom obtains a view containing all of the key-value
  94. * mappings in <tt>m</tt> whose keys are between <tt>low</tt> and
  95. * <tt>high</tt>, inclusive:
  96. *
  97. * <pre> Map sub = m.subMap(low, high+"\0");</pre>
  98. *
  99. * A similarly technique can be used to generate an <i>open range</i>
  100. * (which contains neither endpoint). The following idiom obtains a
  101. * view containing all of the key-value mappings in <tt>m</tt> whose keys
  102. * are between <tt>low</tt> and <tt>high</tt>, exclusive:
  103. *
  104. * <pre> Map sub = m.subMap(low+"\0", high);</pre>
  105. *
  106. * @param fromKey low endpoint (inclusive) of the subMap.
  107. * @param toKey high endpoint (exclusive) of the subMap.
  108. * @return a view of the specified range within this sorted map.
  109. *
  110. * @throws ClassCastException if <tt>fromKey</tt> or <tt>toKey</tt> cannot
  111. * be compared with the keys currently in the sorted map.
  112. * (Implementations may, but are not required to, throw this
  113. * exception under these circumstances.)
  114. * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
  115. * <tt>null</tt> and this sorted map does not tolerate
  116. * <tt>null</tt> keys.
  117. * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
  118. * <tt>toKey</tt>.
  119. */
  120. SortedMap subMap(Object fromKey, Object toKey);
  121. /**
  122. * Returns a view of the portion of this sorted map whose keys are
  123. * strictly less than toKey. The returned sorted map is backed by this
  124. * sorted map, so changes in the returned sorted map are reflected in this
  125. * sorted map, and vice-versa. The returned map supports all optional map
  126. * operations that this sorted map supports.<p>
  127. *
  128. * The map returned by this method will throw an IllegalArgumentException
  129. * if the user attempts to insert a key outside the specified range.<p>
  130. *
  131. * Note: this method always returns a view that does not contain its
  132. * (high) endpoint. If you need a view that does contain this endpoint,
  133. * and the key type allows for calculation of the successor a given
  134. * key, merely request a headMap bounded by successor(highEndpoint).
  135. * For example, suppose that suppose that <tt>m</tt> is a map whose keys
  136. * are strings. The following idiom obtains a view containing all of the
  137. * key-value mappings in <tt>m</tt> whose keys are less than or equal to
  138. * <tt>high</tt>:
  139. *
  140. * <pre> Map head = m.headMap(high+"\0");</pre>
  141. *
  142. * @param toKey high endpoint (exclusive) of the subMap.
  143. * @return a view of the specified initial range of this sorted map.
  144. *
  145. * @throws ClassCastException if <tt>toKey</tt> cannot be compared with
  146. * the keys currently in the sorted map. (Implementations may,
  147. * but are not required to, throw this exception under these
  148. * circumstances.)
  149. * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and
  150. * this sorted map does not tolerate <tt>null</tt> keys.
  151. */
  152. SortedMap headMap(Object toKey);
  153. /**
  154. * Returns a view of the portion of this sorted map whose keys are greater
  155. * than or equal to <tt>fromKey</tt>. The returned sorted map is backed
  156. * by this sorted map, so changes in the returned sorted map are reflected
  157. * in this sorted map, and vice-versa. The returned map supports all
  158. * optional map operations that this sorted map supports.<p>
  159. *
  160. * The map returned by this method will throw an
  161. * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
  162. * outside the specified range.<p>
  163. *
  164. * Note: this method always returns a view that contains its (low)
  165. * endpoint. If you need a view that does not contain this endpoint, and
  166. * the element type allows for calculation of the successor a given value,
  167. * merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>.
  168. * For example, suppose that suppose that <tt>m</tt> is a map whose keys
  169. * are strings. The following idiom obtains a view containing all of the
  170. * key-value mappings in <tt>m</tt> whose keys are strictly greater than
  171. * <tt>low</tt>:
  172. *
  173. * <pre> Map tail = m.tailMap(low+"\0");</pre>
  174. *
  175. * @param fromKey low endpoint (inclusive) of the tailMap.
  176. * @return a view of the specified final range of this sorted map.
  177. *
  178. * @throws ClassCastException if <tt>fromKey</tt> cannot be compared with
  179. * the keys currently in the sorted map. (Implementations may,
  180. * but are not required to, throw this exception under these
  181. * circumstances.)
  182. *
  183. * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and
  184. * this sorted map does not tolerate <tt>null</tt> keys.
  185. */
  186. SortedMap tailMap(Object fromKey);
  187. /**
  188. * Returns the first (lowest) key currently in this sorted map.
  189. *
  190. * @return the first (lowest) key currently in this sorted map.
  191. * @throws NoSuchElementException if this map is empty.
  192. */
  193. Object firstKey();
  194. /**
  195. * Returns the last (highest) key currently in this sorted map.
  196. *
  197. * @return the last (highest) key currently in this sorted map.
  198. * @throws NoSuchElementException if this map is empty.
  199. */
  200. Object lastKey();
  201. }