ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/TreeMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/TreeMap.java (file contents):
Revision 1.2 by dl, Fri Dec 31 13:00:33 2004 UTC vs.
Revision 1.6 by dl, Wed Mar 23 11:20:27 2005 UTC

# Line 11 | Line 11 | package java.util;
11   /**
12   * Red-Black tree based implementation of the <tt>NavigableMap</tt> interface.
13   * This class guarantees that the map will be in ascending key order, sorted
14 < * according to the <i>natural order</i> for the key's class (see
14 > * according to the <i>natural order</i> for the keys' class (see
15   * <tt>Comparable</tt>), or by the comparator provided at creation time,
16   * depending on which constructor is used.<p>
17   *
# Line 61 | Line 61 | package java.util;
61   * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
62   * Therefore, it would be wrong to write a program that depended on this
63   * exception for its correctness:   <i>the fail-fast behavior of iterators
64 < * should be used only to detect bugs.</i><p>
64 > * should be used only to detect bugs.</i>
65   *
66   * <p>All <tt>Map.Entry</tt> pairs returned by methods in this class
67   * and its views represent snapshots of mappings at the time they were
# Line 203 | Line 203 | public class TreeMap<K,V>
203       *            specified key.
204       * @throws ClassCastException if the key cannot be compared with the keys
205       *                  currently in the map.
206 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
206 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
207       *                  natural ordering, or its comparator does not tolerate
208       *            <tt>null</tt> keys.
209       */
# Line 260 | Line 260 | public class TreeMap<K,V>
260       * @param key key whose associated value is to be returned.
261       * @return the value to which this map maps the specified key, or
262       *               <tt>null</tt> if the map contains no mapping for the key.
263 <     * @throws    ClassCastException key cannot be compared with the keys
263 >     * @throws    ClassCastException if key cannot be compared with the keys
264       *                  currently in the map.
265 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
265 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
266       *                  natural ordering, or its comparator does not tolerate
267       *                  <tt>null</tt> keys.
268       *
# Line 343 | Line 343 | public class TreeMap<K,V>
343       *                does not contain an entry for the key.
344       * @throws ClassCastException if the key cannot be compared with the keys
345       *                  currently in the map.
346 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
346 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
347       *                  natural order, or its comparator does not tolerate *
348       *                  <tt>null</tt> keys.
349       */
# Line 542 | Line 542 | public class TreeMap<K,V>
542       * @param key key with which the specified value is to be associated.
543       * @param value value to be associated with the specified key.
544       *
545 <     * @return previous value associated with specified key, or <tt>null</tt>
545 >     * @return the previous value associated with specified key, or <tt>null</tt>
546       *         if there was no mapping for key.  A <tt>null</tt> return can
547       *         also indicate that the map previously associated <tt>null</tt>
548       *         with the specified key.
549 <     * @throws    ClassCastException key cannot be compared with the keys
549 >     * @throws    ClassCastException if key cannot be compared with the keys
550       *            currently in the map.
551 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
551 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
552       *         natural order, or its comparator does not tolerate
553       *         <tt>null</tt> keys.
554       */
# Line 591 | Line 591 | public class TreeMap<K,V>
591       * Removes the mapping for this key from this TreeMap if present.
592       *
593       * @param  key key for which mapping should be removed
594 <     * @return previous value associated with specified key, or <tt>null</tt>
594 >     * @return the previous value associated with specified key, or <tt>null</tt>
595       *         if there was no mapping for key.  A <tt>null</tt> return can
596       *         also indicate that the map previously associated
597       *         <tt>null</tt> with the specified key.
598       *
599 <     * @throws    ClassCastException key cannot be compared with the keys
599 >     * @throws    ClassCastException if key cannot be compared with the keys
600       *            currently in the map.
601 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
601 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
602       *         natural order, or its comparator does not tolerate
603       *         <tt>null</tt> keys.
604       */
# Line 670 | Line 670 | public class TreeMap<K,V>
670      /**
671       * Returns a key-value mapping associated with the greatest
672       * key in this map, or <tt>null</tt> if the map is empty.
673     * The returned entry does <em>not</em> support
674     * the <tt>Entry.setValue</tt> method.
673       *
674       * @return an Entry with greatest key, or <tt>null</tt>
675       * if the map is empty.
# Line 723 | Line 721 | public class TreeMap<K,V>
721       * <tt>null</tt> if there is no such Entry.
722       * @throws ClassCastException if key cannot be compared with the
723       * keys currently in the map.
724 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
724 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
725       *         natural order, or its comparator does not tolerate
726       *         <tt>null</tt> keys.
727       */
# Line 742 | Line 740 | public class TreeMap<K,V>
740       * if there is no such key.
741       * @throws ClassCastException if key cannot be compared with the keys
742       *            currently in the map.
743 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
743 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
744       *         natural order, or its comparator does not tolerate
745       *         <tt>null</tt> keys.
746       */
# Line 763 | Line 761 | public class TreeMap<K,V>
761       * if there is no such Entry.
762       * @throws ClassCastException if key cannot be compared with the keys
763       *            currently in the map.
764 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
764 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
765       *         natural order, or its comparator does not tolerate
766       *         <tt>null</tt> keys.
767       */
# Line 782 | Line 780 | public class TreeMap<K,V>
780       * such key.
781       * @throws ClassCastException if key cannot be compared with the keys
782       *            currently in the map.
783 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
783 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
784       *         natural order, or its comparator does not tolerate
785       *         <tt>null</tt> keys.
786       */
# Line 801 | Line 799 | public class TreeMap<K,V>
799       * <tt>null</tt> if there is no such Entry.
800       * @throws ClassCastException if key cannot be compared with the keys
801       *            currently in the map.
802 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
802 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
803       *         natural order, or its comparator does not tolerate
804       *         <tt>null</tt> keys.
805       */
# Line 819 | Line 817 | public class TreeMap<K,V>
817       * <tt>null</tt> if there is no such key.
818       * @throws ClassCastException if key cannot be compared with the keys
819       *            currently in the map.
820 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
820 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
821       *         natural order, or its comparator does not tolerate
822       *         <tt>null</tt> keys.
823       */
# Line 838 | Line 836 | public class TreeMap<K,V>
836       * key, or <tt>null</tt> if there is no such Entry.
837       * @throws ClassCastException if key cannot be compared with the keys
838       *            currently in the map.
839 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
839 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
840       *         natural order, or its comparator does not tolerate
841       *         <tt>null</tt> keys.
842       */
# Line 856 | Line 854 | public class TreeMap<K,V>
854       * key, or <tt>null</tt> if there is no such key.
855       * @throws ClassCastException if key cannot be compared with the keys
856       *            currently in the map.
857 <     * @throws NullPointerException key is <tt>null</tt> and this map uses
857 >     * @throws NullPointerException if key is <tt>null</tt> and this map uses
858       *         natural order, or its comparator does not tolerate
859       *         <tt>null</tt> keys.
860       */
# Line 1026 | Line 1024 | public class TreeMap<K,V>
1024  
1025      /**
1026       * Returns a set view of the mappings contained in this map.  The
1027 <     * set's iterator returns the mappings in descrending key order.
1027 >     * set's iterator returns the mappings in descending key order.
1028       * Each element in the returned set is a <tt>Map.Entry</tt>.  The
1029       * set is backed by this map, so changes to this map are reflected
1030       * in the set, and vice-versa.  The set supports element removal,
# Line 1078 | Line 1076 | public class TreeMap<K,V>
1076      /**
1077       * Returns a view of the portion of this map whose keys range from
1078       * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.  (If
1079 <     * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned sorted map
1080 <     * is empty.)  The returned sorted map is backed by this map, so changes
1081 <     * in the returned sorted map are reflected in this map, and vice-versa.
1082 <     * The returned sorted map supports all optional map operations.<p>
1079 >     * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned
1080 >     * navigable map is empty.)  The returned navigable map is backed
1081 >     * by this map, so changes in the returned navigable map are
1082 >     * reflected in this map, and vice-versa.  The returned navigable
1083 >     * map supports all optional map operations.<p>
1084       *
1085 <     * The sorted map returned by this method will throw an
1085 >     * The navigable map returned by this method will throw an
1086       * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
1087       * less than <tt>fromKey</tt> or greater than or equal to
1088       * <tt>toKey</tt>.<p>
# Line 1091 | Line 1090 | public class TreeMap<K,V>
1090       * Note: this method always returns a <i>half-open range</i> (which
1091       * includes its low endpoint but not its high endpoint).  If you need a
1092       * <i>closed range</i> (which includes both endpoints), and the key type
1093 <     * allows for calculation of the successor a given key, merely request the
1093 >     * allows for calculation of the successor of a given key, merely request the
1094       * subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>.
1095 <     * For example, suppose that <tt>m</tt> is a sorted map whose keys are
1095 >     * For example, suppose that <tt>m</tt> is a navigable map whose keys are
1096       * strings.  The following idiom obtains a view containing all of the
1097       * key-value mappings in <tt>m</tt> whose keys are between <tt>low</tt>
1098       * and <tt>high</tt>, inclusive:
1099 <     *             <pre>    NavigableMap sub = m.submap(low, high+"\0");</pre>
1099 >     * <pre>  NavigableMap sub = m.navigableSubMap(low, high+"\0");</pre>
1100       * A similar technique can be used to generate an <i>open range</i> (which
1101       * contains neither endpoint).  The following idiom obtains a view
1102       * containing all of the key-value mappings in <tt>m</tt> whose keys are
1103       * between <tt>low</tt> and <tt>high</tt>, exclusive:
1104 <     *             <pre>    NavigableMap sub = m.subMap(low+"\0", high);</pre>
1104 >     * <pre>  NavigableMap sub = m.navigableSubMap(low+"\0", high);</pre>
1105       *
1106       * @param fromKey low endpoint (inclusive) of the subMap.
1107       * @param toKey high endpoint (exclusive) of the subMap.
# Line 1119 | Line 1118 | public class TreeMap<K,V>
1118       *               <tt>null</tt> and this map uses natural order, or its
1119       *               comparator does not tolerate <tt>null</tt> keys.
1120       */
1121 <    public NavigableMap<K,V> subMap(K fromKey, K toKey) {
1121 >    public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) {
1122          return new SubMap(fromKey, toKey);
1123      }
1124  
1125 +
1126      /**
1127       * Returns a view of the portion of this map whose keys are strictly less
1128 <     * than <tt>toKey</tt>.  The returned sorted map is backed by this map, so
1129 <     * changes in the returned sorted map are reflected in this map, and
1130 <     * vice-versa.  The returned sorted map supports all optional map
1128 >     * than <tt>toKey</tt>.  The returned navigable map is backed by this map, so
1129 >     * changes in the returned navigable map are reflected in this map, and
1130 >     * vice-versa.  The returned navigable map supports all optional map
1131       * operations.<p>
1132       *
1133 <     * The sorted map returned by this method will throw an
1133 >     * The navigable map returned by this method will throw an
1134       * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
1135       * greater than or equal to <tt>toKey</tt>.<p>
1136       *
1137       * Note: this method always returns a view that does not contain its
1138       * (high) endpoint.  If you need a view that does contain this endpoint,
1139 <     * and the key type allows for calculation of the successor a given key,
1139 >     * and the key type allows for calculation of the successor of a given key,
1140       * merely request a headMap bounded by <tt>successor(highEndpoint)</tt>.
1141 <     * For example, suppose that suppose that <tt>m</tt> is a sorted map whose
1141 >     * For example, suppose that suppose that <tt>m</tt> is a navigable map whose
1142       * keys are strings.  The following idiom obtains a view containing all of
1143       * the key-value mappings in <tt>m</tt> whose keys are less than or equal
1144       * to <tt>high</tt>:
1145       * <pre>
1146 <     *     NavigableMap head = m.headMap(high+"\0");
1146 >     *     NavigableMap head = m.navigableHeadMap(high+"\0");
1147       * </pre>
1148       *
1149       * @param toKey high endpoint (exclusive) of the headMap.
# Line 1160 | Line 1160 | public class TreeMap<K,V>
1160       *               this map uses natural order, or its comparator does not
1161       *               tolerate <tt>null</tt> keys.
1162       */
1163 <    public NavigableMap<K,V> headMap(K toKey) {
1163 >    public NavigableMap<K,V> navigableHeadMap(K toKey) {
1164          return new SubMap(toKey, true);
1165      }
1166  
1167      /**
1168       * Returns a view of the portion of this map whose keys are greater than
1169 <     * or equal to <tt>fromKey</tt>.  The returned sorted map is backed by
1170 <     * this map, so changes in the returned sorted map are reflected in this
1171 <     * map, and vice-versa.  The returned sorted map supports all optional map
1169 >     * or equal to <tt>fromKey</tt>.  The returned navigable map is backed by
1170 >     * this map, so changes in the returned navigable map are reflected in this
1171 >     * map, and vice-versa.  The returned navigable map supports all optional map
1172       * operations.<p>
1173       *
1174 <     * The sorted map returned by this method will throw an
1174 >     * The navigable map returned by this method will throw an
1175       * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
1176       * less than <tt>fromKey</tt>.<p>
1177       *
1178       * Note: this method always returns a view that contains its (low)
1179       * endpoint.  If you need a view that does not contain this endpoint, and
1180 <     * the element type allows for calculation of the successor a given value,
1180 >     * the element type allows for calculation of the successor of a given value,
1181       * merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>.
1182 <     * For example, suppose that <tt>m</tt> is a sorted map whose keys
1182 >     * For example, suppose that <tt>m</tt> is a navigable map whose keys
1183       * are strings.  The following idiom obtains a view containing
1184       * all of the key-value mappings in <tt>m</tt> whose keys are strictly
1185       * greater than <tt>low</tt>: <pre>
1186 <     *     NavigableMap tail = m.tailMap(low+"\0");
1186 >     *     NavigableMap tail = m.navigableTailMap(low+"\0");
1187       * </pre>
1188       *
1189       * @param fromKey low endpoint (inclusive) of the tailMap.
# Line 1199 | Line 1199 | public class TreeMap<K,V>
1199       *               this map uses natural order, or its comparator does not
1200       *               tolerate <tt>null</tt> keys.
1201       */
1202 <    public NavigableMap<K,V> tailMap(K fromKey) {
1202 >    public NavigableMap<K,V> navigableTailMap(K fromKey) {
1203 >        return new SubMap(fromKey, false);
1204 >    }
1205 >
1206 >    /**
1207 >     * Equivalent to <tt>navigableSubMap</tt> but with a return
1208 >     * type conforming to the <tt>SortedMap</tt> interface.
1209 >     * @param fromKey low endpoint (inclusive) of the subMap.
1210 >     * @param toKey high endpoint (exclusive) of the subMap.
1211 >     *
1212 >     * @return a view of the portion of this map whose keys range from
1213 >     *                <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.
1214 >     *
1215 >     * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
1216 >     *         cannot be compared to one another using this map's comparator
1217 >     *         (or, if the map has no comparator, using natural ordering).
1218 >     * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
1219 >     *         <tt>toKey</tt>.
1220 >     * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
1221 >     *               <tt>null</tt> and this map uses natural order, or its
1222 >     *               comparator does not tolerate <tt>null</tt> keys.
1223 >     */
1224 >    public SortedMap<K,V> subMap(K fromKey, K toKey) {
1225 >        return new SubMap(fromKey, toKey);
1226 >    }
1227 >
1228 >
1229 >    /**
1230 >     * Equivalent to <tt>navigableHeadMap</tt> but with a return
1231 >     * type conforming to the <tt>SortedMap</tt> interface.
1232 >     *
1233 >     * @param toKey high endpoint (exclusive) of the headMap.
1234 >     * @return a view of the portion of this map whose keys are strictly
1235 >     *                less than <tt>toKey</tt>.
1236 >     *
1237 >     * @throws ClassCastException if <tt>toKey</tt> is not compatible
1238 >     *         with this map's comparator (or, if the map has no comparator,
1239 >     *         if <tt>toKey</tt> does not implement <tt>Comparable</tt>).
1240 >     * @throws IllegalArgumentException if this map is itself a subMap,
1241 >     *         headMap, or tailMap, and <tt>toKey</tt> is not within the
1242 >     *         specified range of the subMap, headMap, or tailMap.
1243 >     * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and
1244 >     *               this map uses natural order, or its comparator does not
1245 >     *               tolerate <tt>null</tt> keys.
1246 >     */
1247 >    public SortedMap<K,V> headMap(K toKey) {
1248 >        return new SubMap(toKey, true);
1249 >    }
1250 >
1251 >    /**
1252 >     * Equivalent to <tt>navigableTailMap</tt> but with a return
1253 >     * type conforming to the <tt>SortedMap</tt> interface.
1254 >     *
1255 >     * @param fromKey low endpoint (inclusive) of the tailMap.
1256 >     * @return a view of the portion of this map whose keys are greater
1257 >     *                than or equal to <tt>fromKey</tt>.
1258 >     * @throws ClassCastException if <tt>fromKey</tt> is not compatible
1259 >     *         with this map's comparator (or, if the map has no comparator,
1260 >     *         if <tt>fromKey</tt> does not implement <tt>Comparable</tt>).
1261 >     * @throws IllegalArgumentException if this map is itself a subMap,
1262 >     *         headMap, or tailMap, and <tt>fromKey</tt> is not within the
1263 >     *         specified range of the subMap, headMap, or tailMap.
1264 >     * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and
1265 >     *               this map uses natural order, or its comparator does not
1266 >     *               tolerate <tt>null</tt> keys.
1267 >     */
1268 >    public SortedMap<K,V> tailMap(K fromKey) {
1269          return new SubMap(fromKey, false);
1270      }
1271  
# Line 1497 | Line 1563 | public class TreeMap<K,V>
1563          }
1564  
1565  
1566 <        public NavigableMap<K,V> subMap(K fromKey, K toKey) {
1566 >        public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) {
1567              if (!inRange2(fromKey))
1568                  throw new IllegalArgumentException("fromKey out of range");
1569              if (!inRange2(toKey))
# Line 1505 | Line 1571 | public class TreeMap<K,V>
1571              return new SubMap(fromKey, toKey);
1572          }
1573  
1574 <        public NavigableMap<K,V> headMap(K toKey) {
1574 >        public NavigableMap<K,V> navigableHeadMap(K toKey) {
1575              if (!inRange2(toKey))
1576                  throw new IllegalArgumentException("toKey out of range");
1577              return new SubMap(fromStart, fromKey, false, toKey);
1578          }
1579  
1580 <        public NavigableMap<K,V> tailMap(K fromKey) {
1580 >        public NavigableMap<K,V> navigableTailMap(K fromKey) {
1581              if (!inRange2(fromKey))
1582                  throw new IllegalArgumentException("fromKey out of range");
1583              return new SubMap(false, fromKey, toEnd, toKey);
1584          }
1585  
1586 +
1587 +        public SortedMap<K,V> subMap(K fromKey, K toKey) {
1588 +            return navigableSubMap(fromKey, toKey);
1589 +        }
1590 +
1591 +        public SortedMap<K,V> headMap(K toKey) {
1592 +            return navigableHeadMap(toKey);
1593 +        }
1594 +
1595 +        public SortedMap<K,V> tailMap(K fromKey) {
1596 +            return navigableTailMap(fromKey);
1597 +        }
1598 +
1599          private boolean inRange(K key) {
1600              return (fromStart || compare(key, fromKey) >= 0) &&
1601                     (toEnd     || compare(key, toKey)   <  0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines