1. /*
  2. * @(#)Collection.java 1.49 04/06/28
  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. * The root interface in the <i>collection hierarchy</i>. A collection
  10. * represents a group of objects, known as its <i>elements</i>. Some
  11. * collections allow duplicate elements and others do not. Some are ordered
  12. * and others unordered. The JDK does not provide any <i>direct</i>
  13. * implementations of this interface: it provides implementations of more
  14. * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>. This interface
  15. * is typically used to pass collections around and manipulate them where
  16. * maximum generality is desired.
  17. *
  18. * <p><i>Bags</i> or <i>multisets</i> (unordered collections that may contain
  19. * duplicate elements) should implement this interface directly.
  20. *
  21. * <p>All general-purpose <tt>Collection</tt> implementation classes (which
  22. * typically implement <tt>Collection</tt> indirectly through one of its
  23. * subinterfaces) should provide two "standard" constructors: a void (no
  24. * arguments) constructor, which creates an empty collection, and a
  25. * constructor with a single argument of type <tt>Collection</tt>, which
  26. * creates a new collection with the same elements as its argument. In
  27. * effect, the latter constructor allows the user to copy any collection,
  28. * producing an equivalent collection of the desired implementation type.
  29. * There is no way to enforce this convention (as interfaces cannot contain
  30. * constructors) but all of the general-purpose <tt>Collection</tt>
  31. * implementations in the Java platform libraries comply.
  32. *
  33. * <p>The "destructive" methods contained in this interface, that is, the
  34. * methods that modify the collection on which they operate, are specified to
  35. * throw <tt>UnsupportedOperationException</tt> if this collection does not
  36. * support the operation. If this is the case, these methods may, but are not
  37. * required to, throw an <tt>UnsupportedOperationException</tt> if the
  38. * invocation would have no effect on the collection. For example, invoking
  39. * the {@link #addAll(Collection)} method on an unmodifiable collection may,
  40. * but is not required to, throw the exception if the collection to be added
  41. * is empty.
  42. *
  43. * <p>Some collection implementations have restrictions on the elements that
  44. * they may contain. For example, some implementations prohibit null elements,
  45. * and some have restrictions on the types of their elements. Attempting to
  46. * add an ineligible element throws an unchecked exception, typically
  47. * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
  48. * to query the presence of an ineligible element may throw an exception,
  49. * or it may simply return false; some implementations will exhibit the former
  50. * behavior and some will exhibit the latter. More generally, attempting an
  51. * operation on an ineligible element whose completion would not result in
  52. * the insertion of an ineligible element into the collection may throw an
  53. * exception or it may succeed, at the option of the implementation.
  54. * Such exceptions are marked as "optional" in the specification for this
  55. * interface.
  56. *
  57. * <p>This interface is a member of the
  58. * <a href="{@docRoot}/../guide/collections/index.html">
  59. * Java Collections Framework</a>.
  60. *
  61. * <p>Many methods in Collections Framework interfaces are defined in
  62. * terms of the {@link Object#equals(Object) equals} method. For example,
  63. * the specification for the {@link #contains(Object) contains(Object o)}
  64. * method says: "returns <tt>true</tt> if and only if this collection
  65. * contains at least one element <tt>e</tt> such that
  66. * <tt>(o==null ? e==null : o.equals(e))</tt>." This specification should
  67. * <i>not</i> be construed to imply that invoking <tt>Collection.contains</tt>
  68. * with a non-null argument <tt>o</tt> will cause <tt>o.equals(e)</tt> to be
  69. * invoked for any element <tt>e</tt>. Implementations are free to implement
  70. * optimizations whereby the <tt>equals</tt> invocation is avoided, for
  71. * example, by first comparing the hash codes of the two elements. (The
  72. * {@link Object#hashCode()} specification guarantees that two objects with
  73. * unequal hash codes cannot be equal.) More generally, implementations of
  74. * the various Collections Framework interfaces are free to take advantage of
  75. * the specified behavior of underlying {@link Object} methods wherever the
  76. * implementor deems it appropriate.
  77. *
  78. * @author Josh Bloch
  79. * @author Neal Gafter
  80. * @version 1.49, 06/28/04
  81. * @see Set
  82. * @see List
  83. * @see Map
  84. * @see SortedSet
  85. * @see SortedMap
  86. * @see HashSet
  87. * @see TreeSet
  88. * @see ArrayList
  89. * @see LinkedList
  90. * @see Vector
  91. * @see Collections
  92. * @see Arrays
  93. * @see AbstractCollection
  94. * @since 1.2
  95. */
  96. public interface Collection<E> extends Iterable<E> {
  97. // Query Operations
  98. /**
  99. * Returns the number of elements in this collection. If this collection
  100. * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  101. * <tt>Integer.MAX_VALUE</tt>.
  102. *
  103. * @return the number of elements in this collection
  104. */
  105. int size();
  106. /**
  107. * Returns <tt>true</tt> if this collection contains no elements.
  108. *
  109. * @return <tt>true</tt> if this collection contains no elements
  110. */
  111. boolean isEmpty();
  112. /**
  113. * Returns <tt>true</tt> if this collection contains the specified
  114. * element. More formally, returns <tt>true</tt> if and only if this
  115. * collection contains at least one element <tt>e</tt> such that
  116. * <tt>(o==null ? e==null : o.equals(e))</tt>.
  117. *
  118. * @param o element whose presence in this collection is to be tested.
  119. * @return <tt>true</tt> if this collection contains the specified
  120. * element
  121. * @throws ClassCastException if the type of the specified element
  122. * is incompatible with this collection (optional).
  123. * @throws NullPointerException if the specified element is null and this
  124. * collection does not support null elements (optional).
  125. */
  126. boolean contains(Object o);
  127. /**
  128. * Returns an iterator over the elements in this collection. There are no
  129. * guarantees concerning the order in which the elements are returned
  130. * (unless this collection is an instance of some class that provides a
  131. * guarantee).
  132. *
  133. * @return an <tt>Iterator</tt> over the elements in this collection
  134. */
  135. Iterator<E> iterator();
  136. /**
  137. * Returns an array containing all of the elements in this collection. If
  138. * the collection makes any guarantees as to what order its elements are
  139. * returned by its iterator, this method must return the elements in the
  140. * same order.<p>
  141. *
  142. * The returned array will be "safe" in that no references to it are
  143. * maintained by this collection. (In other words, this method must
  144. * allocate a new array even if this collection is backed by an array).
  145. * The caller is thus free to modify the returned array.<p>
  146. *
  147. * This method acts as bridge between array-based and collection-based
  148. * APIs.
  149. *
  150. * @return an array containing all of the elements in this collection
  151. */
  152. Object[] toArray();
  153. /**
  154. * Returns an array containing all of the elements in this collection;
  155. * the runtime type of the returned array is that of the specified array.
  156. * If the collection fits in the specified array, it is returned therein.
  157. * Otherwise, a new array is allocated with the runtime type of the
  158. * specified array and the size of this collection.<p>
  159. *
  160. * If this collection fits in the specified array with room to spare
  161. * (i.e., the array has more elements than this collection), the element
  162. * in the array immediately following the end of the collection is set to
  163. * <tt>null</tt>. This is useful in determining the length of this
  164. * collection <i>only</i> if the caller knows that this collection does
  165. * not contain any <tt>null</tt> elements.)<p>
  166. *
  167. * If this collection makes any guarantees as to what order its elements
  168. * are returned by its iterator, this method must return the elements in
  169. * the same order.<p>
  170. *
  171. * Like the <tt>toArray</tt> method, this method acts as bridge between
  172. * array-based and collection-based APIs. Further, this method allows
  173. * precise control over the runtime type of the output array, and may,
  174. * under certain circumstances, be used to save allocation costs<p>
  175. *
  176. * Suppose <tt>l</tt> is a <tt>List</tt> known to contain only strings.
  177. * The following code can be used to dump the list into a newly allocated
  178. * array of <tt>String</tt>:
  179. *
  180. * <pre>
  181. * String[] x = (String[]) v.toArray(new String[0]);
  182. * </pre><p>
  183. *
  184. * Note that <tt>toArray(new Object[0])</tt> is identical in function to
  185. * <tt>toArray()</tt>.
  186. *
  187. * @param a the array into which the elements of this collection are to be
  188. * stored, if it is big enough; otherwise, a new array of the same
  189. * runtime type is allocated for this purpose.
  190. * @return an array containing the elements of this collection
  191. *
  192. * @throws ArrayStoreException the runtime type of the specified array is
  193. * not a supertype of the runtime type of every element in this
  194. * collection.
  195. * @throws NullPointerException if the specified array is <tt>null</tt>.
  196. */
  197. <T> T[] toArray(T[] a);
  198. // Modification Operations
  199. /**
  200. * Ensures that this collection contains the specified element (optional
  201. * operation). Returns <tt>true</tt> if this collection changed as a
  202. * result of the call. (Returns <tt>false</tt> if this collection does
  203. * not permit duplicates and already contains the specified element.)<p>
  204. *
  205. * Collections that support this operation may place limitations on what
  206. * elements may be added to this collection. In particular, some
  207. * collections will refuse to add <tt>null</tt> elements, and others will
  208. * impose restrictions on the type of elements that may be added.
  209. * Collection classes should clearly specify in their documentation any
  210. * restrictions on what elements may be added.<p>
  211. *
  212. * If a collection refuses to add a particular element for any reason
  213. * other than that it already contains the element, it <i>must</i> throw
  214. * an exception (rather than returning <tt>false</tt>). This preserves
  215. * the invariant that a collection always contains the specified element
  216. * after this call returns.
  217. *
  218. * @param o element whose presence in this collection is to be ensured.
  219. * @return <tt>true</tt> if this collection changed as a result of the
  220. * call
  221. *
  222. * @throws UnsupportedOperationException <tt>add</tt> is not supported by
  223. * this collection.
  224. * @throws ClassCastException class of the specified element prevents it
  225. * from being added to this collection.
  226. * @throws NullPointerException if the specified element is null and this
  227. * collection does not support null elements.
  228. * @throws IllegalArgumentException some aspect of this element prevents
  229. * it from being added to this collection.
  230. */
  231. boolean add(E o);
  232. /**
  233. * Removes a single instance of the specified element from this
  234. * collection, if it is present (optional operation). More formally,
  235. * removes an element <tt>e</tt> such that <tt>(o==null ? e==null :
  236. * o.equals(e))</tt>, if this collection contains one or more such
  237. * elements. Returns true if this collection contained the specified
  238. * element (or equivalently, if this collection changed as a result of the
  239. * call).
  240. *
  241. * @param o element to be removed from this collection, if present.
  242. * @return <tt>true</tt> if this collection changed as a result of the
  243. * call
  244. *
  245. * @throws ClassCastException if the type of the specified element
  246. * is incompatible with this collection (optional).
  247. * @throws NullPointerException if the specified element is null and this
  248. * collection does not support null elements (optional).
  249. * @throws UnsupportedOperationException remove is not supported by this
  250. * collection.
  251. */
  252. boolean remove(Object o);
  253. // Bulk Operations
  254. /**
  255. * Returns <tt>true</tt> if this collection contains all of the elements
  256. * in the specified collection.
  257. *
  258. * @param c collection to be checked for containment in this collection.
  259. * @return <tt>true</tt> if this collection contains all of the elements
  260. * in the specified collection
  261. * @throws ClassCastException if the types of one or more elements
  262. * in the specified collection are incompatible with this
  263. * collection (optional).
  264. * @throws NullPointerException if the specified collection contains one
  265. * or more null elements and this collection does not support null
  266. * elements (optional).
  267. * @throws NullPointerException if the specified collection is
  268. * <tt>null</tt>.
  269. * @see #contains(Object)
  270. */
  271. boolean containsAll(Collection<?> c);
  272. /**
  273. * Adds all of the elements in the specified collection to this collection
  274. * (optional operation). The behavior of this operation is undefined if
  275. * the specified collection is modified while the operation is in progress.
  276. * (This implies that the behavior of this call is undefined if the
  277. * specified collection is this collection, and this collection is
  278. * nonempty.)
  279. *
  280. * @param c elements to be inserted into this collection.
  281. * @return <tt>true</tt> if this collection changed as a result of the
  282. * call
  283. *
  284. * @throws UnsupportedOperationException if this collection does not
  285. * support the <tt>addAll</tt> method.
  286. * @throws ClassCastException if the class of an element of the specified
  287. * collection prevents it from being added to this collection.
  288. * @throws NullPointerException if the specified collection contains one
  289. * or more null elements and this collection does not support null
  290. * elements, or if the specified collection is <tt>null</tt>.
  291. * @throws IllegalArgumentException some aspect of an element of the
  292. * specified collection prevents it from being added to this
  293. * collection.
  294. * @see #add(Object)
  295. */
  296. boolean addAll(Collection<? extends E> c);
  297. /**
  298. *
  299. * Removes all this collection's elements that are also contained in the
  300. * specified collection (optional operation). After this call returns,
  301. * this collection will contain no elements in common with the specified
  302. * collection.
  303. *
  304. * @param c elements to be removed from this collection.
  305. * @return <tt>true</tt> if this collection changed as a result of the
  306. * call
  307. *
  308. * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
  309. * is not supported by this collection.
  310. * @throws ClassCastException if the types of one or more elements
  311. * in this collection are incompatible with the specified
  312. * collection (optional).
  313. * @throws NullPointerException if this collection contains one or more
  314. * null elements and the specified collection does not support
  315. * null elements (optional).
  316. * @throws NullPointerException if the specified collection is
  317. * <tt>null</tt>.
  318. * @see #remove(Object)
  319. * @see #contains(Object)
  320. */
  321. boolean removeAll(Collection<?> c);
  322. /**
  323. * Retains only the elements in this collection that are contained in the
  324. * specified collection (optional operation). In other words, removes from
  325. * this collection all of its elements that are not contained in the
  326. * specified collection.
  327. *
  328. * @param c elements to be retained in this collection.
  329. * @return <tt>true</tt> if this collection changed as a result of the
  330. * call
  331. *
  332. * @throws UnsupportedOperationException if the <tt>retainAll</tt> method
  333. * is not supported by this Collection.
  334. * @throws ClassCastException if the types of one or more elements
  335. * in this collection are incompatible with the specified
  336. * collection (optional).
  337. * @throws NullPointerException if this collection contains one or more
  338. * null elements and the specified collection does not support null
  339. * elements (optional).
  340. * @throws NullPointerException if the specified collection is
  341. * <tt>null</tt>.
  342. * @see #remove(Object)
  343. * @see #contains(Object)
  344. */
  345. boolean retainAll(Collection<?> c);
  346. /**
  347. * Removes all of the elements from this collection (optional operation).
  348. * This collection will be empty after this method returns unless it
  349. * throws an exception.
  350. *
  351. * @throws UnsupportedOperationException if the <tt>clear</tt> method is
  352. * not supported by this collection.
  353. */
  354. void clear();
  355. // Comparison and hashing
  356. /**
  357. * Compares the specified object with this collection for equality. <p>
  358. *
  359. * While the <tt>Collection</tt> interface adds no stipulations to the
  360. * general contract for the <tt>Object.equals</tt>, programmers who
  361. * implement the <tt>Collection</tt> interface "directly" (in other words,
  362. * create a class that is a <tt>Collection</tt> but is not a <tt>Set</tt>
  363. * or a <tt>List</tt>) must exercise care if they choose to override the
  364. * <tt>Object.equals</tt>. It is not necessary to do so, and the simplest
  365. * course of action is to rely on <tt>Object</tt>'s implementation, but
  366. * the implementer may wish to implement a "value comparison" in place of
  367. * the default "reference comparison." (The <tt>List</tt> and
  368. * <tt>Set</tt> interfaces mandate such value comparisons.)<p>
  369. *
  370. * The general contract for the <tt>Object.equals</tt> method states that
  371. * equals must be symmetric (in other words, <tt>a.equals(b)</tt> if and
  372. * only if <tt>b.equals(a)</tt>). The contracts for <tt>List.equals</tt>
  373. * and <tt>Set.equals</tt> state that lists are only equal to other lists,
  374. * and sets to other sets. Thus, a custom <tt>equals</tt> method for a
  375. * collection class that implements neither the <tt>List</tt> nor
  376. * <tt>Set</tt> interface must return <tt>false</tt> when this collection
  377. * is compared to any list or set. (By the same logic, it is not possible
  378. * to write a class that correctly implements both the <tt>Set</tt> and
  379. * <tt>List</tt> interfaces.)
  380. *
  381. * @param o Object to be compared for equality with this collection.
  382. * @return <tt>true</tt> if the specified object is equal to this
  383. * collection
  384. *
  385. * @see Object#equals(Object)
  386. * @see Set#equals(Object)
  387. * @see List#equals(Object)
  388. */
  389. boolean equals(Object o);
  390. /**
  391. * Returns the hash code value for this collection. While the
  392. * <tt>Collection</tt> interface adds no stipulations to the general
  393. * contract for the <tt>Object.hashCode</tt> method, programmers should
  394. * take note that any class that overrides the <tt>Object.equals</tt>
  395. * method must also override the <tt>Object.hashCode</tt> method in order
  396. * to satisfy the general contract for the <tt>Object.hashCode</tt>method.
  397. * In particular, <tt>c1.equals(c2)</tt> implies that
  398. * <tt>c1.hashCode()==c2.hashCode()</tt>.
  399. *
  400. * @return the hash code value for this collection
  401. *
  402. * @see Object#hashCode()
  403. * @see Object#equals(Object)
  404. */
  405. int hashCode();
  406. }