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