| List.java |
1 /*
2 * %W% %E%
3 *
4 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6 */
7
8 package java.util;
9
10 /**
11 * An ordered collection (also known as a <i>sequence</i>). The user of this
12 * interface has precise control over where in the list each element is
13 * inserted. The user can access elements by their integer index (position in
14 * the list), and search for elements in the list.<p>
15 *
16 * Unlike sets, lists typically allow duplicate elements. More formally,
17 * lists typically allow pairs of elements <tt>e1</tt> and <tt>e2</tt>
18 * such that <tt>e1.equals(e2)</tt>, and they typically allow multiple
19 * null elements if they allow null elements at all. It is not inconceivable
20 * that someone might wish to implement a list that prohibits duplicates, by
21 * throwing runtime exceptions when the user attempts to insert them, but we
22 * expect this usage to be rare.<p>
23 *
24 * The <tt>List</tt> interface places additional stipulations, beyond those
25 * specified in the <tt>Collection</tt> interface, on the contracts of the
26 * <tt>iterator</tt>, <tt>add</tt>, <tt>remove</tt>, <tt>equals</tt>, and
27 * <tt>hashCode</tt> methods. Declarations for other inherited methods are
28 * also included here for convenience.<p>
29 *
30 * The <tt>List</tt> interface provides four methods for positional (indexed)
31 * access to list elements. Lists (like Java arrays) are zero based. Note
32 * that these operations may execute in time proportional to the index value
33 * for some implementations (the <tt>LinkedList</tt> class, for
34 * example). Thus, iterating over the elements in a list is typically
35 * preferable to indexing through it if the caller does not know the
36 * implementation.<p>
37 *
38 * The <tt>List</tt> interface provides a special iterator, called a
39 * <tt>ListIterator</tt>, that allows element insertion and replacement, and
40 * bidirectional access in addition to the normal operations that the
41 * <tt>Iterator</tt> interface provides. A method is provided to obtain a
42 * list iterator that starts at a specified position in the list.<p>
43 *
44 * The <tt>List</tt> interface provides two methods to search for a specified
45 * object. From a performance standpoint, these methods should be used with
46 * caution. In many implementations they will perform costly linear
47 * searches.<p>
48 *
49 * The <tt>List</tt> interface provides two methods to efficiently insert and
50 * remove multiple elements at an arbitrary point in the list.<p>
51 *
52 * Note: While it is permissible for lists to contain themselves as elements,
53 * extreme caution is advised: the <tt>equals</tt> and <tt>hashCode</tt>
54 * methods are no longer well defined on such a list.
55 *
56 * <p>Some list implementations have restrictions on the elements that
57 * they may contain. For example, some implementations prohibit null elements,
58 * and some have restrictions on the types of their elements. Attempting to
59 * add an ineligible element throws an unchecked exception, typically
60 * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
61 * to query the presence of an ineligible element may throw an exception,
62 * or it may simply return false; some implementations will exhibit the former
63 * behavior and some will exhibit the latter. More generally, attempting an
64 * operation on an ineligible element whose completion would not result in
65 * the insertion of an ineligible element into the list may throw an
66 * exception or it may succeed, at the option of the implementation.
67 * Such exceptions are marked as "optional" in the specification for this
68 * interface.
69 *
70 * <p>This interface is a member of the
71 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
72 * Java Collections Framework</a>.
73 *
74 * @author Josh Bloch
75 * @author Neal Gafter
76 * @version %I%, %G%
77 * @see Collection
78 * @see Set
79 * @see ArrayList
80 * @see LinkedList
81 * @see Vector
82 * @see Arrays#asList(Object[])
83 * @see Collections#nCopies(int, Object)
84 * @see Collections#EMPTY_LIST
85 * @see AbstractList
86 * @see AbstractSequentialList
87 * @since 1.2
88 */
89
90 public interface List<E> extends Collection<E> {
91 // Query Operations
92
93 /**
94 * Returns the number of elements in this list. If this list contains
95 * more than <tt>Integer.MAX_VALUE</tt> elements, returns
96 * <tt>Integer.MAX_VALUE</tt>.
97 *
98 * @return the number of elements in this list
99 */
100 int size();
101
102 /**
103 * Returns <tt>true</tt> if this list contains no elements.
104 *
105 * @return <tt>true</tt> if this list contains no elements
106 */
107 boolean isEmpty();
108
109 /**
110 * Returns <tt>true</tt> if this list contains the specified element.
111 * More formally, returns <tt>true</tt> if and only if this list contains
112 * at least one element <tt>e</tt> such that
113 * <tt>(o==null ? e==null : o.equals(e))</tt>.
114 *
115 * @param o element whose presence in this list is to be tested
116 * @return <tt>true</tt> if this list contains the specified element
117 * @throws ClassCastException if the type of the specified element
118 * is incompatible with this list (optional)
119 * @throws NullPointerException if the specified element is null and this
120 * list does not permit null elements (optional)
121 */
122 boolean contains(Object o);
123
124 /**
125 * Returns an iterator over the elements in this list in proper sequence.
126 *
127 * @return an iterator over the elements in this list in proper sequence
128 */
129 Iterator<E> iterator();
130
131 /**
132 * Returns an array containing all of the elements in this list in proper
133 * sequence (from first to last element).
134 *
135 * <p>The returned array will be "safe" in that no references to it are
136 * maintained by this list. (In other words, this method must
137 * allocate a new array even if this list is backed by an array).
138 * The caller is thus free to modify the returned array.
139 *
140 * <p>This method acts as bridge between array-based and collection-based
141 * APIs.
142 *
143 * @return an array containing all of the elements in this list in proper
144 * sequence
145 * @see Arrays#asList(Object[])
146 */
147 Object[] toArray();
148
149 /**
150 * Returns an array containing all of the elements in this list in
151 * proper sequence (from first to last element); the runtime type of
152 * the returned array is that of the specified array. If the list fits
153 * in the specified array, it is returned therein. Otherwise, a new
154 * array is allocated with the runtime type of the specified array and
155 * the size of this list.
156 *
157 * <p>If the list fits in the specified array with room to spare (i.e.,
158 * the array has more elements than the list), the element in the array
159 * immediately following the end of the list is set to <tt>null</tt>.
160 * (This is useful in determining the length of the list <i>only</i> if
161 * the caller knows that the list does not contain any null elements.)
162 *
163 * <p>Like the {@link #toArray()} method, this method acts as bridge between
164 * array-based and collection-based APIs. Further, this method allows
165 * precise control over the runtime type of the output array, and may,
166 * under certain circumstances, be used to save allocation costs.
167 *
168 * <p>Suppose <tt>x</tt> is a list known to contain only strings.
169 * The following code can be used to dump the list into a newly
170 * allocated array of <tt>String</tt>:
171 *
172 * <pre>
173 * String[] y = x.toArray(new String[0]);</pre>
174 *
175 * Note that <tt>toArray(new Object[0])</tt> is identical in function to
176 * <tt>toArray()</tt>.
177 *
178 * @param a the array into which the elements of this list are to
179 * be stored, if it is big enough; otherwise, a new array of the
180 * same runtime type is allocated for this purpose.
181 * @return an array containing the elements of this list
182 * @throws ArrayStoreException if the runtime type of the specified array
183 * is not a supertype of the runtime type of every element in
184 * this list
185 * @throws NullPointerException if the specified array is null
186 */
187 <T> T[] toArray(T[] a);
188
189
190 // Modification Operations
191
192 /**
193 * Appends the specified element to the end of this list (optional
194 * operation).
195 *
196 * <p>Lists that support this operation may place limitations on what
197 * elements may be added to this list. In particular, some
198 * lists will refuse to add null elements, and others will impose
199 * restrictions on the type of elements that may be added. List
200 * classes should clearly specify in their documentation any restrictions
201 * on what elements may be added.
202 *
203 * @param e element to be appended to this list
204 * @return <tt>true</tt> (as specified by {@link Collection#add})
205 * @throws UnsupportedOperationException if the <tt>add</tt> operation
206 * is not supported by this list
207 * @throws ClassCastException if the class of the specified element
208 * prevents it from being added to this list
209 * @throws NullPointerException if the specified element is null and this
210 * list does not permit null elements
211 * @throws IllegalArgumentException if some property of this element
212 * prevents it from being added to this list
213 */
214 boolean add(E e);
215
216 /**
217 * Removes the first occurrence of the specified element from this list,
218 * if it is present (optional operation). If this list does not contain
219 * the element, it is unchanged. More formally, removes the element with
220 * the lowest index <tt>i</tt> such that
221 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
222 * (if such an element exists). Returns <tt>true</tt> if this list
223 * contained the specified element (or equivalently, if this list changed
224 * as a result of the call).
225 *
226 * @param o element to be removed from this list, if present
227 * @return <tt>true</tt> if this list contained the specified element
228 * @throws ClassCastException if the type of the specified element
229 * is incompatible with this list (optional)
230 * @throws NullPointerException if the specified element is null and this
231 * list does not permit null elements (optional)
232 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
233 * is not supported by this list
234 */
235 boolean remove(Object o);
236
237
238 // Bulk Modification Operations
239
240 /**
241 * Returns <tt>true</tt> if this list contains all of the elements of the
242 * specified collection.
243 *
244 * @param c collection to be checked for containment in this list
245 * @return <tt>true</tt> if this list contains all of the elements of the
246 * specified collection
247 * @throws ClassCastException if the types of one or more elements
248 * in the specified collection are incompatible with this
249 * list (optional)
250 * @throws NullPointerException if the specified collection contains one
251 * or more null elements and this list does not permit null
252 * elements (optional), or if the specified collection is null
253 * @see #contains(Object)
254 */
255 boolean containsAll(Collection<?> c);
256
257 /**
258 * Appends all of the elements in the specified collection to the end of
259 * this list, in the order that they are returned by the specified
260 * collection's iterator (optional operation). The behavior of this
261 * operation is undefined if the specified collection is modified while
262 * the operation is in progress. (Note that this will occur if the
263 * specified collection is this list, and it's nonempty.)
264 *
265 * @param c collection containing elements to be added to this list
266 * @return <tt>true</tt> if this list changed as a result of the call
267 * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
268 * is not supported by this list
269 * @throws ClassCastException if the class of an element of the specified
270 * collection prevents it from being added to this list
271 * @throws NullPointerException if the specified collection contains one
272 * or more null elements and this list does not permit null
273 * elements, or if the specified collection is null
274 * @throws IllegalArgumentException if some property of an element of the
275 * specified collection prevents it from being added to this list
276 * @see #add(Object)
277 */
278 boolean addAll(Collection<? extends E> c);
279
280 /**
281 * Inserts all of the elements in the specified collection into this
282 * list at the specified position (optional operation). Shifts the
283 * element currently at that position (if any) and any subsequent
284 * elements to the right (increases their indices). The new elements
285 * will appear in this list in the order that they are returned by the
286 * specified collection's iterator. The behavior of this operation is
287 * undefined if the specified collection is modified while the
288 * operation is in progress. (Note that this will occur if the specified
289 * collection is this list, and it's nonempty.)
290 *
291 * @param index index at which to insert the first element from the
292 * specified collection
293 * @param c collection containing elements to be added to this list
294 * @return <tt>true</tt> if this list changed as a result of the call
295 * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
296 * is not supported by this list
297 * @throws ClassCastException if the class of an element of the specified
298 * collection prevents it from being added to this list
299 * @throws NullPointerException if the specified collection contains one
300 * or more null elements and this list does not permit null
301 * elements, or if the specified collection is null
302 * @throws IllegalArgumentException if some property of an element of the
303 * specified collection prevents it from being added to this list
304 * @throws IndexOutOfBoundsException if the index is out of range
305 * (<tt>index < 0 || index > size()</tt>)
306 */
307 boolean addAll(int index, Collection<? extends E> c);
308
309 /**
310 * Removes from this list all of its elements that are contained in the
311 * specified collection (optional operation).
312 *
313 * @param c collection containing elements to be removed from this list
314 * @return <tt>true</tt> if this list changed as a result of the call
315 * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
316 * is not supported by this list
317 * @throws ClassCastException if the class of an element of this list
318 * is incompatible with the specified collection (optional)
319 * @throws NullPointerException if this list contains a null element and the
320 * specified collection does not permit null elements (optional),
321 * or if the specified collection is null
322 * @see #remove(Object)
323 * @see #contains(Object)
324 */
325 boolean removeAll(Collection<?> c);
326
327 /**
328 * Retains only the elements in this list that are contained in the
329 * specified collection (optional operation). In other words, removes
330 * from this list all the elements that are not contained in the specified
331 * collection.
332 *
333 * @param c collection containing elements to be retained in this list
334 * @return <tt>true</tt> if this list changed as a result of the call
335 * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
336 * is not supported by this list
337 * @throws ClassCastException if the class of an element of this list
338 * is incompatible with the specified collection (optional)
339 * @throws NullPointerException if this list contains a null element and the
340 * specified collection does not permit null elements (optional),
341 * or if the specified collection is null
342 * @see #remove(Object)
343 * @see #contains(Object)
344 */
345 boolean retainAll(Collection<?> c);
346
347 /**
348 * Removes all of the elements from this list (optional operation).
349 * The list will be empty after this call returns.
350 *
351 * @throws UnsupportedOperationException if the <tt>clear</tt> operation
352 * is not supported by this list
353 */
354 void clear();
355
356
357 // Comparison and hashing
358
359 /**
360 * Compares the specified object with this list for equality. Returns
361 * <tt>true</tt> if and only if the specified object is also a list, both
362 * lists have the same size, and all corresponding pairs of elements in
363 * the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and
364 * <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null :
365 * e1.equals(e2))</tt>.) In other words, two lists are defined to be
366 * equal if they contain the same elements in the same order. This
367 * definition ensures that the equals method works properly across
368 * different implementations of the <tt>List</tt> interface.
369 *
370 * @param o the object to be compared for equality with this list
371 * @return <tt>true</tt> if the specified object is equal to this list
372 */
373 boolean equals(Object o);
374
375 /**
376 * Returns the hash code value for this list. The hash code of a list
377 * is defined to be the result of the following calculation:
378 * <pre>
379 * int hashCode = 1;
380 * Iterator<E> i = list.iterator();
381 * while (i.hasNext()) {
382 * E obj = i.next();
383 * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
384 * }
385 * </pre>
386 * This ensures that <tt>list1.equals(list2)</tt> implies that
387 * <tt>list1.hashCode()==list2.hashCode()</tt> for any two lists,
388 * <tt>list1</tt> and <tt>list2</tt>, as required by the general
389 * contract of {@link Object#hashCode}.
390 *
391 * @return the hash code value for this list
392 * @see Object#equals(Object)
393 * @see #equals(Object)
394 */
395 int hashCode();
396
397
398 // Positional Access Operations
399
400 /**
401 * Returns the element at the specified position in this list.
402 *
403 * @param index index of the element to return
404 * @return the element at the specified position in this list
405 * @throws IndexOutOfBoundsException if the index is out of range
406 * (<tt>index < 0 || index >= size()</tt>)
407 */
408 E get(int index);
409
410 /**
411 * Replaces the element at the specified position in this list with the
412 * specified element (optional operation).
413 *
414 * @param index index of the element to replace
415 * @param element element to be stored at the specified position
416 * @return the element previously at the specified position
417 * @throws UnsupportedOperationException if the <tt>set</tt> operation
418 * is not supported by this list
419 * @throws ClassCastException if the class of the specified element
420 * prevents it from being added to this list
421 * @throws NullPointerException if the specified element is null and
422 * this list does not permit null elements
423 * @throws IllegalArgumentException if some property of the specified
424 * element prevents it from being added to this list
425 * @throws IndexOutOfBoundsException if the index is out of range
426 * (<tt>index < 0 || index >= size()</tt>)
427 */
428 E set(int index, E element);
429
430 /**
431 * Inserts the specified element at the specified position in this list
432 * (optional operation). Shifts the element currently at that position
433 * (if any) and any subsequent elements to the right (adds one to their
434 * indices).
435 *
436 * @param index index at which the specified element is to be inserted
437 * @param element element to be inserted
438 * @throws UnsupportedOperationException if the <tt>add</tt> operation
439 * is not supported by this list
440 * @throws ClassCastException if the class of the specified element
441 * prevents it from being added to this list
442 * @throws NullPointerException if the specified element is null and
443 * this list does not permit null elements
444 * @throws IllegalArgumentException if some property of the specified
445 * element prevents it from being added to this list
446 * @throws IndexOutOfBoundsException if the index is out of range
447 * (<tt>index < 0 || index > size()</tt>)
448 */
449 void add(int index, E element);
450
451 /**
452 * Removes the element at the specified position in this list (optional
453 * operation). Shifts any subsequent elements to the left (subtracts one
454 * from their indices). Returns the element that was removed from the
455 * list.
456 *
457 * @param index the index of the element to be removed
458 * @return the element previously at the specified position
459 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
460 * is not supported by this list
461 * @throws IndexOutOfBoundsException if the index is out of range
462 * (<tt>index < 0 || index >= size()</tt>)
463 */
464 E remove(int index);
465
466
467 // Search Operations
468
469 /**
470 * Returns the index of the first occurrence of the specified element
471 * in this list, or -1 if this list does not contain the element.
472 * More formally, returns the lowest index <tt>i</tt> such that
473 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
474 * or -1 if there is no such index.
475 *
476 * @param o element to search for
477 * @return the index of the first occurrence of the specified element in
478 * this list, or -1 if this list does not contain the element
479 * @throws ClassCastException if the type of the specified element
480 * is incompatible with this list (optional)
481 * @throws NullPointerException if the specified element is null and this
482 * list does not permit null elements (optional)
483 */
484 int indexOf(Object o);
485
486 /**
487 * Returns the index of the last occurrence of the specified element
488 * in this list, or -1 if this list does not contain the element.
489 * More formally, returns the highest index <tt>i</tt> such that
490 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
491 * or -1 if there is no such index.
492 *
493 * @param o element to search for
494 * @return the index of the last occurrence of the specified element in
495 * this list, or -1 if this list does not contain the element
496 * @throws ClassCastException if the type of the specified element
497 * is incompatible with this list (optional)
498 * @throws NullPointerException if the specified element is null and this
499 * list does not permit null elements (optional)
500 */
501 int lastIndexOf(Object o);
502
503
504 // List Iterators
505
506 /**
507 * Returns a list iterator over the elements in this list (in proper
508 * sequence).
509 *
510 * @return a list iterator over the elements in this list (in proper
511 * sequence)
512 */
513 ListIterator<E> listIterator();
514
515 /**
516 * Returns a list iterator of the elements in this list (in proper
517 * sequence), starting at the specified position in this list.
518 * The specified index indicates the first element that would be
519 * returned by an initial call to {@link ListIterator#next next}.
520 * An initial call to {@link ListIterator#previous previous} would
521 * return the element with the specified index minus one.
522 *
523 * @param index index of first element to be returned from the
524 * list iterator (by a call to the <tt>next</tt> method)
525 * @return a list iterator of the elements in this list (in proper
526 * sequence), starting at the specified position in this list
527 * @throws IndexOutOfBoundsException if the index is out of range
528 * (<tt>index < 0 || index > size()</tt>)
529 */
530 ListIterator<E> listIterator(int index);
531
532 // View
533
534 /**
535 * Returns a view of the portion of this list between the specified
536 * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. (If
537 * <tt>fromIndex</tt> and <tt>toIndex</tt> are equal, the returned list is
538 * empty.) The returned list is backed by this list, so non-structural
539 * changes in the returned list are reflected in this list, and vice-versa.
540 * The returned list supports all of the optional list operations supported
541 * by this list.<p>
542 *
543 * This method eliminates the need for explicit range operations (of
544 * the sort that commonly exist for arrays). Any operation that expects
545 * a list can be used as a range operation by passing a subList view
546 * instead of a whole list. For example, the following idiom
547 * removes a range of elements from a list:
548 * <pre>
549 * list.subList(from, to).clear();
550 * </pre>
551 * Similar idioms may be constructed for <tt>indexOf</tt> and
552 * <tt>lastIndexOf</tt>, and all of the algorithms in the
553 * <tt>Collections</tt> class can be applied to a subList.<p>
554 *
555 * The semantics of the list returned by this method become undefined if
556 * the backing list (i.e., this list) is <i>structurally modified</i> in
557 * any way other than via the returned list. (Structural modifications are
558 * those that change the size of this list, or otherwise perturb it in such
559 * a fashion that iterations in progress may yield incorrect results.)
560 *
561 * @param fromIndex low endpoint (inclusive) of the subList
562 * @param toIndex high endpoint (exclusive) of the subList
563 * @return a view of the specified range within this list
564 * @throws IndexOutOfBoundsException for an illegal endpoint index value
565 * (<tt>fromIndex < 0 || toIndex > size ||
566 * fromIndex > toIndex</tt>)
567 */
568 List<E> subList(int fromIndex, int toIndex);
569 }
570