1. /*
  2. * @(#)List.java 1.27 01/11/29
  3. *
  4. * Copyright 2002 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. * @author Josh Bloch
  55. * @version 1.15 10/13/97
  56. * @see Collection
  57. * @see Set
  58. * @see ArrayList
  59. * @see LinkedList
  60. * @see Vector
  61. * @see Arrays#asList(Object[])
  62. * @see Collections#nCopies(int, Object)
  63. * @see Collections#EMPTY_LIST
  64. * @see AbstractList
  65. * @see AbstractSequentialList
  66. * @since JDK1.2
  67. */
  68. public interface List extends Collection {
  69. // Query Operations
  70. /**
  71. * Returns the number of elements in this list. If this list contains
  72. * more than <tt>Integer.MAX_VALUE</tt> elements, returns
  73. * <tt>Integer.MAX_VALUE</tt>.
  74. *
  75. * @return the number of elements in this list.
  76. */
  77. int size();
  78. /**
  79. * Returns <tt>true</tt> if this list contains no elements.
  80. *
  81. * @return <tt>true</tt> if this list contains no elements.
  82. */
  83. boolean isEmpty();
  84. /**
  85. *
  86. * Returns <tt>true</tt> if this list contains the specified element.
  87. * More formally, returns <tt>true</tt> if and only if this list contains
  88. * at least one element <tt>e</tt> such that
  89. * <tt>(o==null ? e==null : o.equals(e))</tt>.
  90. *
  91. * @param o element whose presence in this list is to be tested.
  92. * @returns <tt>true</tt> if this list contains the specified element.
  93. */
  94. boolean contains(Object o);
  95. /**
  96. * Returns an iterator over the elements in this list in proper sequence.
  97. *
  98. * @return an iterator over the elements in this list in proper sequence.
  99. */
  100. Iterator iterator();
  101. /**
  102. * Returns an array containing all of the elements in this list in proper
  103. * sequence. Obeys the general contract of the
  104. * <tt>Collection.toArray</tt> method.
  105. *
  106. * @return an array containing all of the elements in this list in proper
  107. * sequence.
  108. * @see Arrays#asList(Object[])
  109. */
  110. Object[] toArray();
  111. /**
  112. * Returns an array containing all of the elements in this list in proper
  113. * sequence; the runtime type of the returned array is that of the
  114. * specified array. Obeys the general contract of the
  115. * <tt>Collection.toArray(Object[])</tt> method.
  116. *
  117. * @param a the array into which the elements of this list are to
  118. * be stored, if it is big enough; otherwise, a new array of the
  119. * same runtime type is allocated for this purpose.
  120. * @return an array containing the elements of this list.
  121. *
  122. * @throws ArrayStoreException if the runtime type of the specified array
  123. * is not a supertype of the runtime type of every element in
  124. * this list.
  125. */
  126. Object[] toArray(Object a[]);
  127. // Modification Operations
  128. /**
  129. * Appends the specified element to the end of this list (optional
  130. * operation). <p>
  131. *
  132. * Lists that support this operation may place limitations on what
  133. * elements may be added to this list. In particular, some
  134. * lists will refuse to add null elements, and others will impose
  135. * restrictions on the type of elements that may be added. List
  136. * classes should clearly specify in their documentation any restrictions
  137. * on what elements may be added.
  138. *
  139. * @param o element to be appended to this list.
  140. * @return <tt>true</tt> (as per the general contract of the
  141. * <tt>Collection.add</tt> method).
  142. *
  143. * @throws UnsupportedOperationException if the <tt>add</tt> method is not
  144. * supported by this list.
  145. * @throws ClassCastException if the class of the specified element
  146. * prevents it from being added to this list.
  147. * @throws IllegalArgumentException if some aspect of this element
  148. * prevents it from being added to this collection.
  149. */
  150. boolean add(Object o);
  151. /**
  152. * Removes the first occurrence in this list of the specified element
  153. * (optional operation). If this list does not contain the element, it is
  154. * unchanged. More formally, removes the element with the lowest index i
  155. * such that <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> (if
  156. * such an element exists).
  157. *
  158. * @param o element to be removed from this list, if present.
  159. * @return <tt>true</tt> if this list contained the specified element.
  160. *
  161. * @throws UnsupportedOperationException if the <tt>remove</tt> method is
  162. * not supported by this list.
  163. */
  164. boolean remove(Object o);
  165. // Bulk Modification Operations
  166. /**
  167. *
  168. * Returns <tt>true</tt> if this list contains all of the elements of the
  169. * specified collection.
  170. *
  171. * @param c collection to be checked for containment in this list.
  172. * @return <tt>true</tt> if this list contains all of the elements of the
  173. * specified collection.
  174. *
  175. * @see #contains(Object)
  176. */
  177. boolean containsAll(Collection c);
  178. /**
  179. * Appends all of the elements in the specified collection to the end of
  180. * this list, in the order that they are returned by the specified
  181. * collection's iterator (optional operation). The behavior of this
  182. * operation is unspecified if the specified collection is modified while
  183. * the operation is in progress. (Note that this will occur if the
  184. * specified collection is this list, and it's nonempty.)
  185. *
  186. * @param c collection whose elements are to be added to this list.
  187. * @return <tt>true</tt> if this list changed as a result of the call.
  188. *
  189. * @throws UnsupportedOperationException if the <tt>addAll</tt> method is
  190. * not supported by this list.
  191. *
  192. * @throws ClassCastException if the class of an element in the specified
  193. * collection prevents it from being added to this list.
  194. *
  195. * @throws IllegalArgumentException if some aspect of an element in the
  196. * specified collection prevents it from being added to this
  197. * list.
  198. *
  199. * @see #add(Object)
  200. */
  201. boolean addAll(Collection c);
  202. /**
  203. * Inserts all of the elements in the specified collection into this
  204. * list at the specified position (optional operation). Shifts the
  205. * element currently at that position (if any) and any subsequent
  206. * elements to the right (increases their indices). The new elements
  207. * will appear in this list in the order that they are returned by the
  208. * specified collection's iterator. The behavior of this operation is
  209. * unspecified if the specified collection is modified while the
  210. * operation is in progress. (Note that this will occur if the specified
  211. * collection is this list, and it's nonempty.)
  212. *
  213. * @param index index at which to insert first element from the specified
  214. * collection.
  215. * @param c elements to be inserted into this list.
  216. * @return <tt>true</tt> if this list changed as a result of the call.
  217. *
  218. * @throws UnsupportedOperationException if the <tt>addAll</tt> method is
  219. * not supported by this list.
  220. * @throws ClassCastException if the class of one of elements of the
  221. * specified collection prevents it from being added to this
  222. * list.
  223. * @throws IllegalArgumentException if some aspect of one of elements of
  224. * the specified collection prevents it from being added to
  225. * this list.
  226. * @throws IndexOutOfBoundsException if the index is out of range (index
  227. * < 0 || index > size()).
  228. */
  229. boolean addAll(int index, Collection c);
  230. /**
  231. * Removes from this list all the elements that are contained in the
  232. * specified collection (optional operation).
  233. *
  234. * @param c collection that defines which elements will be removed from
  235. * this list.
  236. * @return <tt>true</tt> if this list changed as a result of the call.
  237. *
  238. * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
  239. * is not supported by this list.
  240. *
  241. * @see #remove(Object)
  242. * @see #contains(Object)
  243. */
  244. boolean removeAll(Collection c);
  245. /**
  246. * Retains only the elements in this list that are contained in the
  247. * specified collection (optional operation). In other words, removes
  248. * from this list all the elements that are not contained in the specified
  249. * collection.
  250. *
  251. * @param c collection that defines which elements this set will retain.
  252. *
  253. * @return <tt>true</tt> if this list changed as a result of the call.
  254. *
  255. * @throws UnsupportedOperationException if the <tt>retainAll</tt> method
  256. * is not supported by this list.
  257. *
  258. * @see #remove(Object)
  259. * @see #contains(Object)
  260. */
  261. boolean retainAll(Collection c);
  262. /**
  263. * Removes all of the elements from this list (optional operation). This
  264. * list will be empty after this call returns (unless it throws an
  265. * exception).
  266. *
  267. * @throws UnsupportedOperationException if the <tt>clear</tt> method is
  268. * not supported by this list.
  269. */
  270. void clear();
  271. // Comparison and hashing
  272. /**
  273. * Compares the specified object with this list for equality. Returns
  274. * <tt>true</tt> if and only if the specified object is also a list, both
  275. * lists have the same size, and all corresponding pairs of elements in
  276. * the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and
  277. * <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null :
  278. * e1.equals(e2))</tt>.) In other words, two lists are defined to be
  279. * equal if they contain the same elements in the same order. This
  280. * definition ensures that the equals method works properly across
  281. * different implementations of the <tt>List</tt> interface.
  282. *
  283. * @param o the object to be compared for equality with this list.
  284. * @return <tt>true</tt> if the specified object is equal to this list.
  285. */
  286. boolean equals(Object o);
  287. /**
  288. * Returns the hash code value for this list. The hash code of a list
  289. * is defined to be the result of the following calculation:
  290. * <pre>
  291. * hashCode = 1;
  292. * Iterator i = list.iterator();
  293. * while (i.hasNext()) {
  294. * Object obj = i.next();
  295. * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
  296. * }
  297. * </pre>
  298. * This ensures that <tt>list1.equals(list2)</tt> implies that
  299. * <tt>list1.hashCode()==list2.hashCode()</tt> for any two lists,
  300. * <tt>list1</tt> and <tt>list2</tt>, as required by the general
  301. * contract of <tt>Object.hashCode</tt>.
  302. *
  303. * @return the hash code value for this list.
  304. * @see Object#hashCode()
  305. * @see Object#equals(Object)
  306. * @see #equals(Object)
  307. */
  308. int hashCode();
  309. // Positional Access Operations
  310. /**
  311. * Returns the element at the specified position in this list.
  312. *
  313. * @param index index of element to return.
  314. * @return the element at the specified position in this list.
  315. *
  316. * @throws IndexOutOfBoundsException if the index is out of range (index
  317. * < 0 || index >= size()).
  318. */
  319. Object get(int index);
  320. /**
  321. * Replaces the element at the specified position in this list with the
  322. * specified element (optional operation).
  323. *
  324. * @param index index of element to replace.
  325. * @param element element to be stored at the specified position.
  326. * @return the element previously at the specified position.
  327. *
  328. * @throws UnsupportedOperationException if the <tt>set</tt> method is not
  329. * supported by this list.
  330. * @throws ClassCastException if the class of the specified element
  331. * prevents it from being added to this list.
  332. * @throws IllegalArgumentException if some aspect of the specified
  333. * element prevents it from being added to this list.
  334. * @throws IndexOutOfBoundsException if the index is out of range
  335. * (index < 0 || index >= size()). */
  336. Object set(int index, Object element);
  337. /**
  338. * Inserts the specified element at the specified position in this list
  339. * (optional operation). Shifts the element currently at that position
  340. * (if any) and any subsequent elements to the right (adds one to their
  341. * indices).
  342. *
  343. * @param index index at which the specified element is to be inserted.
  344. * @param element element to be inserted.
  345. *
  346. * @throws UnsupportedOperationException if the <tt>add</tt> method is not
  347. * supported by this list.
  348. * @throws ClassCastException if the class of the specified element
  349. * prevents it from being added to this list.
  350. * @throws IllegalArgumentException if some aspect of the specified
  351. * element prevents it from being added to this list.
  352. * @throws IndexOutOfBoundsException if the index is out of range
  353. * (index < 0 || index > size()).
  354. */
  355. void add(int index, Object element);
  356. /**
  357. * Removes the element at the specified position in this list (optional
  358. * operation). Shifts any subsequent elements to the left (subtracts one
  359. * from their indices). Returns the element that was removed from the
  360. * list.
  361. *
  362. * @param index the index of the element to removed.
  363. * @return the element previously at the specified position.
  364. *
  365. * @throws UnsupportedOperationException if the <tt>remove</tt> method is
  366. * not supported by this list.
  367. *
  368. * @throws IndexOutOfBoundsException if the index is out of range (index
  369. * < 0 || index >= size()).
  370. */
  371. Object remove(int index);
  372. // Search Operations
  373. /**
  374. * Returns the index in this list of the first occurrence of the specified
  375. * element, or -1 if this list does not contain this element.
  376. * More formally, returns the lowest index <tt>i</tt> such that
  377. * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
  378. * or -1 if there is no such index.
  379. *
  380. * @param o element to search for.
  381. * @return the index in this list of the first occurrence of the specified
  382. * element, or -1 if this list does not contain this element.
  383. */
  384. int indexOf(Object o);
  385. /**
  386. * Returns the index in this list of the last occurrence of the specified
  387. * element, or -1 if this list does not contain this element.
  388. * More formally, returns the highest index <tt>i</tt> such that
  389. * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
  390. * or -1 if there is no such index.
  391. *
  392. * @param o element to search for.
  393. * @return the index in this list of the last occurrence of the specified
  394. * element, or -1 if this list does not contain this element.
  395. */
  396. int lastIndexOf(Object o);
  397. // List Iterators
  398. /**
  399. * Returns a list iterator of the elements in this list (in proper
  400. * sequence).
  401. *
  402. * @return a list iterator of the elements in this list (in proper
  403. * sequence).
  404. */
  405. ListIterator listIterator();
  406. /**
  407. * Returns a list iterator of the elements in this list (in proper
  408. * sequence), starting at the specified position in this list. The
  409. * specified index indicates the first element that would be returned by
  410. * an initial call to the <tt>next</tt> method. An initial call to
  411. * the <tt>previous</tt> method would return the element with the
  412. * specified index minus one.
  413. *
  414. * @param index index of first element to be returned from the
  415. * list iterator (by a call to the <tt>next</tt> method).
  416. * @return a list iterator of the elements in this list (in proper
  417. * sequence), starting at the specified position in this list.
  418. * @throws IndexOutOfBoundsException if the index is out of range (index
  419. * < 0 || index > size()).
  420. */
  421. ListIterator listIterator(int index);
  422. // View
  423. /**
  424. *
  425. * Returns a view of the portion of this list between the specified
  426. * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. (If
  427. * <tt>fromIndex</tt> and <tt>toIndex</tt> are equal, the returned list is
  428. * empty.) The returned list is backed by this list, so changes in the
  429. * returned list are reflected in this list, and vice-versa. The returned
  430. * list supports all of the optional list operations supported by this
  431. * list.<p>
  432. *
  433. * This method eliminates the need for explicit range operations (of
  434. * the sort that commonly exist for arrays). Any operation that expects
  435. * a list can be used as a range operation by passing a subList view
  436. * instead of a whole list. For example, the following idiom
  437. * removes a range of elements from a list:
  438. * <pre>
  439. * list.subList(from, to).clear();
  440. * </pre>
  441. *
  442. * Similar idioms may be constructed for <tt>indexOf</tt> and
  443. * <tt>lastIndexOf</tt>, and all of the algorithms in the
  444. * <tt>Collections</tt> class can be applied to a subList.<p>
  445. *
  446. * The semantics of this list returned by this method become undefined if
  447. * the backing list (i.e., this list) is <i>structurally modified</i> in
  448. * any way other than via the returned list. (Structural modifications are
  449. * those that change the size of this list, or otherwise perturb it in such
  450. * a fashion that iterations in progress may yield incorrect results.)
  451. *
  452. * @param fromIndex low endpoint (inclusive) of the subList.
  453. * @param toKey high endpoint (exclusive) of the subList.
  454. * @return a view of the specified range within this list.
  455. *
  456. * @throws IndexOutOfBoundsException for an illegal endpoint index value
  457. * (fromIndex < 0 || toIndex > size || fromIndex > toIndex).
  458. */
  459. List subList(int fromIndex, int toIndex);
  460. }