jaroslav@597
|
1 |
/*
|
jaroslav@597
|
2 |
* Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@597
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@597
|
4 |
*
|
jaroslav@597
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@597
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@597
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@597
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@597
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@597
|
10 |
*
|
jaroslav@597
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@597
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@597
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@597
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@597
|
15 |
* accompanied this code).
|
jaroslav@597
|
16 |
*
|
jaroslav@597
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@597
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@597
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@597
|
20 |
*
|
jaroslav@597
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@597
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@597
|
23 |
* questions.
|
jaroslav@597
|
24 |
*/
|
jaroslav@597
|
25 |
|
jaroslav@597
|
26 |
package java.util;
|
jaroslav@597
|
27 |
|
jaroslav@597
|
28 |
/**
|
jaroslav@597
|
29 |
* A {@link Map} that further provides a <em>total ordering</em> on its keys.
|
jaroslav@597
|
30 |
* The map is ordered according to the {@linkplain Comparable natural
|
jaroslav@597
|
31 |
* ordering} of its keys, or by a {@link Comparator} typically
|
jaroslav@597
|
32 |
* provided at sorted map creation time. This order is reflected when
|
jaroslav@597
|
33 |
* iterating over the sorted map's collection views (returned by the
|
jaroslav@597
|
34 |
* {@code entrySet}, {@code keySet} and {@code values} methods).
|
jaroslav@597
|
35 |
* Several additional operations are provided to take advantage of the
|
jaroslav@597
|
36 |
* ordering. (This interface is the map analogue of {@link SortedSet}.)
|
jaroslav@597
|
37 |
*
|
jaroslav@597
|
38 |
* <p>All keys inserted into a sorted map must implement the {@code Comparable}
|
jaroslav@597
|
39 |
* interface (or be accepted by the specified comparator). Furthermore, all
|
jaroslav@597
|
40 |
* such keys must be <em>mutually comparable</em>: {@code k1.compareTo(k2)} (or
|
jaroslav@597
|
41 |
* {@code comparator.compare(k1, k2)}) must not throw a
|
jaroslav@597
|
42 |
* {@code ClassCastException} for any keys {@code k1} and {@code k2} in
|
jaroslav@597
|
43 |
* the sorted map. Attempts to violate this restriction will cause the
|
jaroslav@597
|
44 |
* offending method or constructor invocation to throw a
|
jaroslav@597
|
45 |
* {@code ClassCastException}.
|
jaroslav@597
|
46 |
*
|
jaroslav@597
|
47 |
* <p>Note that the ordering maintained by a sorted map (whether or not an
|
jaroslav@597
|
48 |
* explicit comparator is provided) must be <em>consistent with equals</em> if
|
jaroslav@597
|
49 |
* the sorted map is to correctly implement the {@code Map} interface. (See
|
jaroslav@597
|
50 |
* the {@code Comparable} interface or {@code Comparator} interface for a
|
jaroslav@597
|
51 |
* precise definition of <em>consistent with equals</em>.) This is so because
|
jaroslav@597
|
52 |
* the {@code Map} interface is defined in terms of the {@code equals}
|
jaroslav@597
|
53 |
* operation, but a sorted map performs all key comparisons using its
|
jaroslav@597
|
54 |
* {@code compareTo} (or {@code compare}) method, so two keys that are
|
jaroslav@597
|
55 |
* deemed equal by this method are, from the standpoint of the sorted map,
|
jaroslav@597
|
56 |
* equal. The behavior of a tree map <em>is</em> well-defined even if its
|
jaroslav@597
|
57 |
* ordering is inconsistent with equals; it just fails to obey the general
|
jaroslav@597
|
58 |
* contract of the {@code Map} interface.
|
jaroslav@597
|
59 |
*
|
jaroslav@597
|
60 |
* <p>All general-purpose sorted map implementation classes should provide four
|
jaroslav@597
|
61 |
* "standard" constructors. It is not possible to enforce this recommendation
|
jaroslav@597
|
62 |
* though as required constructors cannot be specified by interfaces. The
|
jaroslav@597
|
63 |
* expected "standard" constructors for all sorted map implementations are:
|
jaroslav@597
|
64 |
* <ol>
|
jaroslav@597
|
65 |
* <li>A void (no arguments) constructor, which creates an empty sorted map
|
jaroslav@597
|
66 |
* sorted according to the natural ordering of its keys.</li>
|
jaroslav@597
|
67 |
* <li>A constructor with a single argument of type {@code Comparator}, which
|
jaroslav@597
|
68 |
* creates an empty sorted map sorted according to the specified comparator.</li>
|
jaroslav@597
|
69 |
* <li>A constructor with a single argument of type {@code Map}, which creates
|
jaroslav@597
|
70 |
* a new map with the same key-value mappings as its argument, sorted
|
jaroslav@597
|
71 |
* according to the keys' natural ordering.</li>
|
jaroslav@597
|
72 |
* <li>A constructor with a single argument of type {@code SortedMap}, which
|
jaroslav@597
|
73 |
* creates a new sorted map with the same key-value mappings and the same
|
jaroslav@597
|
74 |
* ordering as the input sorted map.</li>
|
jaroslav@597
|
75 |
* </ol>
|
jaroslav@597
|
76 |
*
|
jaroslav@597
|
77 |
* <p><strong>Note</strong>: several methods return submaps with restricted key
|
jaroslav@597
|
78 |
* ranges. Such ranges are <em>half-open</em>, that is, they include their low
|
jaroslav@597
|
79 |
* endpoint but not their high endpoint (where applicable). If you need a
|
jaroslav@597
|
80 |
* <em>closed range</em> (which includes both endpoints), and the key type
|
jaroslav@597
|
81 |
* allows for calculation of the successor of a given key, merely request
|
jaroslav@597
|
82 |
* the subrange from {@code lowEndpoint} to
|
jaroslav@597
|
83 |
* {@code successor(highEndpoint)}. For example, suppose that {@code m}
|
jaroslav@597
|
84 |
* is a map whose keys are strings. The following idiom obtains a view
|
jaroslav@597
|
85 |
* containing all of the key-value mappings in {@code m} whose keys are
|
jaroslav@597
|
86 |
* between {@code low} and {@code high}, inclusive:<pre>
|
jaroslav@597
|
87 |
* SortedMap<String, V> sub = m.subMap(low, high+"\0");</pre>
|
jaroslav@597
|
88 |
*
|
jaroslav@597
|
89 |
* A similar technique can be used to generate an <em>open range</em>
|
jaroslav@597
|
90 |
* (which contains neither endpoint). The following idiom obtains a
|
jaroslav@597
|
91 |
* view containing all of the key-value mappings in {@code m} whose keys
|
jaroslav@597
|
92 |
* are between {@code low} and {@code high}, exclusive:<pre>
|
jaroslav@597
|
93 |
* SortedMap<String, V> sub = m.subMap(low+"\0", high);</pre>
|
jaroslav@597
|
94 |
*
|
jaroslav@597
|
95 |
* <p>This interface is a member of the
|
jaroslav@597
|
96 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
jaroslav@597
|
97 |
* Java Collections Framework</a>.
|
jaroslav@597
|
98 |
*
|
jaroslav@597
|
99 |
* @param <K> the type of keys maintained by this map
|
jaroslav@597
|
100 |
* @param <V> the type of mapped values
|
jaroslav@597
|
101 |
*
|
jaroslav@597
|
102 |
* @author Josh Bloch
|
jaroslav@597
|
103 |
* @see Map
|
jaroslav@597
|
104 |
* @see TreeMap
|
jaroslav@597
|
105 |
* @see SortedSet
|
jaroslav@597
|
106 |
* @see Comparator
|
jaroslav@597
|
107 |
* @see Comparable
|
jaroslav@597
|
108 |
* @see Collection
|
jaroslav@597
|
109 |
* @see ClassCastException
|
jaroslav@597
|
110 |
* @since 1.2
|
jaroslav@597
|
111 |
*/
|
jaroslav@597
|
112 |
|
jaroslav@597
|
113 |
public interface SortedMap<K,V> extends Map<K,V> {
|
jaroslav@597
|
114 |
/**
|
jaroslav@597
|
115 |
* Returns the comparator used to order the keys in this map, or
|
jaroslav@597
|
116 |
* {@code null} if this map uses the {@linkplain Comparable
|
jaroslav@597
|
117 |
* natural ordering} of its keys.
|
jaroslav@597
|
118 |
*
|
jaroslav@597
|
119 |
* @return the comparator used to order the keys in this map,
|
jaroslav@597
|
120 |
* or {@code null} if this map uses the natural ordering
|
jaroslav@597
|
121 |
* of its keys
|
jaroslav@597
|
122 |
*/
|
jaroslav@597
|
123 |
Comparator<? super K> comparator();
|
jaroslav@597
|
124 |
|
jaroslav@597
|
125 |
/**
|
jaroslav@597
|
126 |
* Returns a view of the portion of this map whose keys range from
|
jaroslav@597
|
127 |
* {@code fromKey}, inclusive, to {@code toKey}, exclusive. (If
|
jaroslav@597
|
128 |
* {@code fromKey} and {@code toKey} are equal, the returned map
|
jaroslav@597
|
129 |
* is empty.) The returned map is backed by this map, so changes
|
jaroslav@597
|
130 |
* in the returned map are reflected in this map, and vice-versa.
|
jaroslav@597
|
131 |
* The returned map supports all optional map operations that this
|
jaroslav@597
|
132 |
* map supports.
|
jaroslav@597
|
133 |
*
|
jaroslav@597
|
134 |
* <p>The returned map will throw an {@code IllegalArgumentException}
|
jaroslav@597
|
135 |
* on an attempt to insert a key outside its range.
|
jaroslav@597
|
136 |
*
|
jaroslav@597
|
137 |
* @param fromKey low endpoint (inclusive) of the keys in the returned map
|
jaroslav@597
|
138 |
* @param toKey high endpoint (exclusive) of the keys in the returned map
|
jaroslav@597
|
139 |
* @return a view of the portion of this map whose keys range from
|
jaroslav@597
|
140 |
* {@code fromKey}, inclusive, to {@code toKey}, exclusive
|
jaroslav@597
|
141 |
* @throws ClassCastException if {@code fromKey} and {@code toKey}
|
jaroslav@597
|
142 |
* cannot be compared to one another using this map's comparator
|
jaroslav@597
|
143 |
* (or, if the map has no comparator, using natural ordering).
|
jaroslav@597
|
144 |
* Implementations may, but are not required to, throw this
|
jaroslav@597
|
145 |
* exception if {@code fromKey} or {@code toKey}
|
jaroslav@597
|
146 |
* cannot be compared to keys currently in the map.
|
jaroslav@597
|
147 |
* @throws NullPointerException if {@code fromKey} or {@code toKey}
|
jaroslav@597
|
148 |
* is null and this map does not permit null keys
|
jaroslav@597
|
149 |
* @throws IllegalArgumentException if {@code fromKey} is greater than
|
jaroslav@597
|
150 |
* {@code toKey}; or if this map itself has a restricted
|
jaroslav@597
|
151 |
* range, and {@code fromKey} or {@code toKey} lies
|
jaroslav@597
|
152 |
* outside the bounds of the range
|
jaroslav@597
|
153 |
*/
|
jaroslav@597
|
154 |
SortedMap<K,V> subMap(K fromKey, K toKey);
|
jaroslav@597
|
155 |
|
jaroslav@597
|
156 |
/**
|
jaroslav@597
|
157 |
* Returns a view of the portion of this map whose keys are
|
jaroslav@597
|
158 |
* strictly less than {@code toKey}. The returned map is backed
|
jaroslav@597
|
159 |
* by this map, so changes in the returned map are reflected in
|
jaroslav@597
|
160 |
* this map, and vice-versa. The returned map supports all
|
jaroslav@597
|
161 |
* optional map operations that this map supports.
|
jaroslav@597
|
162 |
*
|
jaroslav@597
|
163 |
* <p>The returned map will throw an {@code IllegalArgumentException}
|
jaroslav@597
|
164 |
* on an attempt to insert a key outside its range.
|
jaroslav@597
|
165 |
*
|
jaroslav@597
|
166 |
* @param toKey high endpoint (exclusive) of the keys in the returned map
|
jaroslav@597
|
167 |
* @return a view of the portion of this map whose keys are strictly
|
jaroslav@597
|
168 |
* less than {@code toKey}
|
jaroslav@597
|
169 |
* @throws ClassCastException if {@code toKey} is not compatible
|
jaroslav@597
|
170 |
* with this map's comparator (or, if the map has no comparator,
|
jaroslav@597
|
171 |
* if {@code toKey} does not implement {@link Comparable}).
|
jaroslav@597
|
172 |
* Implementations may, but are not required to, throw this
|
jaroslav@597
|
173 |
* exception if {@code toKey} cannot be compared to keys
|
jaroslav@597
|
174 |
* currently in the map.
|
jaroslav@597
|
175 |
* @throws NullPointerException if {@code toKey} is null and
|
jaroslav@597
|
176 |
* this map does not permit null keys
|
jaroslav@597
|
177 |
* @throws IllegalArgumentException if this map itself has a
|
jaroslav@597
|
178 |
* restricted range, and {@code toKey} lies outside the
|
jaroslav@597
|
179 |
* bounds of the range
|
jaroslav@597
|
180 |
*/
|
jaroslav@597
|
181 |
SortedMap<K,V> headMap(K toKey);
|
jaroslav@597
|
182 |
|
jaroslav@597
|
183 |
/**
|
jaroslav@597
|
184 |
* Returns a view of the portion of this map whose keys are
|
jaroslav@597
|
185 |
* greater than or equal to {@code fromKey}. The returned map is
|
jaroslav@597
|
186 |
* backed by this map, so changes in the returned map are
|
jaroslav@597
|
187 |
* reflected in this map, and vice-versa. The returned map
|
jaroslav@597
|
188 |
* supports all optional map operations that this map supports.
|
jaroslav@597
|
189 |
*
|
jaroslav@597
|
190 |
* <p>The returned map will throw an {@code IllegalArgumentException}
|
jaroslav@597
|
191 |
* on an attempt to insert a key outside its range.
|
jaroslav@597
|
192 |
*
|
jaroslav@597
|
193 |
* @param fromKey low endpoint (inclusive) of the keys in the returned map
|
jaroslav@597
|
194 |
* @return a view of the portion of this map whose keys are greater
|
jaroslav@597
|
195 |
* than or equal to {@code fromKey}
|
jaroslav@597
|
196 |
* @throws ClassCastException if {@code fromKey} is not compatible
|
jaroslav@597
|
197 |
* with this map's comparator (or, if the map has no comparator,
|
jaroslav@597
|
198 |
* if {@code fromKey} does not implement {@link Comparable}).
|
jaroslav@597
|
199 |
* Implementations may, but are not required to, throw this
|
jaroslav@597
|
200 |
* exception if {@code fromKey} cannot be compared to keys
|
jaroslav@597
|
201 |
* currently in the map.
|
jaroslav@597
|
202 |
* @throws NullPointerException if {@code fromKey} is null and
|
jaroslav@597
|
203 |
* this map does not permit null keys
|
jaroslav@597
|
204 |
* @throws IllegalArgumentException if this map itself has a
|
jaroslav@597
|
205 |
* restricted range, and {@code fromKey} lies outside the
|
jaroslav@597
|
206 |
* bounds of the range
|
jaroslav@597
|
207 |
*/
|
jaroslav@597
|
208 |
SortedMap<K,V> tailMap(K fromKey);
|
jaroslav@597
|
209 |
|
jaroslav@597
|
210 |
/**
|
jaroslav@597
|
211 |
* Returns the first (lowest) key currently in this map.
|
jaroslav@597
|
212 |
*
|
jaroslav@597
|
213 |
* @return the first (lowest) key currently in this map
|
jaroslav@597
|
214 |
* @throws NoSuchElementException if this map is empty
|
jaroslav@597
|
215 |
*/
|
jaroslav@597
|
216 |
K firstKey();
|
jaroslav@597
|
217 |
|
jaroslav@597
|
218 |
/**
|
jaroslav@597
|
219 |
* Returns the last (highest) key currently in this map.
|
jaroslav@597
|
220 |
*
|
jaroslav@597
|
221 |
* @return the last (highest) key currently in this map
|
jaroslav@597
|
222 |
* @throws NoSuchElementException if this map is empty
|
jaroslav@597
|
223 |
*/
|
jaroslav@597
|
224 |
K lastKey();
|
jaroslav@597
|
225 |
|
jaroslav@597
|
226 |
/**
|
jaroslav@597
|
227 |
* Returns a {@link Set} view of the keys contained in this map.
|
jaroslav@597
|
228 |
* The set's iterator returns the keys in ascending order.
|
jaroslav@597
|
229 |
* The set is backed by the map, so changes to the map are
|
jaroslav@597
|
230 |
* reflected in the set, and vice-versa. If the map is modified
|
jaroslav@597
|
231 |
* while an iteration over the set is in progress (except through
|
jaroslav@597
|
232 |
* the iterator's own {@code remove} operation), the results of
|
jaroslav@597
|
233 |
* the iteration are undefined. The set supports element removal,
|
jaroslav@597
|
234 |
* which removes the corresponding mapping from the map, via the
|
jaroslav@597
|
235 |
* {@code Iterator.remove}, {@code Set.remove},
|
jaroslav@597
|
236 |
* {@code removeAll}, {@code retainAll}, and {@code clear}
|
jaroslav@597
|
237 |
* operations. It does not support the {@code add} or {@code addAll}
|
jaroslav@597
|
238 |
* operations.
|
jaroslav@597
|
239 |
*
|
jaroslav@597
|
240 |
* @return a set view of the keys contained in this map, sorted in
|
jaroslav@597
|
241 |
* ascending order
|
jaroslav@597
|
242 |
*/
|
jaroslav@597
|
243 |
Set<K> keySet();
|
jaroslav@597
|
244 |
|
jaroslav@597
|
245 |
/**
|
jaroslav@597
|
246 |
* Returns a {@link Collection} view of the values contained in this map.
|
jaroslav@597
|
247 |
* The collection's iterator returns the values in ascending order
|
jaroslav@597
|
248 |
* of the corresponding keys.
|
jaroslav@597
|
249 |
* The collection is backed by the map, so changes to the map are
|
jaroslav@597
|
250 |
* reflected in the collection, and vice-versa. If the map is
|
jaroslav@597
|
251 |
* modified while an iteration over the collection is in progress
|
jaroslav@597
|
252 |
* (except through the iterator's own {@code remove} operation),
|
jaroslav@597
|
253 |
* the results of the iteration are undefined. The collection
|
jaroslav@597
|
254 |
* supports element removal, which removes the corresponding
|
jaroslav@597
|
255 |
* mapping from the map, via the {@code Iterator.remove},
|
jaroslav@597
|
256 |
* {@code Collection.remove}, {@code removeAll},
|
jaroslav@597
|
257 |
* {@code retainAll} and {@code clear} operations. It does not
|
jaroslav@597
|
258 |
* support the {@code add} or {@code addAll} operations.
|
jaroslav@597
|
259 |
*
|
jaroslav@597
|
260 |
* @return a collection view of the values contained in this map,
|
jaroslav@597
|
261 |
* sorted in ascending key order
|
jaroslav@597
|
262 |
*/
|
jaroslav@597
|
263 |
Collection<V> values();
|
jaroslav@597
|
264 |
|
jaroslav@597
|
265 |
/**
|
jaroslav@597
|
266 |
* Returns a {@link Set} view of the mappings contained in this map.
|
jaroslav@597
|
267 |
* The set's iterator returns the entries in ascending key order.
|
jaroslav@597
|
268 |
* The set is backed by the map, so changes to the map are
|
jaroslav@597
|
269 |
* reflected in the set, and vice-versa. If the map is modified
|
jaroslav@597
|
270 |
* while an iteration over the set is in progress (except through
|
jaroslav@597
|
271 |
* the iterator's own {@code remove} operation, or through the
|
jaroslav@597
|
272 |
* {@code setValue} operation on a map entry returned by the
|
jaroslav@597
|
273 |
* iterator) the results of the iteration are undefined. The set
|
jaroslav@597
|
274 |
* supports element removal, which removes the corresponding
|
jaroslav@597
|
275 |
* mapping from the map, via the {@code Iterator.remove},
|
jaroslav@597
|
276 |
* {@code Set.remove}, {@code removeAll}, {@code retainAll} and
|
jaroslav@597
|
277 |
* {@code clear} operations. It does not support the
|
jaroslav@597
|
278 |
* {@code add} or {@code addAll} operations.
|
jaroslav@597
|
279 |
*
|
jaroslav@597
|
280 |
* @return a set view of the mappings contained in this map,
|
jaroslav@597
|
281 |
* sorted in ascending key order
|
jaroslav@597
|
282 |
*/
|
jaroslav@597
|
283 |
Set<Map.Entry<K, V>> entrySet();
|
jaroslav@597
|
284 |
}
|