001 /*
002 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util;
027
028 /**
029 * An ordered collection (also known as a <i>sequence</i>). The user of this
030 * interface has precise control over where in the list each element is
031 * inserted. The user can access elements by their integer index (position in
032 * the list), and search for elements in the list.<p>
033 *
034 * Unlike sets, lists typically allow duplicate elements. More formally,
035 * lists typically allow pairs of elements <tt>e1</tt> and <tt>e2</tt>
036 * such that <tt>e1.equals(e2)</tt>, and they typically allow multiple
037 * null elements if they allow null elements at all. It is not inconceivable
038 * that someone might wish to implement a list that prohibits duplicates, by
039 * throwing runtime exceptions when the user attempts to insert them, but we
040 * expect this usage to be rare.<p>
041 *
042 * The <tt>List</tt> interface places additional stipulations, beyond those
043 * specified in the <tt>Collection</tt> interface, on the contracts of the
044 * <tt>iterator</tt>, <tt>add</tt>, <tt>remove</tt>, <tt>equals</tt>, and
045 * <tt>hashCode</tt> methods. Declarations for other inherited methods are
046 * also included here for convenience.<p>
047 *
048 * The <tt>List</tt> interface provides four methods for positional (indexed)
049 * access to list elements. Lists (like Java arrays) are zero based. Note
050 * that these operations may execute in time proportional to the index value
051 * for some implementations (the <tt>LinkedList</tt> class, for
052 * example). Thus, iterating over the elements in a list is typically
053 * preferable to indexing through it if the caller does not know the
054 * implementation.<p>
055 *
056 * The <tt>List</tt> interface provides a special iterator, called a
057 * <tt>ListIterator</tt>, that allows element insertion and replacement, and
058 * bidirectional access in addition to the normal operations that the
059 * <tt>Iterator</tt> interface provides. A method is provided to obtain a
060 * list iterator that starts at a specified position in the list.<p>
061 *
062 * The <tt>List</tt> interface provides two methods to search for a specified
063 * object. From a performance standpoint, these methods should be used with
064 * caution. In many implementations they will perform costly linear
065 * searches.<p>
066 *
067 * The <tt>List</tt> interface provides two methods to efficiently insert and
068 * remove multiple elements at an arbitrary point in the list.<p>
069 *
070 * Note: While it is permissible for lists to contain themselves as elements,
071 * extreme caution is advised: the <tt>equals</tt> and <tt>hashCode</tt>
072 * methods are no longer well defined on such a list.
073 *
074 * <p>Some list implementations have restrictions on the elements that
075 * they may contain. For example, some implementations prohibit null elements,
076 * and some have restrictions on the types of their elements. Attempting to
077 * add an ineligible element throws an unchecked exception, typically
078 * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
079 * to query the presence of an ineligible element may throw an exception,
080 * or it may simply return false; some implementations will exhibit the former
081 * behavior and some will exhibit the latter. More generally, attempting an
082 * operation on an ineligible element whose completion would not result in
083 * the insertion of an ineligible element into the list may throw an
084 * exception or it may succeed, at the option of the implementation.
085 * Such exceptions are marked as "optional" in the specification for this
086 * interface.
087 *
088 * <p>This interface is a member of the
089 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
090 * Java Collections Framework</a>.
091 *
092 * @author Josh Bloch
093 * @author Neal Gafter
094 * @version 1.58, 06/24/07
095 * @see Collection
096 * @see Set
097 * @see ArrayList
098 * @see LinkedList
099 * @see Vector
100 * @see Arrays#asList(Object[])
101 * @see Collections#nCopies(int, Object)
102 * @see Collections#EMPTY_LIST
103 * @see AbstractList
104 * @see AbstractSequentialList
105 * @since 1.2
106 */
107
108 public interface List<E> extends Collection<E> {
109 // Query Operations
110
111 /**
112 * Returns the number of elements in this list. If this list contains
113 * more than <tt>Integer.MAX_VALUE</tt> elements, returns
114 * <tt>Integer.MAX_VALUE</tt>.
115 *
116 * @return the number of elements in this list
117 */
118 int size();
119
120 /**
121 * Returns <tt>true</tt> if this list contains no elements.
122 *
123 * @return <tt>true</tt> if this list contains no elements
124 */
125 boolean isEmpty();
126
127 /**
128 * Returns <tt>true</tt> if this list contains the specified element.
129 * More formally, returns <tt>true</tt> if and only if this list contains
130 * at least one element <tt>e</tt> such that
131 * <tt>(o==null ? e==null : o.equals(e))</tt>.
132 *
133 * @param o element whose presence in this list is to be tested
134 * @return <tt>true</tt> if this list contains the specified element
135 * @throws ClassCastException if the type of the specified element
136 * is incompatible with this list (optional)
137 * @throws NullPointerException if the specified element is null and this
138 * list does not permit null elements (optional)
139 */
140 boolean contains(Object o);
141
142 /**
143 * Returns an iterator over the elements in this list in proper sequence.
144 *
145 * @return an iterator over the elements in this list in proper sequence
146 */
147 Iterator<E> iterator();
148
149 /**
150 * Returns an array containing all of the elements in this list in proper
151 * sequence (from first to last element).
152 *
153 * <p>The returned array will be "safe" in that no references to it are
154 * maintained by this list. (In other words, this method must
155 * allocate a new array even if this list is backed by an array).
156 * The caller is thus free to modify the returned array.
157 *
158 * <p>This method acts as bridge between array-based and collection-based
159 * APIs.
160 *
161 * @return an array containing all of the elements in this list in proper
162 * sequence
163 * @see Arrays#asList(Object[])
164 */
165 Object[] toArray();
166
167 /**
168 * Returns an array containing all of the elements in this list in
169 * proper sequence (from first to last element); the runtime type of
170 * the returned array is that of the specified array. If the list fits
171 * in the specified array, it is returned therein. Otherwise, a new
172 * array is allocated with the runtime type of the specified array and
173 * the size of this list.
174 *
175 * <p>If the list fits in the specified array with room to spare (i.e.,
176 * the array has more elements than the list), the element in the array
177 * immediately following the end of the list is set to <tt>null</tt>.
178 * (This is useful in determining the length of the list <i>only</i> if
179 * the caller knows that the list does not contain any null elements.)
180 *
181 * <p>Like the {@link #toArray()} method, this method acts as bridge between
182 * array-based and collection-based APIs. Further, this method allows
183 * precise control over the runtime type of the output array, and may,
184 * under certain circumstances, be used to save allocation costs.
185 *
186 * <p>Suppose <tt>x</tt> is a list known to contain only strings.
187 * The following code can be used to dump the list into a newly
188 * allocated array of <tt>String</tt>:
189 *
190 * <pre>
191 * String[] y = x.toArray(new String[0]);</pre>
192 *
193 * Note that <tt>toArray(new Object[0])</tt> is identical in function to
194 * <tt>toArray()</tt>.
195 *
196 * @param a the array into which the elements of this list are to
197 * be stored, if it is big enough; otherwise, a new array of the
198 * same runtime type is allocated for this purpose.
199 * @return an array containing the elements of this list
200 * @throws ArrayStoreException if the runtime type of the specified array
201 * is not a supertype of the runtime type of every element in
202 * this list
203 * @throws NullPointerException if the specified array is null
204 */
205 <T> T[] toArray(T[] a);
206
207 // Modification Operations
208
209 /**
210 * Appends the specified element to the end of this list (optional
211 * operation).
212 *
213 * <p>Lists that support this operation may place limitations on what
214 * elements may be added to this list. In particular, some
215 * lists will refuse to add null elements, and others will impose
216 * restrictions on the type of elements that may be added. List
217 * classes should clearly specify in their documentation any restrictions
218 * on what elements may be added.
219 *
220 * @param e element to be appended to this list
221 * @return <tt>true</tt> (as specified by {@link Collection#add})
222 * @throws UnsupportedOperationException if the <tt>add</tt> operation
223 * is not supported by this list
224 * @throws ClassCastException if the class of the specified element
225 * prevents it from being added to this list
226 * @throws NullPointerException if the specified element is null and this
227 * list does not permit null elements
228 * @throws IllegalArgumentException if some property of this element
229 * prevents it from being added to this list
230 */
231 boolean add(E e);
232
233 /**
234 * Removes the first occurrence of the specified element from this list,
235 * if it is present (optional operation). If this list does not contain
236 * the element, it is unchanged. More formally, removes the element with
237 * the lowest index <tt>i</tt> such that
238 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
239 * (if such an element exists). Returns <tt>true</tt> if this list
240 * contained the specified element (or equivalently, if this list changed
241 * as a result of the call).
242 *
243 * @param o element to be removed from this list, if present
244 * @return <tt>true</tt> if this list contained the specified element
245 * @throws ClassCastException if the type of the specified element
246 * is incompatible with this list (optional)
247 * @throws NullPointerException if the specified element is null and this
248 * list does not permit null elements (optional)
249 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
250 * is not supported by this list
251 */
252 boolean remove(Object o);
253
254 // Bulk Modification Operations
255
256 /**
257 * Returns <tt>true</tt> if this list contains all of the elements of the
258 * specified collection.
259 *
260 * @param c collection to be checked for containment in this list
261 * @return <tt>true</tt> if this list contains all of the elements of the
262 * specified collection
263 * @throws ClassCastException if the types of one or more elements
264 * in the specified collection are incompatible with this
265 * list (optional)
266 * @throws NullPointerException if the specified collection contains one
267 * or more null elements and this list does not permit null
268 * elements (optional), or if the specified collection is null
269 * @see #contains(Object)
270 */
271 boolean containsAll(Collection<?> c);
272
273 /**
274 * Appends all of the elements in the specified collection to the end of
275 * this list, in the order that they are returned by the specified
276 * collection's iterator (optional operation). The behavior of this
277 * operation is undefined if the specified collection is modified while
278 * the operation is in progress. (Note that this will occur if the
279 * specified collection is this list, and it's nonempty.)
280 *
281 * @param c collection containing elements to be added to this list
282 * @return <tt>true</tt> if this list changed as a result of the call
283 * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
284 * is not supported by this list
285 * @throws ClassCastException if the class of an element of the specified
286 * collection prevents it from being added to this list
287 * @throws NullPointerException if the specified collection contains one
288 * or more null elements and this list does not permit null
289 * elements, or if the specified collection is null
290 * @throws IllegalArgumentException if some property of an element of the
291 * specified collection prevents it from being added to this list
292 * @see #add(Object)
293 */
294 boolean addAll(Collection<? extends E> c);
295
296 /**
297 * Inserts all of the elements in the specified collection into this
298 * list at the specified position (optional operation). Shifts the
299 * element currently at that position (if any) and any subsequent
300 * elements to the right (increases their indices). The new elements
301 * will appear in this list in the order that they are returned by the
302 * specified collection's iterator. The behavior of this operation is
303 * undefined if the specified collection is modified while the
304 * operation is in progress. (Note that this will occur if the specified
305 * collection is this list, and it's nonempty.)
306 *
307 * @param index index at which to insert the first element from the
308 * specified collection
309 * @param c collection containing elements to be added to this list
310 * @return <tt>true</tt> if this list changed as a result of the call
311 * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
312 * is not supported by this list
313 * @throws ClassCastException if the class of an element of the specified
314 * collection prevents it from being added to this list
315 * @throws NullPointerException if the specified collection contains one
316 * or more null elements and this list does not permit null
317 * elements, or if the specified collection is null
318 * @throws IllegalArgumentException if some property of an element of the
319 * specified collection prevents it from being added to this list
320 * @throws IndexOutOfBoundsException if the index is out of range
321 * (<tt>index < 0 || index > size()</tt>)
322 */
323 boolean addAll(int index, Collection<? extends E> c);
324
325 /**
326 * Removes from this list all of its elements that are contained in the
327 * specified collection (optional operation).
328 *
329 * @param c collection containing elements to be removed from this list
330 * @return <tt>true</tt> if this list changed as a result of the call
331 * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
332 * is not supported by this list
333 * @throws ClassCastException if the class of an element of this list
334 * is incompatible with the specified collection (optional)
335 * @throws NullPointerException if this list contains a null element and the
336 * specified collection does not permit null elements (optional),
337 * or if the specified collection is null
338 * @see #remove(Object)
339 * @see #contains(Object)
340 */
341 boolean removeAll(Collection<?> c);
342
343 /**
344 * Retains only the elements in this list that are contained in the
345 * specified collection (optional operation). In other words, removes
346 * from this list all of its elements that are not contained in the
347 * specified collection.
348 *
349 * @param c collection containing elements to be retained in this list
350 * @return <tt>true</tt> if this list changed as a result of the call
351 * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
352 * is not supported by this list
353 * @throws ClassCastException if the class of an element of this list
354 * is incompatible with the specified collection (optional)
355 * @throws NullPointerException if this list contains a null element and the
356 * specified collection does not permit null elements (optional),
357 * or if the specified collection is null
358 * @see #remove(Object)
359 * @see #contains(Object)
360 */
361 boolean retainAll(Collection<?> c);
362
363 /**
364 * Removes all of the elements from this list (optional operation).
365 * The list will be empty after this call returns.
366 *
367 * @throws UnsupportedOperationException if the <tt>clear</tt> operation
368 * is not supported by this list
369 */
370 void clear();
371
372 // Comparison and hashing
373
374 /**
375 * Compares the specified object with this list for equality. Returns
376 * <tt>true</tt> if and only if the specified object is also a list, both
377 * lists have the same size, and all corresponding pairs of elements in
378 * the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and
379 * <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null :
380 * e1.equals(e2))</tt>.) In other words, two lists are defined to be
381 * equal if they contain the same elements in the same order. This
382 * definition ensures that the equals method works properly across
383 * different implementations of the <tt>List</tt> interface.
384 *
385 * @param o the object to be compared for equality with this list
386 * @return <tt>true</tt> if the specified object is equal to this list
387 */
388 boolean equals(Object o);
389
390 /**
391 * Returns the hash code value for this list. The hash code of a list
392 * is defined to be the result of the following calculation:
393 * <pre>
394 * int hashCode = 1;
395 * for (E e : list)
396 * hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
397 * </pre>
398 * This ensures that <tt>list1.equals(list2)</tt> implies that
399 * <tt>list1.hashCode()==list2.hashCode()</tt> for any two lists,
400 * <tt>list1</tt> and <tt>list2</tt>, as required by the general
401 * contract of {@link Object#hashCode}.
402 *
403 * @return the hash code value for this list
404 * @see Object#equals(Object)
405 * @see #equals(Object)
406 */
407 int hashCode();
408
409 // Positional Access Operations
410
411 /**
412 * Returns the element at the specified position in this list.
413 *
414 * @param index index of the element to return
415 * @return the element at the specified position in this list
416 * @throws IndexOutOfBoundsException if the index is out of range
417 * (<tt>index < 0 || index >= size()</tt>)
418 */
419 E get(int index);
420
421 /**
422 * Replaces the element at the specified position in this list with the
423 * specified element (optional operation).
424 *
425 * @param index index of the element to replace
426 * @param element element to be stored at the specified position
427 * @return the element previously at the specified position
428 * @throws UnsupportedOperationException if the <tt>set</tt> operation
429 * is not supported by this list
430 * @throws ClassCastException if the class of the specified element
431 * prevents it from being added to this list
432 * @throws NullPointerException if the specified element is null and
433 * this list does not permit null elements
434 * @throws IllegalArgumentException if some property of the specified
435 * element prevents it from being added to this list
436 * @throws IndexOutOfBoundsException if the index is out of range
437 * (<tt>index < 0 || index >= size()</tt>)
438 */
439 E set(int index, E element);
440
441 /**
442 * Inserts the specified element at the specified position in this list
443 * (optional operation). Shifts the element currently at that position
444 * (if any) and any subsequent elements to the right (adds one to their
445 * indices).
446 *
447 * @param index index at which the specified element is to be inserted
448 * @param element element to be inserted
449 * @throws UnsupportedOperationException if the <tt>add</tt> operation
450 * is not supported by this list
451 * @throws ClassCastException if the class of the specified element
452 * prevents it from being added to this list
453 * @throws NullPointerException if the specified element is null and
454 * this list does not permit null elements
455 * @throws IllegalArgumentException if some property of the specified
456 * element prevents it from being added to this list
457 * @throws IndexOutOfBoundsException if the index is out of range
458 * (<tt>index < 0 || index > size()</tt>)
459 */
460 void add(int index, E element);
461
462 /**
463 * Removes the element at the specified position in this list (optional
464 * operation). Shifts any subsequent elements to the left (subtracts one
465 * from their indices). Returns the element that was removed from the
466 * list.
467 *
468 * @param index the index of the element to be removed
469 * @return the element previously at the specified position
470 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
471 * is not supported by this list
472 * @throws IndexOutOfBoundsException if the index is out of range
473 * (<tt>index < 0 || index >= size()</tt>)
474 */
475 E remove(int index);
476
477 // Search Operations
478
479 /**
480 * Returns the index of the first occurrence of the specified element
481 * in this list, or -1 if this list does not contain the element.
482 * More formally, returns the lowest index <tt>i</tt> such that
483 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
484 * or -1 if there is no such index.
485 *
486 * @param o element to search for
487 * @return the index of the first occurrence of the specified element in
488 * this list, or -1 if this list does not contain the element
489 * @throws ClassCastException if the type of the specified element
490 * is incompatible with this list (optional)
491 * @throws NullPointerException if the specified element is null and this
492 * list does not permit null elements (optional)
493 */
494 int indexOf(Object o);
495
496 /**
497 * Returns the index of the last occurrence of the specified element
498 * in this list, or -1 if this list does not contain the element.
499 * More formally, returns the highest index <tt>i</tt> such that
500 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
501 * or -1 if there is no such index.
502 *
503 * @param o element to search for
504 * @return the index of the last occurrence of the specified element in
505 * this list, or -1 if this list does not contain the element
506 * @throws ClassCastException if the type of the specified element
507 * is incompatible with this list (optional)
508 * @throws NullPointerException if the specified element is null and this
509 * list does not permit null elements (optional)
510 */
511 int lastIndexOf(Object o);
512
513 // List Iterators
514
515 /**
516 * Returns a list iterator over the elements in this list (in proper
517 * sequence).
518 *
519 * @return a list iterator over the elements in this list (in proper
520 * sequence)
521 */
522 ListIterator<E> listIterator();
523
524 /**
525 * Returns a list iterator over the elements in this list (in proper
526 * sequence), starting at the specified position in the list.
527 * The specified index indicates the first element that would be
528 * returned by an initial call to {@link ListIterator#next next}.
529 * An initial call to {@link ListIterator#previous previous} would
530 * return the element with the specified index minus one.
531 *
532 * @param index index of the first element to be returned from the
533 * list iterator (by a call to {@link ListIterator#next next})
534 * @return a list iterator over the elements in this list (in proper
535 * sequence), starting at the specified position in the list
536 * @throws IndexOutOfBoundsException if the index is out of range
537 * ({@code index < 0 || index > size()})
538 */
539 ListIterator<E> listIterator(int index);
540
541 // View
542
543 /**
544 * Returns a view of the portion of this list between the specified
545 * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. (If
546 * <tt>fromIndex</tt> and <tt>toIndex</tt> are equal, the returned list is
547 * empty.) The returned list is backed by this list, so non-structural
548 * changes in the returned list are reflected in this list, and vice-versa.
549 * The returned list supports all of the optional list operations supported
550 * by this list.<p>
551 *
552 * This method eliminates the need for explicit range operations (of
553 * the sort that commonly exist for arrays). Any operation that expects
554 * a list can be used as a range operation by passing a subList view
555 * instead of a whole list. For example, the following idiom
556 * removes a range of elements from a list:
557 * <pre>
558 * list.subList(from, to).clear();
559 * </pre>
560 * Similar idioms may be constructed for <tt>indexOf</tt> and
561 * <tt>lastIndexOf</tt>, and all of the algorithms in the
562 * <tt>Collections</tt> class can be applied to a subList.<p>
563 *
564 * The semantics of the list returned by this method become undefined if
565 * the backing list (i.e., this list) is <i>structurally modified</i> in
566 * any way other than via the returned list. (Structural modifications are
567 * those that change the size of this list, or otherwise perturb it in such
568 * a fashion that iterations in progress may yield incorrect results.)
569 *
570 * @param fromIndex low endpoint (inclusive) of the subList
571 * @param toIndex high endpoint (exclusive) of the subList
572 * @return a view of the specified range within this list
573 * @throws IndexOutOfBoundsException for an illegal endpoint index value
574 * (<tt>fromIndex < 0 || toIndex > size ||
575 * fromIndex > toIndex</tt>)
576 */
577 List<E> subList(int fromIndex, int toIndex);
578 }
|