author | mchung |
Fri, 22 May 2015 16:43:39 -0700 | |
changeset 30789 | 9eca83469588 |
parent 25859 | 3317bb8137f4 |
child 32108 | aa5490a167ee |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
||
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
28 |
import java.util.function.UnaryOperator; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
29 |
|
2 | 30 |
/** |
31 |
* An ordered collection (also known as a <i>sequence</i>). The user of this |
|
32 |
* interface has precise control over where in the list each element is |
|
33 |
* inserted. The user can access elements by their integer index (position in |
|
34 |
* the list), and search for elements in the list.<p> |
|
35 |
* |
|
36 |
* Unlike sets, lists typically allow duplicate elements. More formally, |
|
37 |
* lists typically allow pairs of elements <tt>e1</tt> and <tt>e2</tt> |
|
38 |
* such that <tt>e1.equals(e2)</tt>, and they typically allow multiple |
|
39 |
* null elements if they allow null elements at all. It is not inconceivable |
|
40 |
* that someone might wish to implement a list that prohibits duplicates, by |
|
41 |
* throwing runtime exceptions when the user attempts to insert them, but we |
|
42 |
* expect this usage to be rare.<p> |
|
43 |
* |
|
44 |
* The <tt>List</tt> interface places additional stipulations, beyond those |
|
45 |
* specified in the <tt>Collection</tt> interface, on the contracts of the |
|
46 |
* <tt>iterator</tt>, <tt>add</tt>, <tt>remove</tt>, <tt>equals</tt>, and |
|
47 |
* <tt>hashCode</tt> methods. Declarations for other inherited methods are |
|
48 |
* also included here for convenience.<p> |
|
49 |
* |
|
50 |
* The <tt>List</tt> interface provides four methods for positional (indexed) |
|
51 |
* access to list elements. Lists (like Java arrays) are zero based. Note |
|
52 |
* that these operations may execute in time proportional to the index value |
|
53 |
* for some implementations (the <tt>LinkedList</tt> class, for |
|
54 |
* example). Thus, iterating over the elements in a list is typically |
|
55 |
* preferable to indexing through it if the caller does not know the |
|
56 |
* implementation.<p> |
|
57 |
* |
|
58 |
* The <tt>List</tt> interface provides a special iterator, called a |
|
59 |
* <tt>ListIterator</tt>, that allows element insertion and replacement, and |
|
60 |
* bidirectional access in addition to the normal operations that the |
|
61 |
* <tt>Iterator</tt> interface provides. A method is provided to obtain a |
|
62 |
* list iterator that starts at a specified position in the list.<p> |
|
63 |
* |
|
64 |
* The <tt>List</tt> interface provides two methods to search for a specified |
|
65 |
* object. From a performance standpoint, these methods should be used with |
|
66 |
* caution. In many implementations they will perform costly linear |
|
67 |
* searches.<p> |
|
68 |
* |
|
69 |
* The <tt>List</tt> interface provides two methods to efficiently insert and |
|
70 |
* remove multiple elements at an arbitrary point in the list.<p> |
|
71 |
* |
|
72 |
* Note: While it is permissible for lists to contain themselves as elements, |
|
73 |
* extreme caution is advised: the <tt>equals</tt> and <tt>hashCode</tt> |
|
74 |
* methods are no longer well defined on such a list. |
|
75 |
* |
|
76 |
* <p>Some list implementations have restrictions on the elements that |
|
77 |
* they may contain. For example, some implementations prohibit null elements, |
|
78 |
* and some have restrictions on the types of their elements. Attempting to |
|
79 |
* add an ineligible element throws an unchecked exception, typically |
|
80 |
* <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting |
|
81 |
* to query the presence of an ineligible element may throw an exception, |
|
82 |
* or it may simply return false; some implementations will exhibit the former |
|
83 |
* behavior and some will exhibit the latter. More generally, attempting an |
|
84 |
* operation on an ineligible element whose completion would not result in |
|
85 |
* the insertion of an ineligible element into the list may throw an |
|
86 |
* exception or it may succeed, at the option of the implementation. |
|
87 |
* Such exceptions are marked as "optional" in the specification for this |
|
88 |
* interface. |
|
89 |
* |
|
90 |
* <p>This interface is a member of the |
|
91 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
92 |
* Java Collections Framework</a>. |
|
93 |
* |
|
4977
81902a400bbf
6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents:
2
diff
changeset
|
94 |
* @param <E> the type of elements in this list |
81902a400bbf
6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents:
2
diff
changeset
|
95 |
* |
2 | 96 |
* @author Josh Bloch |
97 |
* @author Neal Gafter |
|
98 |
* @see Collection |
|
99 |
* @see Set |
|
100 |
* @see ArrayList |
|
101 |
* @see LinkedList |
|
102 |
* @see Vector |
|
103 |
* @see Arrays#asList(Object[]) |
|
104 |
* @see Collections#nCopies(int, Object) |
|
105 |
* @see Collections#EMPTY_LIST |
|
106 |
* @see AbstractList |
|
107 |
* @see AbstractSequentialList |
|
108 |
* @since 1.2 |
|
109 |
*/ |
|
110 |
||
111 |
public interface List<E> extends Collection<E> { |
|
112 |
// Query Operations |
|
113 |
||
114 |
/** |
|
115 |
* Returns the number of elements in this list. If this list contains |
|
116 |
* more than <tt>Integer.MAX_VALUE</tt> elements, returns |
|
117 |
* <tt>Integer.MAX_VALUE</tt>. |
|
118 |
* |
|
119 |
* @return the number of elements in this list |
|
120 |
*/ |
|
121 |
int size(); |
|
122 |
||
123 |
/** |
|
124 |
* Returns <tt>true</tt> if this list contains no elements. |
|
125 |
* |
|
126 |
* @return <tt>true</tt> if this list contains no elements |
|
127 |
*/ |
|
128 |
boolean isEmpty(); |
|
129 |
||
130 |
/** |
|
131 |
* Returns <tt>true</tt> if this list contains the specified element. |
|
132 |
* More formally, returns <tt>true</tt> if and only if this list contains |
|
133 |
* at least one element <tt>e</tt> such that |
|
134 |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
|
135 |
* |
|
136 |
* @param o element whose presence in this list is to be tested |
|
137 |
* @return <tt>true</tt> if this list contains the specified element |
|
138 |
* @throws ClassCastException if the type of the specified element |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
139 |
* is incompatible with this list |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
140 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 141 |
* @throws NullPointerException if the specified element is null and this |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
142 |
* list does not permit null elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
143 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 144 |
*/ |
145 |
boolean contains(Object o); |
|
146 |
||
147 |
/** |
|
148 |
* Returns an iterator over the elements in this list in proper sequence. |
|
149 |
* |
|
150 |
* @return an iterator over the elements in this list in proper sequence |
|
151 |
*/ |
|
152 |
Iterator<E> iterator(); |
|
153 |
||
154 |
/** |
|
155 |
* Returns an array containing all of the elements in this list in proper |
|
156 |
* sequence (from first to last element). |
|
157 |
* |
|
158 |
* <p>The returned array will be "safe" in that no references to it are |
|
159 |
* maintained by this list. (In other words, this method must |
|
160 |
* allocate a new array even if this list is backed by an array). |
|
161 |
* The caller is thus free to modify the returned array. |
|
162 |
* |
|
163 |
* <p>This method acts as bridge between array-based and collection-based |
|
164 |
* APIs. |
|
165 |
* |
|
166 |
* @return an array containing all of the elements in this list in proper |
|
167 |
* sequence |
|
168 |
* @see Arrays#asList(Object[]) |
|
169 |
*/ |
|
170 |
Object[] toArray(); |
|
171 |
||
172 |
/** |
|
173 |
* Returns an array containing all of the elements in this list in |
|
174 |
* proper sequence (from first to last element); the runtime type of |
|
175 |
* the returned array is that of the specified array. If the list fits |
|
176 |
* in the specified array, it is returned therein. Otherwise, a new |
|
177 |
* array is allocated with the runtime type of the specified array and |
|
178 |
* the size of this list. |
|
179 |
* |
|
180 |
* <p>If the list fits in the specified array with room to spare (i.e., |
|
181 |
* the array has more elements than the list), the element in the array |
|
182 |
* immediately following the end of the list is set to <tt>null</tt>. |
|
183 |
* (This is useful in determining the length of the list <i>only</i> if |
|
184 |
* the caller knows that the list does not contain any null elements.) |
|
185 |
* |
|
186 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
187 |
* array-based and collection-based APIs. Further, this method allows |
|
188 |
* precise control over the runtime type of the output array, and may, |
|
189 |
* under certain circumstances, be used to save allocation costs. |
|
190 |
* |
|
191 |
* <p>Suppose <tt>x</tt> is a list known to contain only strings. |
|
192 |
* The following code can be used to dump the list into a newly |
|
193 |
* allocated array of <tt>String</tt>: |
|
194 |
* |
|
21339 | 195 |
* <pre>{@code |
196 |
* String[] y = x.toArray(new String[0]); |
|
197 |
* }</pre> |
|
2 | 198 |
* |
199 |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
|
200 |
* <tt>toArray()</tt>. |
|
201 |
* |
|
202 |
* @param a the array into which the elements of this list are to |
|
203 |
* be stored, if it is big enough; otherwise, a new array of the |
|
204 |
* same runtime type is allocated for this purpose. |
|
205 |
* @return an array containing the elements of this list |
|
206 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
207 |
* is not a supertype of the runtime type of every element in |
|
208 |
* this list |
|
209 |
* @throws NullPointerException if the specified array is null |
|
210 |
*/ |
|
211 |
<T> T[] toArray(T[] a); |
|
212 |
||
213 |
||
214 |
// Modification Operations |
|
215 |
||
216 |
/** |
|
217 |
* Appends the specified element to the end of this list (optional |
|
218 |
* operation). |
|
219 |
* |
|
220 |
* <p>Lists that support this operation may place limitations on what |
|
221 |
* elements may be added to this list. In particular, some |
|
222 |
* lists will refuse to add null elements, and others will impose |
|
223 |
* restrictions on the type of elements that may be added. List |
|
224 |
* classes should clearly specify in their documentation any restrictions |
|
225 |
* on what elements may be added. |
|
226 |
* |
|
227 |
* @param e element to be appended to this list |
|
228 |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
|
229 |
* @throws UnsupportedOperationException if the <tt>add</tt> operation |
|
230 |
* is not supported by this list |
|
231 |
* @throws ClassCastException if the class of the specified element |
|
232 |
* prevents it from being added to this list |
|
233 |
* @throws NullPointerException if the specified element is null and this |
|
234 |
* list does not permit null elements |
|
235 |
* @throws IllegalArgumentException if some property of this element |
|
236 |
* prevents it from being added to this list |
|
237 |
*/ |
|
238 |
boolean add(E e); |
|
239 |
||
240 |
/** |
|
241 |
* Removes the first occurrence of the specified element from this list, |
|
242 |
* if it is present (optional operation). If this list does not contain |
|
243 |
* the element, it is unchanged. More formally, removes the element with |
|
244 |
* the lowest index <tt>i</tt> such that |
|
245 |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> |
|
246 |
* (if such an element exists). Returns <tt>true</tt> if this list |
|
247 |
* contained the specified element (or equivalently, if this list changed |
|
248 |
* as a result of the call). |
|
249 |
* |
|
250 |
* @param o element to be removed from this list, if present |
|
251 |
* @return <tt>true</tt> if this list contained the specified element |
|
252 |
* @throws ClassCastException if the type of the specified element |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
253 |
* is incompatible with this list |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
254 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 255 |
* @throws NullPointerException if the specified element is null and this |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
256 |
* list does not permit null elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
257 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 258 |
* @throws UnsupportedOperationException if the <tt>remove</tt> operation |
259 |
* is not supported by this list |
|
260 |
*/ |
|
261 |
boolean remove(Object o); |
|
262 |
||
263 |
||
264 |
// Bulk Modification Operations |
|
265 |
||
266 |
/** |
|
267 |
* Returns <tt>true</tt> if this list contains all of the elements of the |
|
268 |
* specified collection. |
|
269 |
* |
|
270 |
* @param c collection to be checked for containment in this list |
|
271 |
* @return <tt>true</tt> if this list contains all of the elements of the |
|
272 |
* specified collection |
|
273 |
* @throws ClassCastException if the types of one or more elements |
|
274 |
* in the specified collection are incompatible with this |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
275 |
* list |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
276 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 277 |
* @throws NullPointerException if the specified collection contains one |
278 |
* or more null elements and this list does not permit null |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
279 |
* elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
280 |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
281 |
* or if the specified collection is null |
2 | 282 |
* @see #contains(Object) |
283 |
*/ |
|
284 |
boolean containsAll(Collection<?> c); |
|
285 |
||
286 |
/** |
|
287 |
* Appends all of the elements in the specified collection to the end of |
|
288 |
* this list, in the order that they are returned by the specified |
|
289 |
* collection's iterator (optional operation). The behavior of this |
|
290 |
* operation is undefined if the specified collection is modified while |
|
291 |
* the operation is in progress. (Note that this will occur if the |
|
292 |
* specified collection is this list, and it's nonempty.) |
|
293 |
* |
|
294 |
* @param c collection containing elements to be added to this list |
|
295 |
* @return <tt>true</tt> if this list changed as a result of the call |
|
296 |
* @throws UnsupportedOperationException if the <tt>addAll</tt> operation |
|
297 |
* is not supported by this list |
|
298 |
* @throws ClassCastException if the class of an element of the specified |
|
299 |
* collection prevents it from being added to this list |
|
300 |
* @throws NullPointerException if the specified collection contains one |
|
301 |
* or more null elements and this list does not permit null |
|
302 |
* elements, or if the specified collection is null |
|
303 |
* @throws IllegalArgumentException if some property of an element of the |
|
304 |
* specified collection prevents it from being added to this list |
|
305 |
* @see #add(Object) |
|
306 |
*/ |
|
307 |
boolean addAll(Collection<? extends E> c); |
|
308 |
||
309 |
/** |
|
310 |
* Inserts all of the elements in the specified collection into this |
|
311 |
* list at the specified position (optional operation). Shifts the |
|
312 |
* element currently at that position (if any) and any subsequent |
|
313 |
* elements to the right (increases their indices). The new elements |
|
314 |
* will appear in this list in the order that they are returned by the |
|
315 |
* specified collection's iterator. The behavior of this operation is |
|
316 |
* undefined if the specified collection is modified while the |
|
317 |
* operation is in progress. (Note that this will occur if the specified |
|
318 |
* collection is this list, and it's nonempty.) |
|
319 |
* |
|
320 |
* @param index index at which to insert the first element from the |
|
321 |
* specified collection |
|
322 |
* @param c collection containing elements to be added to this list |
|
323 |
* @return <tt>true</tt> if this list changed as a result of the call |
|
324 |
* @throws UnsupportedOperationException if the <tt>addAll</tt> operation |
|
325 |
* is not supported by this list |
|
326 |
* @throws ClassCastException if the class of an element of the specified |
|
327 |
* collection prevents it from being added to this list |
|
328 |
* @throws NullPointerException if the specified collection contains one |
|
329 |
* or more null elements and this list does not permit null |
|
330 |
* elements, or if the specified collection is null |
|
331 |
* @throws IllegalArgumentException if some property of an element of the |
|
332 |
* specified collection prevents it from being added to this list |
|
333 |
* @throws IndexOutOfBoundsException if the index is out of range |
|
334 |
* (<tt>index < 0 || index > size()</tt>) |
|
335 |
*/ |
|
336 |
boolean addAll(int index, Collection<? extends E> c); |
|
337 |
||
338 |
/** |
|
339 |
* Removes from this list all of its elements that are contained in the |
|
340 |
* specified collection (optional operation). |
|
341 |
* |
|
342 |
* @param c collection containing elements to be removed from this list |
|
343 |
* @return <tt>true</tt> if this list changed as a result of the call |
|
344 |
* @throws UnsupportedOperationException if the <tt>removeAll</tt> operation |
|
345 |
* is not supported by this list |
|
346 |
* @throws ClassCastException if the class of an element of this list |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
347 |
* is incompatible with the specified collection |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
348 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 349 |
* @throws NullPointerException if this list contains a null element and the |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
350 |
* specified collection does not permit null elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
351 |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
2 | 352 |
* or if the specified collection is null |
353 |
* @see #remove(Object) |
|
354 |
* @see #contains(Object) |
|
355 |
*/ |
|
356 |
boolean removeAll(Collection<?> c); |
|
357 |
||
358 |
/** |
|
359 |
* Retains only the elements in this list that are contained in the |
|
360 |
* specified collection (optional operation). In other words, removes |
|
361 |
* from this list all of its elements that are not contained in the |
|
362 |
* specified collection. |
|
363 |
* |
|
364 |
* @param c collection containing elements to be retained in this list |
|
365 |
* @return <tt>true</tt> if this list changed as a result of the call |
|
366 |
* @throws UnsupportedOperationException if the <tt>retainAll</tt> operation |
|
367 |
* is not supported by this list |
|
368 |
* @throws ClassCastException if the class of an element of this list |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
369 |
* is incompatible with the specified collection |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
370 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 371 |
* @throws NullPointerException if this list contains a null element and the |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
372 |
* specified collection does not permit null elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
373 |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
2 | 374 |
* or if the specified collection is null |
375 |
* @see #remove(Object) |
|
376 |
* @see #contains(Object) |
|
377 |
*/ |
|
378 |
boolean retainAll(Collection<?> c); |
|
379 |
||
380 |
/** |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
381 |
* Replaces each element of this list with the result of applying the |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
382 |
* operator to that element. Errors or runtime exceptions thrown by |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
383 |
* the operator are relayed to the caller. |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
384 |
* |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
385 |
* @implSpec |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
386 |
* The default implementation is equivalent to, for this {@code list}: |
21339 | 387 |
* <pre>{@code |
388 |
* final ListIterator<E> li = list.listIterator(); |
|
389 |
* while (li.hasNext()) { |
|
390 |
* li.set(operator.apply(li.next())); |
|
391 |
* } |
|
392 |
* }</pre> |
|
393 |
* |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
394 |
* If the list's list-iterator does not support the {@code set} operation |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
395 |
* then an {@code UnsupportedOperationException} will be thrown when |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
396 |
* replacing the first element. |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
397 |
* |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
398 |
* @param operator the operator to apply to each element |
20477
f7a1f6688508
8023340: Clarify that unmodifiable List.replaceAll() may not throw UOE if there are no items to be replaced.
mduigou
parents:
19435
diff
changeset
|
399 |
* @throws UnsupportedOperationException if this list is unmodifiable. |
f7a1f6688508
8023340: Clarify that unmodifiable List.replaceAll() may not throw UOE if there are no items to be replaced.
mduigou
parents:
19435
diff
changeset
|
400 |
* Implementations may throw this exception if an element |
f7a1f6688508
8023340: Clarify that unmodifiable List.replaceAll() may not throw UOE if there are no items to be replaced.
mduigou
parents:
19435
diff
changeset
|
401 |
* cannot be replaced or if, in general, modification is not |
f7a1f6688508
8023340: Clarify that unmodifiable List.replaceAll() may not throw UOE if there are no items to be replaced.
mduigou
parents:
19435
diff
changeset
|
402 |
* supported |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
403 |
* @throws NullPointerException if the specified operator is null or |
20477
f7a1f6688508
8023340: Clarify that unmodifiable List.replaceAll() may not throw UOE if there are no items to be replaced.
mduigou
parents:
19435
diff
changeset
|
404 |
* if the operator result is a null value and this list does |
f7a1f6688508
8023340: Clarify that unmodifiable List.replaceAll() may not throw UOE if there are no items to be replaced.
mduigou
parents:
19435
diff
changeset
|
405 |
* not permit null elements |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
406 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
407 |
* @since 1.8 |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
408 |
*/ |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
409 |
default void replaceAll(UnaryOperator<E> operator) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
410 |
Objects.requireNonNull(operator); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
411 |
final ListIterator<E> li = this.listIterator(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
412 |
while (li.hasNext()) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
413 |
li.set(operator.apply(li.next())); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
414 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
415 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
416 |
|
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
417 |
/** |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
418 |
* Sorts this list according to the order induced by the specified |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
419 |
* {@link Comparator}. |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
420 |
* |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
421 |
* <p>All elements in this list must be <i>mutually comparable</i> using the |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
422 |
* specified comparator (that is, {@code c.compare(e1, e2)} must not throw |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
423 |
* a {@code ClassCastException} for any elements {@code e1} and {@code e2} |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
424 |
* in the list). |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
425 |
* |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
426 |
* <p>If the specified comparator is {@code null} then all elements in this |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
427 |
* list must implement the {@link Comparable} interface and the elements' |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
428 |
* {@linkplain Comparable natural ordering} should be used. |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
429 |
* |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
430 |
* <p>This list must be modifiable, but need not be resizable. |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
431 |
* |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
432 |
* @implSpec |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
433 |
* The default implementation obtains an array containing all elements in |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
434 |
* this list, sorts the array, and iterates over this list resetting each |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
435 |
* element from the corresponding position in the array. (This avoids the |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
436 |
* n<sup>2</sup> log(n) performance that would result from attempting |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
437 |
* to sort a linked list in place.) |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
438 |
* |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
439 |
* @implNote |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
440 |
* This implementation is a stable, adaptive, iterative mergesort that |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
441 |
* requires far fewer than n lg(n) comparisons when the input array is |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
442 |
* partially sorted, while offering the performance of a traditional |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
443 |
* mergesort when the input array is randomly ordered. If the input array |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
444 |
* is nearly sorted, the implementation requires approximately n |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
445 |
* comparisons. Temporary storage requirements vary from a small constant |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
446 |
* for nearly sorted input arrays to n/2 object references for randomly |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
447 |
* ordered input arrays. |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
448 |
* |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
449 |
* <p>The implementation takes equal advantage of ascending and |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
450 |
* descending order in its input array, and can take advantage of |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
451 |
* ascending and descending order in different parts of the same |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
452 |
* input array. It is well-suited to merging two or more sorted arrays: |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
453 |
* simply concatenate the arrays and sort the resulting array. |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
454 |
* |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
455 |
* <p>The implementation was adapted from Tim Peters's list sort for Python |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
456 |
* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
457 |
* TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
458 |
* Sorting and Information Theoretic Complexity", in Proceedings of the |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
459 |
* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
460 |
* January 1993. |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
461 |
* |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
462 |
* @param c the {@code Comparator} used to compare list elements. |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
463 |
* A {@code null} value indicates that the elements' |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
464 |
* {@linkplain Comparable natural ordering} should be used |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
465 |
* @throws ClassCastException if the list contains elements that are not |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
466 |
* <i>mutually comparable</i> using the specified comparator |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
467 |
* @throws UnsupportedOperationException if the list's list-iterator does |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
468 |
* not support the {@code set} operation |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
469 |
* @throws IllegalArgumentException |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
470 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
471 |
* if the comparator is found to violate the {@link Comparator} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
472 |
* contract |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
473 |
* @since 1.8 |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
474 |
*/ |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
475 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
476 |
default void sort(Comparator<? super E> c) { |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
477 |
Object[] a = this.toArray(); |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
478 |
Arrays.sort(a, (Comparator) c); |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
479 |
ListIterator<E> i = this.listIterator(); |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
480 |
for (Object e : a) { |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
481 |
i.next(); |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
482 |
i.set((E) e); |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
21339
diff
changeset
|
483 |
} |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
484 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
485 |
|
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
486 |
/** |
2 | 487 |
* Removes all of the elements from this list (optional operation). |
488 |
* The list will be empty after this call returns. |
|
489 |
* |
|
490 |
* @throws UnsupportedOperationException if the <tt>clear</tt> operation |
|
491 |
* is not supported by this list |
|
492 |
*/ |
|
493 |
void clear(); |
|
494 |
||
495 |
||
496 |
// Comparison and hashing |
|
497 |
||
498 |
/** |
|
499 |
* Compares the specified object with this list for equality. Returns |
|
500 |
* <tt>true</tt> if and only if the specified object is also a list, both |
|
501 |
* lists have the same size, and all corresponding pairs of elements in |
|
502 |
* the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and |
|
503 |
* <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null : |
|
504 |
* e1.equals(e2))</tt>.) In other words, two lists are defined to be |
|
505 |
* equal if they contain the same elements in the same order. This |
|
506 |
* definition ensures that the equals method works properly across |
|
507 |
* different implementations of the <tt>List</tt> interface. |
|
508 |
* |
|
509 |
* @param o the object to be compared for equality with this list |
|
510 |
* @return <tt>true</tt> if the specified object is equal to this list |
|
511 |
*/ |
|
512 |
boolean equals(Object o); |
|
513 |
||
514 |
/** |
|
515 |
* Returns the hash code value for this list. The hash code of a list |
|
516 |
* is defined to be the result of the following calculation: |
|
21339 | 517 |
* <pre>{@code |
518 |
* int hashCode = 1; |
|
519 |
* for (E e : list) |
|
520 |
* hashCode = 31*hashCode + (e==null ? 0 : e.hashCode()); |
|
521 |
* }</pre> |
|
2 | 522 |
* This ensures that <tt>list1.equals(list2)</tt> implies that |
523 |
* <tt>list1.hashCode()==list2.hashCode()</tt> for any two lists, |
|
524 |
* <tt>list1</tt> and <tt>list2</tt>, as required by the general |
|
525 |
* contract of {@link Object#hashCode}. |
|
526 |
* |
|
527 |
* @return the hash code value for this list |
|
528 |
* @see Object#equals(Object) |
|
529 |
* @see #equals(Object) |
|
530 |
*/ |
|
531 |
int hashCode(); |
|
532 |
||
533 |
||
534 |
// Positional Access Operations |
|
535 |
||
536 |
/** |
|
537 |
* Returns the element at the specified position in this list. |
|
538 |
* |
|
539 |
* @param index index of the element to return |
|
540 |
* @return the element at the specified position in this list |
|
541 |
* @throws IndexOutOfBoundsException if the index is out of range |
|
542 |
* (<tt>index < 0 || index >= size()</tt>) |
|
543 |
*/ |
|
544 |
E get(int index); |
|
545 |
||
546 |
/** |
|
547 |
* Replaces the element at the specified position in this list with the |
|
548 |
* specified element (optional operation). |
|
549 |
* |
|
550 |
* @param index index of the element to replace |
|
551 |
* @param element element to be stored at the specified position |
|
552 |
* @return the element previously at the specified position |
|
553 |
* @throws UnsupportedOperationException if the <tt>set</tt> operation |
|
554 |
* is not supported by this list |
|
555 |
* @throws ClassCastException if the class of the specified element |
|
556 |
* prevents it from being added to this list |
|
557 |
* @throws NullPointerException if the specified element is null and |
|
558 |
* this list does not permit null elements |
|
559 |
* @throws IllegalArgumentException if some property of the specified |
|
560 |
* element prevents it from being added to this list |
|
561 |
* @throws IndexOutOfBoundsException if the index is out of range |
|
562 |
* (<tt>index < 0 || index >= size()</tt>) |
|
563 |
*/ |
|
564 |
E set(int index, E element); |
|
565 |
||
566 |
/** |
|
567 |
* Inserts the specified element at the specified position in this list |
|
568 |
* (optional operation). Shifts the element currently at that position |
|
569 |
* (if any) and any subsequent elements to the right (adds one to their |
|
570 |
* indices). |
|
571 |
* |
|
572 |
* @param index index at which the specified element is to be inserted |
|
573 |
* @param element element to be inserted |
|
574 |
* @throws UnsupportedOperationException if the <tt>add</tt> operation |
|
575 |
* is not supported by this list |
|
576 |
* @throws ClassCastException if the class of the specified element |
|
577 |
* prevents it from being added to this list |
|
578 |
* @throws NullPointerException if the specified element is null and |
|
579 |
* this list does not permit null elements |
|
580 |
* @throws IllegalArgumentException if some property of the specified |
|
581 |
* element prevents it from being added to this list |
|
582 |
* @throws IndexOutOfBoundsException if the index is out of range |
|
583 |
* (<tt>index < 0 || index > size()</tt>) |
|
584 |
*/ |
|
585 |
void add(int index, E element); |
|
586 |
||
587 |
/** |
|
588 |
* Removes the element at the specified position in this list (optional |
|
589 |
* operation). Shifts any subsequent elements to the left (subtracts one |
|
590 |
* from their indices). Returns the element that was removed from the |
|
591 |
* list. |
|
592 |
* |
|
593 |
* @param index the index of the element to be removed |
|
594 |
* @return the element previously at the specified position |
|
595 |
* @throws UnsupportedOperationException if the <tt>remove</tt> operation |
|
596 |
* is not supported by this list |
|
597 |
* @throws IndexOutOfBoundsException if the index is out of range |
|
598 |
* (<tt>index < 0 || index >= size()</tt>) |
|
599 |
*/ |
|
600 |
E remove(int index); |
|
601 |
||
602 |
||
603 |
// Search Operations |
|
604 |
||
605 |
/** |
|
606 |
* Returns the index of the first occurrence of the specified element |
|
607 |
* in this list, or -1 if this list does not contain the element. |
|
608 |
* More formally, returns the lowest index <tt>i</tt> such that |
|
609 |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
|
610 |
* or -1 if there is no such index. |
|
611 |
* |
|
612 |
* @param o element to search for |
|
613 |
* @return the index of the first occurrence of the specified element in |
|
614 |
* this list, or -1 if this list does not contain the element |
|
615 |
* @throws ClassCastException if the type of the specified element |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
616 |
* is incompatible with this list |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
617 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 618 |
* @throws NullPointerException if the specified element is null and this |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
619 |
* list does not permit null elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
620 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 621 |
*/ |
622 |
int indexOf(Object o); |
|
623 |
||
624 |
/** |
|
625 |
* Returns the index of the last occurrence of the specified element |
|
626 |
* in this list, or -1 if this list does not contain the element. |
|
627 |
* More formally, returns the highest index <tt>i</tt> such that |
|
628 |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
|
629 |
* or -1 if there is no such index. |
|
630 |
* |
|
631 |
* @param o element to search for |
|
632 |
* @return the index of the last occurrence of the specified element in |
|
633 |
* this list, or -1 if this list does not contain the element |
|
634 |
* @throws ClassCastException if the type of the specified element |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
635 |
* is incompatible with this list |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
636 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 637 |
* @throws NullPointerException if the specified element is null and this |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
638 |
* list does not permit null elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
639 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 640 |
*/ |
641 |
int lastIndexOf(Object o); |
|
642 |
||
643 |
||
644 |
// List Iterators |
|
645 |
||
646 |
/** |
|
647 |
* Returns a list iterator over the elements in this list (in proper |
|
648 |
* sequence). |
|
649 |
* |
|
650 |
* @return a list iterator over the elements in this list (in proper |
|
651 |
* sequence) |
|
652 |
*/ |
|
653 |
ListIterator<E> listIterator(); |
|
654 |
||
655 |
/** |
|
656 |
* Returns a list iterator over the elements in this list (in proper |
|
657 |
* sequence), starting at the specified position in the list. |
|
658 |
* The specified index indicates the first element that would be |
|
659 |
* returned by an initial call to {@link ListIterator#next next}. |
|
660 |
* An initial call to {@link ListIterator#previous previous} would |
|
661 |
* return the element with the specified index minus one. |
|
662 |
* |
|
663 |
* @param index index of the first element to be returned from the |
|
664 |
* list iterator (by a call to {@link ListIterator#next next}) |
|
665 |
* @return a list iterator over the elements in this list (in proper |
|
666 |
* sequence), starting at the specified position in the list |
|
667 |
* @throws IndexOutOfBoundsException if the index is out of range |
|
668 |
* ({@code index < 0 || index > size()}) |
|
669 |
*/ |
|
670 |
ListIterator<E> listIterator(int index); |
|
671 |
||
672 |
// View |
|
673 |
||
674 |
/** |
|
675 |
* Returns a view of the portion of this list between the specified |
|
676 |
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. (If |
|
677 |
* <tt>fromIndex</tt> and <tt>toIndex</tt> are equal, the returned list is |
|
678 |
* empty.) The returned list is backed by this list, so non-structural |
|
679 |
* changes in the returned list are reflected in this list, and vice-versa. |
|
680 |
* The returned list supports all of the optional list operations supported |
|
681 |
* by this list.<p> |
|
682 |
* |
|
683 |
* This method eliminates the need for explicit range operations (of |
|
684 |
* the sort that commonly exist for arrays). Any operation that expects |
|
685 |
* a list can be used as a range operation by passing a subList view |
|
686 |
* instead of a whole list. For example, the following idiom |
|
687 |
* removes a range of elements from a list: |
|
21339 | 688 |
* <pre>{@code |
2 | 689 |
* list.subList(from, to).clear(); |
21339 | 690 |
* }</pre> |
2 | 691 |
* Similar idioms may be constructed for <tt>indexOf</tt> and |
692 |
* <tt>lastIndexOf</tt>, and all of the algorithms in the |
|
693 |
* <tt>Collections</tt> class can be applied to a subList.<p> |
|
694 |
* |
|
695 |
* The semantics of the list returned by this method become undefined if |
|
696 |
* the backing list (i.e., this list) is <i>structurally modified</i> in |
|
697 |
* any way other than via the returned list. (Structural modifications are |
|
698 |
* those that change the size of this list, or otherwise perturb it in such |
|
699 |
* a fashion that iterations in progress may yield incorrect results.) |
|
700 |
* |
|
701 |
* @param fromIndex low endpoint (inclusive) of the subList |
|
702 |
* @param toIndex high endpoint (exclusive) of the subList |
|
703 |
* @return a view of the specified range within this list |
|
704 |
* @throws IndexOutOfBoundsException for an illegal endpoint index value |
|
705 |
* (<tt>fromIndex < 0 || toIndex > size || |
|
706 |
* fromIndex > toIndex</tt>) |
|
707 |
*/ |
|
708 |
List<E> subList(int fromIndex, int toIndex); |
|
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
709 |
|
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
710 |
/** |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
711 |
* Creates a {@link Spliterator} over the elements in this list. |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
712 |
* |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
713 |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
714 |
* {@link Spliterator#ORDERED}. Implementations should document the |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
715 |
* reporting of additional characteristic values. |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
716 |
* |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
717 |
* @implSpec |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
718 |
* The default implementation creates a |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
719 |
* <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
720 |
* from the list's {@code Iterator}. The spliterator inherits the |
19435
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19074
diff
changeset
|
721 |
* <em>fail-fast</em> properties of the list's iterator. |
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
722 |
* |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
723 |
* @implNote |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
724 |
* The created {@code Spliterator} additionally reports |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
725 |
* {@link Spliterator#SUBSIZED}. |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
726 |
* |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
727 |
* @return a {@code Spliterator} over the elements in this list |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
728 |
* @since 1.8 |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
729 |
*/ |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
730 |
@Override |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
731 |
default Spliterator<E> spliterator() { |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
732 |
return Spliterators.spliterator(this, Spliterator.ORDERED); |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
733 |
} |
2 | 734 |
} |