jaroslav@597: /* jaroslav@597: * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@597: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@597: * jaroslav@597: * This code is free software; you can redistribute it and/or modify it jaroslav@597: * under the terms of the GNU General Public License version 2 only, as jaroslav@597: * published by the Free Software Foundation. Oracle designates this jaroslav@597: * particular file as subject to the "Classpath" exception as provided jaroslav@597: * by Oracle in the LICENSE file that accompanied this code. jaroslav@597: * jaroslav@597: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@597: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@597: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@597: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@597: * accompanied this code). jaroslav@597: * jaroslav@597: * You should have received a copy of the GNU General Public License version jaroslav@597: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@597: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@597: * jaroslav@597: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@597: * or visit www.oracle.com if you need additional information or have any jaroslav@597: * questions. jaroslav@597: */ jaroslav@597: jaroslav@597: package java.util; jaroslav@597: jaroslav@597: /** jaroslav@597: * A {@link Map} that further provides a total ordering on its keys. jaroslav@597: * The map is ordered according to the {@linkplain Comparable natural jaroslav@597: * ordering} of its keys, or by a {@link Comparator} typically jaroslav@597: * provided at sorted map creation time. This order is reflected when jaroslav@597: * iterating over the sorted map's collection views (returned by the jaroslav@597: * {@code entrySet}, {@code keySet} and {@code values} methods). jaroslav@597: * Several additional operations are provided to take advantage of the jaroslav@597: * ordering. (This interface is the map analogue of {@link SortedSet}.) jaroslav@597: * jaroslav@597: *

All keys inserted into a sorted map must implement the {@code Comparable} jaroslav@597: * interface (or be accepted by the specified comparator). Furthermore, all jaroslav@597: * such keys must be mutually comparable: {@code k1.compareTo(k2)} (or jaroslav@597: * {@code comparator.compare(k1, k2)}) must not throw a jaroslav@597: * {@code ClassCastException} for any keys {@code k1} and {@code k2} in jaroslav@597: * the sorted map. Attempts to violate this restriction will cause the jaroslav@597: * offending method or constructor invocation to throw a jaroslav@597: * {@code ClassCastException}. jaroslav@597: * jaroslav@597: *

Note that the ordering maintained by a sorted map (whether or not an jaroslav@597: * explicit comparator is provided) must be consistent with equals if jaroslav@597: * the sorted map is to correctly implement the {@code Map} interface. (See jaroslav@597: * the {@code Comparable} interface or {@code Comparator} interface for a jaroslav@597: * precise definition of consistent with equals.) This is so because jaroslav@597: * the {@code Map} interface is defined in terms of the {@code equals} jaroslav@597: * operation, but a sorted map performs all key comparisons using its jaroslav@597: * {@code compareTo} (or {@code compare}) method, so two keys that are jaroslav@597: * deemed equal by this method are, from the standpoint of the sorted map, jaroslav@597: * equal. The behavior of a tree map is well-defined even if its jaroslav@597: * ordering is inconsistent with equals; it just fails to obey the general jaroslav@597: * contract of the {@code Map} interface. jaroslav@597: * jaroslav@597: *

All general-purpose sorted map implementation classes should provide four jaroslav@597: * "standard" constructors. It is not possible to enforce this recommendation jaroslav@597: * though as required constructors cannot be specified by interfaces. The jaroslav@597: * expected "standard" constructors for all sorted map implementations are: jaroslav@597: *

    jaroslav@597: *
  1. A void (no arguments) constructor, which creates an empty sorted map jaroslav@597: * sorted according to the natural ordering of its keys.
  2. jaroslav@597: *
  3. A constructor with a single argument of type {@code Comparator}, which jaroslav@597: * creates an empty sorted map sorted according to the specified comparator.
  4. jaroslav@597: *
  5. A constructor with a single argument of type {@code Map}, which creates jaroslav@597: * a new map with the same key-value mappings as its argument, sorted jaroslav@597: * according to the keys' natural ordering.
  6. jaroslav@597: *
  7. A constructor with a single argument of type {@code SortedMap}, which jaroslav@597: * creates a new sorted map with the same key-value mappings and the same jaroslav@597: * ordering as the input sorted map.
  8. jaroslav@597: *
jaroslav@597: * jaroslav@597: *

Note: several methods return submaps with restricted key jaroslav@597: * ranges. Such ranges are half-open, that is, they include their low jaroslav@597: * endpoint but not their high endpoint (where applicable). If you need a jaroslav@597: * closed range (which includes both endpoints), and the key type jaroslav@597: * allows for calculation of the successor of a given key, merely request jaroslav@597: * the subrange from {@code lowEndpoint} to jaroslav@597: * {@code successor(highEndpoint)}. For example, suppose that {@code m} jaroslav@597: * is a map whose keys are strings. The following idiom obtains a view jaroslav@597: * containing all of the key-value mappings in {@code m} whose keys are jaroslav@597: * between {@code low} and {@code high}, inclusive:

jaroslav@597:  *   SortedMap<String, V> sub = m.subMap(low, high+"\0");
jaroslav@597: * jaroslav@597: * A similar technique can be used to generate an open range jaroslav@597: * (which contains neither endpoint). The following idiom obtains a jaroslav@597: * view containing all of the key-value mappings in {@code m} whose keys jaroslav@597: * are between {@code low} and {@code high}, exclusive:
jaroslav@597:  *   SortedMap<String, V> sub = m.subMap(low+"\0", high);
jaroslav@597: * jaroslav@597: *

This interface is a member of the jaroslav@597: * jaroslav@597: * Java Collections Framework. jaroslav@597: * jaroslav@597: * @param the type of keys maintained by this map jaroslav@597: * @param the type of mapped values jaroslav@597: * jaroslav@597: * @author Josh Bloch jaroslav@597: * @see Map jaroslav@597: * @see TreeMap jaroslav@597: * @see SortedSet jaroslav@597: * @see Comparator jaroslav@597: * @see Comparable jaroslav@597: * @see Collection jaroslav@597: * @see ClassCastException jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: jaroslav@597: public interface SortedMap extends Map { jaroslav@597: /** jaroslav@597: * Returns the comparator used to order the keys in this map, or jaroslav@597: * {@code null} if this map uses the {@linkplain Comparable jaroslav@597: * natural ordering} of its keys. jaroslav@597: * jaroslav@597: * @return the comparator used to order the keys in this map, jaroslav@597: * or {@code null} if this map uses the natural ordering jaroslav@597: * of its keys jaroslav@597: */ jaroslav@597: Comparator comparator(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a view of the portion of this map whose keys range from jaroslav@597: * {@code fromKey}, inclusive, to {@code toKey}, exclusive. (If jaroslav@597: * {@code fromKey} and {@code toKey} are equal, the returned map jaroslav@597: * is empty.) The returned map is backed by this map, so changes jaroslav@597: * in the returned map are reflected in this map, and vice-versa. jaroslav@597: * The returned map supports all optional map operations that this jaroslav@597: * map supports. jaroslav@597: * jaroslav@597: *

The returned map will throw an {@code IllegalArgumentException} jaroslav@597: * on an attempt to insert a key outside its range. jaroslav@597: * jaroslav@597: * @param fromKey low endpoint (inclusive) of the keys in the returned map jaroslav@597: * @param toKey high endpoint (exclusive) of the keys in the returned map jaroslav@597: * @return a view of the portion of this map whose keys range from jaroslav@597: * {@code fromKey}, inclusive, to {@code toKey}, exclusive jaroslav@597: * @throws ClassCastException if {@code fromKey} and {@code toKey} jaroslav@597: * cannot be compared to one another using this map's comparator jaroslav@597: * (or, if the map has no comparator, using natural ordering). jaroslav@597: * Implementations may, but are not required to, throw this jaroslav@597: * exception if {@code fromKey} or {@code toKey} jaroslav@597: * cannot be compared to keys currently in the map. jaroslav@597: * @throws NullPointerException if {@code fromKey} or {@code toKey} jaroslav@597: * is null and this map does not permit null keys jaroslav@597: * @throws IllegalArgumentException if {@code fromKey} is greater than jaroslav@597: * {@code toKey}; or if this map itself has a restricted jaroslav@597: * range, and {@code fromKey} or {@code toKey} lies jaroslav@597: * outside the bounds of the range jaroslav@597: */ jaroslav@597: SortedMap subMap(K fromKey, K toKey); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a view of the portion of this map whose keys are jaroslav@597: * strictly less than {@code toKey}. The returned map is backed jaroslav@597: * by this map, so changes in the returned map are reflected in jaroslav@597: * this map, and vice-versa. The returned map supports all jaroslav@597: * optional map operations that this map supports. jaroslav@597: * jaroslav@597: *

The returned map will throw an {@code IllegalArgumentException} jaroslav@597: * on an attempt to insert a key outside its range. jaroslav@597: * jaroslav@597: * @param toKey high endpoint (exclusive) of the keys in the returned map jaroslav@597: * @return a view of the portion of this map whose keys are strictly jaroslav@597: * less than {@code toKey} jaroslav@597: * @throws ClassCastException if {@code toKey} is not compatible jaroslav@597: * with this map's comparator (or, if the map has no comparator, jaroslav@597: * if {@code toKey} does not implement {@link Comparable}). jaroslav@597: * Implementations may, but are not required to, throw this jaroslav@597: * exception if {@code toKey} cannot be compared to keys jaroslav@597: * currently in the map. jaroslav@597: * @throws NullPointerException if {@code toKey} is null and jaroslav@597: * this map does not permit null keys jaroslav@597: * @throws IllegalArgumentException if this map itself has a jaroslav@597: * restricted range, and {@code toKey} lies outside the jaroslav@597: * bounds of the range jaroslav@597: */ jaroslav@597: SortedMap headMap(K toKey); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a view of the portion of this map whose keys are jaroslav@597: * greater than or equal to {@code fromKey}. The returned map is jaroslav@597: * backed by this map, so changes in the returned map are jaroslav@597: * reflected in this map, and vice-versa. The returned map jaroslav@597: * supports all optional map operations that this map supports. jaroslav@597: * jaroslav@597: *

The returned map will throw an {@code IllegalArgumentException} jaroslav@597: * on an attempt to insert a key outside its range. jaroslav@597: * jaroslav@597: * @param fromKey low endpoint (inclusive) of the keys in the returned map jaroslav@597: * @return a view of the portion of this map whose keys are greater jaroslav@597: * than or equal to {@code fromKey} jaroslav@597: * @throws ClassCastException if {@code fromKey} is not compatible jaroslav@597: * with this map's comparator (or, if the map has no comparator, jaroslav@597: * if {@code fromKey} does not implement {@link Comparable}). jaroslav@597: * Implementations may, but are not required to, throw this jaroslav@597: * exception if {@code fromKey} cannot be compared to keys jaroslav@597: * currently in the map. jaroslav@597: * @throws NullPointerException if {@code fromKey} is null and jaroslav@597: * this map does not permit null keys jaroslav@597: * @throws IllegalArgumentException if this map itself has a jaroslav@597: * restricted range, and {@code fromKey} lies outside the jaroslav@597: * bounds of the range jaroslav@597: */ jaroslav@597: SortedMap tailMap(K fromKey); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the first (lowest) key currently in this map. jaroslav@597: * jaroslav@597: * @return the first (lowest) key currently in this map jaroslav@597: * @throws NoSuchElementException if this map is empty jaroslav@597: */ jaroslav@597: K firstKey(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the last (highest) key currently in this map. jaroslav@597: * jaroslav@597: * @return the last (highest) key currently in this map jaroslav@597: * @throws NoSuchElementException if this map is empty jaroslav@597: */ jaroslav@597: K lastKey(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a {@link Set} view of the keys contained in this map. jaroslav@597: * The set's iterator returns the keys in ascending order. jaroslav@597: * The set is backed by the map, so changes to the map are jaroslav@597: * reflected in the set, and vice-versa. If the map is modified jaroslav@597: * while an iteration over the set is in progress (except through jaroslav@597: * the iterator's own {@code remove} operation), the results of jaroslav@597: * the iteration are undefined. The set supports element removal, jaroslav@597: * which removes the corresponding mapping from the map, via the jaroslav@597: * {@code Iterator.remove}, {@code Set.remove}, jaroslav@597: * {@code removeAll}, {@code retainAll}, and {@code clear} jaroslav@597: * operations. It does not support the {@code add} or {@code addAll} jaroslav@597: * operations. jaroslav@597: * jaroslav@597: * @return a set view of the keys contained in this map, sorted in jaroslav@597: * ascending order jaroslav@597: */ jaroslav@597: Set keySet(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a {@link Collection} view of the values contained in this map. jaroslav@597: * The collection's iterator returns the values in ascending order jaroslav@597: * of the corresponding keys. jaroslav@597: * The collection is backed by the map, so changes to the map are jaroslav@597: * reflected in the collection, and vice-versa. If the map is jaroslav@597: * modified while an iteration over the collection is in progress jaroslav@597: * (except through the iterator's own {@code remove} operation), jaroslav@597: * the results of the iteration are undefined. The collection jaroslav@597: * supports element removal, which removes the corresponding jaroslav@597: * mapping from the map, via the {@code Iterator.remove}, jaroslav@597: * {@code Collection.remove}, {@code removeAll}, jaroslav@597: * {@code retainAll} and {@code clear} operations. It does not jaroslav@597: * support the {@code add} or {@code addAll} operations. jaroslav@597: * jaroslav@597: * @return a collection view of the values contained in this map, jaroslav@597: * sorted in ascending key order jaroslav@597: */ jaroslav@597: Collection values(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a {@link Set} view of the mappings contained in this map. jaroslav@597: * The set's iterator returns the entries in ascending key order. jaroslav@597: * The set is backed by the map, so changes to the map are jaroslav@597: * reflected in the set, and vice-versa. If the map is modified jaroslav@597: * while an iteration over the set is in progress (except through jaroslav@597: * the iterator's own {@code remove} operation, or through the jaroslav@597: * {@code setValue} operation on a map entry returned by the jaroslav@597: * iterator) the results of the iteration are undefined. The set jaroslav@597: * supports element removal, which removes the corresponding jaroslav@597: * mapping from the map, via the {@code Iterator.remove}, jaroslav@597: * {@code Set.remove}, {@code removeAll}, {@code retainAll} and jaroslav@597: * {@code clear} operations. It does not support the jaroslav@597: * {@code add} or {@code addAll} operations. jaroslav@597: * jaroslav@597: * @return a set view of the mappings contained in this map, jaroslav@597: * sorted in ascending key order jaroslav@597: */ jaroslav@597: Set> entrySet(); jaroslav@597: }