NavigableMap.java revision be0e537d8953365ae5a008350461ccf777a719fb
1/* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25/* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * Written by Doug Lea and Josh Bloch with assistance from members of JCP 31 * JSR-166 Expert Group and released to the public domain, as explained at 32 * http://creativecommons.org/publicdomain/zero/1.0/ 33 */ 34 35package java.util; 36 37// BEGIN android-note 38// removed link to collections framework docs 39// END android-note 40 41/** 42 * A {@link SortedMap} extended with navigation methods returning the 43 * closest matches for given search targets. Methods 44 * {@code lowerEntry}, {@code floorEntry}, {@code ceilingEntry}, 45 * and {@code higherEntry} return {@code Map.Entry} objects 46 * associated with keys respectively less than, less than or equal, 47 * greater than or equal, and greater than a given key, returning 48 * {@code null} if there is no such key. Similarly, methods 49 * {@code lowerKey}, {@code floorKey}, {@code ceilingKey}, and 50 * {@code higherKey} return only the associated keys. All of these 51 * methods are designed for locating, not traversing entries. 52 * 53 * <p>A {@code NavigableMap} may be accessed and traversed in either 54 * ascending or descending key order. The {@code descendingMap} 55 * method returns a view of the map with the senses of all relational 56 * and directional methods inverted. The performance of ascending 57 * operations and views is likely to be faster than that of descending 58 * ones. Methods {@code subMap}, {@code headMap}, 59 * and {@code tailMap} differ from the like-named {@code 60 * SortedMap} methods in accepting additional arguments describing 61 * whether lower and upper bounds are inclusive versus exclusive. 62 * Submaps of any {@code NavigableMap} must implement the {@code 63 * NavigableMap} interface. 64 * 65 * <p>This interface additionally defines methods {@code firstEntry}, 66 * {@code pollFirstEntry}, {@code lastEntry}, and 67 * {@code pollLastEntry} that return and/or remove the least and 68 * greatest mappings, if any exist, else returning {@code null}. 69 * 70 * <p>Implementations of entry-returning methods are expected to 71 * return {@code Map.Entry} pairs representing snapshots of mappings 72 * at the time they were produced, and thus generally do <em>not</em> 73 * support the optional {@code Entry.setValue} method. Note however 74 * that it is possible to change mappings in the associated map using 75 * method {@code put}. 76 * 77 * <p>Methods 78 * {@link #subMap(Object, Object) subMap(K, K)}, 79 * {@link #headMap(Object) headMap(K)}, and 80 * {@link #tailMap(Object) tailMap(K)} 81 * are specified to return {@code SortedMap} to allow existing 82 * implementations of {@code SortedMap} to be compatibly retrofitted to 83 * implement {@code NavigableMap}, but extensions and implementations 84 * of this interface are encouraged to override these methods to return 85 * {@code NavigableMap}. Similarly, 86 * {@link #keySet()} can be overridden to return {@code NavigableSet}. 87 * 88 * @author Doug Lea 89 * @author Josh Bloch 90 * @param <K> the type of keys maintained by this map 91 * @param <V> the type of mapped values 92 * @since 1.6 93 */ 94public interface NavigableMap<K,V> extends SortedMap<K,V> { 95 /** 96 * Returns a key-value mapping associated with the greatest key 97 * strictly less than the given key, or {@code null} if there is 98 * no such key. 99 * 100 * @param key the key 101 * @return an entry with the greatest key less than {@code key}, 102 * or {@code null} if there is no such key 103 * @throws ClassCastException if the specified key cannot be compared 104 * with the keys currently in the map 105 * @throws NullPointerException if the specified key is null 106 * and this map does not permit null keys 107 */ 108 Map.Entry<K,V> lowerEntry(K key); 109 110 /** 111 * Returns the greatest key strictly less than the given key, or 112 * {@code null} if there is no such key. 113 * 114 * @param key the key 115 * @return the greatest key less than {@code key}, 116 * or {@code null} if there is no such key 117 * @throws ClassCastException if the specified key cannot be compared 118 * with the keys currently in the map 119 * @throws NullPointerException if the specified key is null 120 * and this map does not permit null keys 121 */ 122 K lowerKey(K key); 123 124 /** 125 * Returns a key-value mapping associated with the greatest key 126 * less than or equal to the given key, or {@code null} if there 127 * is no such key. 128 * 129 * @param key the key 130 * @return an entry with the greatest key less than or equal to 131 * {@code key}, or {@code null} if there is no such key 132 * @throws ClassCastException if the specified key cannot be compared 133 * with the keys currently in the map 134 * @throws NullPointerException if the specified key is null 135 * and this map does not permit null keys 136 */ 137 Map.Entry<K,V> floorEntry(K key); 138 139 /** 140 * Returns the greatest key less than or equal to the given key, 141 * or {@code null} if there is no such key. 142 * 143 * @param key the key 144 * @return the greatest key less than or equal to {@code key}, 145 * or {@code null} if there is no such key 146 * @throws ClassCastException if the specified key cannot be compared 147 * with the keys currently in the map 148 * @throws NullPointerException if the specified key is null 149 * and this map does not permit null keys 150 */ 151 K floorKey(K key); 152 153 /** 154 * Returns a key-value mapping associated with the least key 155 * greater than or equal to the given key, or {@code null} if 156 * there is no such key. 157 * 158 * @param key the key 159 * @return an entry with the least key greater than or equal to 160 * {@code key}, or {@code null} if there is no such key 161 * @throws ClassCastException if the specified key cannot be compared 162 * with the keys currently in the map 163 * @throws NullPointerException if the specified key is null 164 * and this map does not permit null keys 165 */ 166 Map.Entry<K,V> ceilingEntry(K key); 167 168 /** 169 * Returns the least key greater than or equal to the given key, 170 * or {@code null} if there is no such key. 171 * 172 * @param key the key 173 * @return the least key greater than or equal to {@code key}, 174 * or {@code null} if there is no such key 175 * @throws ClassCastException if the specified key cannot be compared 176 * with the keys currently in the map 177 * @throws NullPointerException if the specified key is null 178 * and this map does not permit null keys 179 */ 180 K ceilingKey(K key); 181 182 /** 183 * Returns a key-value mapping associated with the least key 184 * strictly greater than the given key, or {@code null} if there 185 * is no such key. 186 * 187 * @param key the key 188 * @return an entry with the least key greater than {@code key}, 189 * or {@code null} if there is no such key 190 * @throws ClassCastException if the specified key cannot be compared 191 * with the keys currently in the map 192 * @throws NullPointerException if the specified key is null 193 * and this map does not permit null keys 194 */ 195 Map.Entry<K,V> higherEntry(K key); 196 197 /** 198 * Returns the least key strictly greater than the given key, or 199 * {@code null} if there is no such key. 200 * 201 * @param key the key 202 * @return the least key greater than {@code key}, 203 * or {@code null} if there is no such key 204 * @throws ClassCastException if the specified key cannot be compared 205 * with the keys currently in the map 206 * @throws NullPointerException if the specified key is null 207 * and this map does not permit null keys 208 */ 209 K higherKey(K key); 210 211 /** 212 * Returns a key-value mapping associated with the least 213 * key in this map, or {@code null} if the map is empty. 214 * 215 * @return an entry with the least key, 216 * or {@code null} if this map is empty 217 */ 218 Map.Entry<K,V> firstEntry(); 219 220 /** 221 * Returns a key-value mapping associated with the greatest 222 * key in this map, or {@code null} if the map is empty. 223 * 224 * @return an entry with the greatest key, 225 * or {@code null} if this map is empty 226 */ 227 Map.Entry<K,V> lastEntry(); 228 229 /** 230 * Removes and returns a key-value mapping associated with 231 * the least key in this map, or {@code null} if the map is empty. 232 * 233 * @return the removed first entry of this map, 234 * or {@code null} if this map is empty 235 */ 236 Map.Entry<K,V> pollFirstEntry(); 237 238 /** 239 * Removes and returns a key-value mapping associated with 240 * the greatest key in this map, or {@code null} if the map is empty. 241 * 242 * @return the removed last entry of this map, 243 * or {@code null} if this map is empty 244 */ 245 Map.Entry<K,V> pollLastEntry(); 246 247 /** 248 * Returns a reverse order view of the mappings contained in this map. 249 * The descending map is backed by this map, so changes to the map are 250 * reflected in the descending map, and vice-versa. If either map is 251 * modified while an iteration over a collection view of either map 252 * is in progress (except through the iterator's own {@code remove} 253 * operation), the results of the iteration are undefined. 254 * 255 * <p>The returned map has an ordering equivalent to 256 * {@link Collections#reverseOrder(Comparator) Collections.reverseOrder}{@code (comparator())}. 257 * The expression {@code m.descendingMap().descendingMap()} returns a 258 * view of {@code m} essentially equivalent to {@code m}. 259 * 260 * @return a reverse order view of this map 261 */ 262 NavigableMap<K,V> descendingMap(); 263 264 /** 265 * Returns a {@link NavigableSet} view of the keys contained in this map. 266 * The set's iterator returns the keys in ascending order. 267 * The set is backed by the map, so changes to the map are reflected in 268 * the set, and vice-versa. If the map is modified while an iteration 269 * over the set is in progress (except through the iterator's own {@code 270 * remove} operation), the results of the iteration are undefined. The 271 * set supports element removal, which removes the corresponding mapping 272 * from the map, via the {@code Iterator.remove}, {@code Set.remove}, 273 * {@code removeAll}, {@code retainAll}, and {@code clear} operations. 274 * It does not support the {@code add} or {@code addAll} operations. 275 * 276 * @return a navigable set view of the keys in this map 277 */ 278 NavigableSet<K> navigableKeySet(); 279 280 /** 281 * Returns a reverse order {@link NavigableSet} view of the keys contained in this map. 282 * The set's iterator returns the keys in descending order. 283 * The set is backed by the map, so changes to the map are reflected in 284 * the set, and vice-versa. If the map is modified while an iteration 285 * over the set is in progress (except through the iterator's own {@code 286 * remove} operation), the results of the iteration are undefined. The 287 * set supports element removal, which removes the corresponding mapping 288 * from the map, via the {@code Iterator.remove}, {@code Set.remove}, 289 * {@code removeAll}, {@code retainAll}, and {@code clear} operations. 290 * It does not support the {@code add} or {@code addAll} operations. 291 * 292 * @return a reverse order navigable set view of the keys in this map 293 */ 294 NavigableSet<K> descendingKeySet(); 295 296 /** 297 * Returns a view of the portion of this map whose keys range from 298 * {@code fromKey} to {@code toKey}. If {@code fromKey} and 299 * {@code toKey} are equal, the returned map is empty unless 300 * {@code fromExclusive} and {@code toExclusive} are both true. The 301 * returned map is backed by this map, so changes in the returned map are 302 * reflected in this map, and vice-versa. The returned map supports all 303 * optional map operations that this map supports. 304 * 305 * <p>The returned map will throw an {@code IllegalArgumentException} 306 * on an attempt to insert a key outside of its range, or to construct a 307 * submap either of whose endpoints lie outside its range. 308 * 309 * @param fromKey low endpoint of the keys in the returned map 310 * @param fromInclusive {@code true} if the low endpoint 311 * is to be included in the returned view 312 * @param toKey high endpoint of the keys in the returned map 313 * @param toInclusive {@code true} if the high endpoint 314 * is to be included in the returned view 315 * @return a view of the portion of this map whose keys range from 316 * {@code fromKey} to {@code toKey} 317 * @throws ClassCastException if {@code fromKey} and {@code toKey} 318 * cannot be compared to one another using this map's comparator 319 * (or, if the map has no comparator, using natural ordering). 320 * Implementations may, but are not required to, throw this 321 * exception if {@code fromKey} or {@code toKey} 322 * cannot be compared to keys currently in the map. 323 * @throws NullPointerException if {@code fromKey} or {@code toKey} 324 * is null and this map does not permit null keys 325 * @throws IllegalArgumentException if {@code fromKey} is greater than 326 * {@code toKey}; or if this map itself has a restricted 327 * range, and {@code fromKey} or {@code toKey} lies 328 * outside the bounds of the range 329 */ 330 NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, 331 K toKey, boolean toInclusive); 332 333 /** 334 * Returns a view of the portion of this map whose keys are less than (or 335 * equal to, if {@code inclusive} is true) {@code toKey}. The returned 336 * map is backed by this map, so changes in the returned map are reflected 337 * in this map, and vice-versa. The returned map supports all optional 338 * map operations that this map supports. 339 * 340 * <p>The returned map will throw an {@code IllegalArgumentException} 341 * on an attempt to insert a key outside its range. 342 * 343 * @param toKey high endpoint of the keys in the returned map 344 * @param inclusive {@code true} if the high endpoint 345 * is to be included in the returned view 346 * @return a view of the portion of this map whose keys are less than 347 * (or equal to, if {@code inclusive} is true) {@code toKey} 348 * @throws ClassCastException if {@code toKey} is not compatible 349 * with this map's comparator (or, if the map has no comparator, 350 * if {@code toKey} does not implement {@link Comparable}). 351 * Implementations may, but are not required to, throw this 352 * exception if {@code toKey} cannot be compared to keys 353 * currently in the map. 354 * @throws NullPointerException if {@code toKey} is null 355 * and this map does not permit null keys 356 * @throws IllegalArgumentException if this map itself has a 357 * restricted range, and {@code toKey} lies outside the 358 * bounds of the range 359 */ 360 NavigableMap<K,V> headMap(K toKey, boolean inclusive); 361 362 /** 363 * Returns a view of the portion of this map whose keys are greater than (or 364 * equal to, if {@code inclusive} is true) {@code fromKey}. The returned 365 * map is backed by this map, so changes in the returned map are reflected 366 * in this map, and vice-versa. The returned map supports all optional 367 * map operations that this map supports. 368 * 369 * <p>The returned map will throw an {@code IllegalArgumentException} 370 * on an attempt to insert a key outside its range. 371 * 372 * @param fromKey low endpoint of the keys in the returned map 373 * @param inclusive {@code true} if the low endpoint 374 * is to be included in the returned view 375 * @return a view of the portion of this map whose keys are greater than 376 * (or equal to, if {@code inclusive} is true) {@code fromKey} 377 * @throws ClassCastException if {@code fromKey} is not compatible 378 * with this map's comparator (or, if the map has no comparator, 379 * if {@code fromKey} does not implement {@link Comparable}). 380 * Implementations may, but are not required to, throw this 381 * exception if {@code fromKey} cannot be compared to keys 382 * currently in the map. 383 * @throws NullPointerException if {@code fromKey} is null 384 * and this map does not permit null keys 385 * @throws IllegalArgumentException if this map itself has a 386 * restricted range, and {@code fromKey} lies outside the 387 * bounds of the range 388 */ 389 NavigableMap<K,V> tailMap(K fromKey, boolean inclusive); 390 391 /** 392 * {@inheritDoc} 393 * 394 * <p>Equivalent to {@code subMap(fromKey, true, toKey, false)}. 395 * 396 * @throws ClassCastException {@inheritDoc} 397 * @throws NullPointerException {@inheritDoc} 398 * @throws IllegalArgumentException {@inheritDoc} 399 */ 400 SortedMap<K,V> subMap(K fromKey, K toKey); 401 402 /** 403 * {@inheritDoc} 404 * 405 * <p>Equivalent to {@code headMap(toKey, false)}. 406 * 407 * @throws ClassCastException {@inheritDoc} 408 * @throws NullPointerException {@inheritDoc} 409 * @throws IllegalArgumentException {@inheritDoc} 410 */ 411 SortedMap<K,V> headMap(K toKey); 412 413 /** 414 * {@inheritDoc} 415 * 416 * <p>Equivalent to {@code tailMap(fromKey, true)}. 417 * 418 * @throws ClassCastException {@inheritDoc} 419 * @throws NullPointerException {@inheritDoc} 420 * @throws IllegalArgumentException {@inheritDoc} 421 */ 422 SortedMap<K,V> tailMap(K fromKey); 423} 424