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