1. /*
  2. * @(#)Collection.java 1.39 03/01/17
  3. *
  4. * Copyright 2003 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 SDK 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. * @author Josh Bloch
  62. * @version 1.40, 01/23/03
  63. * @see Set
  64. * @see List
  65. * @see Map
  66. * @see SortedSet
  67. * @see SortedMap
  68. * @see HashSet
  69. * @see TreeSet
  70. * @see ArrayList
  71. * @see LinkedList
  72. * @see Vector
  73. * @see Collections
  74. * @see Arrays
  75. * @see AbstractCollection
  76. * @since 1.2
  77. */
  78. public interface Collection {
  79. // Query Operations
  80. /**
  81. * Returns the number of elements in this collection. If this collection
  82. * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  83. * <tt>Integer.MAX_VALUE</tt>.
  84. *
  85. * @return the number of elements in this collection
  86. */
  87. int size();
  88. /**
  89. * Returns <tt>true</tt> if this collection contains no elements.
  90. *
  91. * @return <tt>true</tt> if this collection contains no elements
  92. */
  93. boolean isEmpty();
  94. /**
  95. * Returns <tt>true</tt> if this collection contains the specified
  96. * element. More formally, returns <tt>true</tt> if and only if this
  97. * collection contains at least one element <tt>e</tt> such that
  98. * <tt>(o==null ? e==null : o.equals(e))</tt>.
  99. *
  100. * @param o element whose presence in this collection is to be tested.
  101. * @return <tt>true</tt> if this collection contains the specified
  102. * element
  103. * @throws ClassCastException if the type of the specified element
  104. * is incompatible with this collection (optional).
  105. * @throws NullPointerException if the specified element is null and this
  106. * collection does not support null elements (optional).
  107. */
  108. boolean contains(Object o);
  109. /**
  110. * Returns an iterator over the elements in this collection. There are no
  111. * guarantees concerning the order in which the elements are returned
  112. * (unless this collection is an instance of some class that provides a
  113. * guarantee).
  114. *
  115. * @return an <tt>Iterator</tt> over the elements in this collection
  116. */
  117. Iterator iterator();
  118. /**
  119. * Returns an array containing all of the elements in this collection. If
  120. * the collection makes any guarantees as to what order its elements are
  121. * returned by its iterator, this method must return the elements in the
  122. * same order.<p>
  123. *
  124. * The returned array will be "safe" in that no references to it are
  125. * maintained by this collection. (In other words, this method must
  126. * allocate a new array even if this collection is backed by an array).
  127. * The caller is thus free to modify the returned array.<p>
  128. *
  129. * This method acts as bridge between array-based and collection-based
  130. * APIs.
  131. *
  132. * @return an array containing all of the elements in this collection
  133. */
  134. Object[] toArray();
  135. /**
  136. * Returns an array containing all of the elements in this collection;
  137. * the runtime type of the returned array is that of the specified array.
  138. * If the collection fits in the specified array, it is returned therein.
  139. * Otherwise, a new array is allocated with the runtime type of the
  140. * specified array and the size of this collection.<p>
  141. *
  142. * If this collection fits in the specified array with room to spare
  143. * (i.e., the array has more elements than this collection), the element
  144. * in the array immediately following the end of the collection is set to
  145. * <tt>null</tt>. This is useful in determining the length of this
  146. * collection <i>only</i> if the caller knows that this collection does
  147. * not contain any <tt>null</tt> elements.)<p>
  148. *
  149. * If this collection makes any guarantees as to what order its elements
  150. * are returned by its iterator, this method must return the elements in
  151. * the same order.<p>
  152. *
  153. * Like the <tt>toArray</tt> method, this method acts as bridge between
  154. * array-based and collection-based APIs. Further, this method allows
  155. * precise control over the runtime type of the output array, and may,
  156. * under certain circumstances, be used to save allocation costs<p>
  157. *
  158. * Suppose <tt>l</tt> is a <tt>List</tt> known to contain only strings.
  159. * The following code can be used to dump the list into a newly allocated
  160. * array of <tt>String</tt>:
  161. *
  162. * <pre>
  163. * String[] x = (String[]) v.toArray(new String[0]);
  164. * </pre><p>
  165. *
  166. * Note that <tt>toArray(new Object[0])</tt> is identical in function to
  167. * <tt>toArray()</tt>.
  168. *
  169. * @param a the array into which the elements of this collection are to be
  170. * stored, if it is big enough; otherwise, a new array of the same
  171. * runtime type is allocated for this purpose.
  172. * @return an array containing the elements of this collection
  173. *
  174. * @throws ArrayStoreException the runtime type of the specified array is
  175. * not a supertype of the runtime type of every element in this
  176. * collection.
  177. * @throws NullPointerException if the specified array is <tt>null</tt>.
  178. */
  179. Object[] toArray(Object a[]);
  180. // Modification Operations
  181. /**
  182. * Ensures that this collection contains the specified element (optional
  183. * operation). Returns <tt>true</tt> if this collection changed as a
  184. * result of the call. (Returns <tt>false</tt> if this collection does
  185. * not permit duplicates and already contains the specified element.)<p>
  186. *
  187. * Collections that support this operation may place limitations on what
  188. * elements may be added to this collection. In particular, some
  189. * collections will refuse to add <tt>null</tt> elements, and others will
  190. * impose restrictions on the type of elements that may be added.
  191. * Collection classes should clearly specify in their documentation any
  192. * restrictions on what elements may be added.<p>
  193. *
  194. * If a collection refuses to add a particular element for any reason
  195. * other than that it already contains the element, it <i>must</i> throw
  196. * an exception (rather than returning <tt>false</tt>). This preserves
  197. * the invariant that a collection always contains the specified element
  198. * after this call returns.
  199. *
  200. * @param o element whose presence in this collection is to be ensured.
  201. * @return <tt>true</tt> if this collection changed as a result of the
  202. * call
  203. *
  204. * @throws UnsupportedOperationException <tt>add</tt> is not supported by
  205. * this collection.
  206. * @throws ClassCastException class of the specified element prevents it
  207. * from being added to this collection.
  208. * @throws NullPointerException if the specified element is null and this
  209. * collection does not support null elements.
  210. * @throws IllegalArgumentException some aspect of this element prevents
  211. * it from being added to this collection.
  212. */
  213. boolean add(Object o);
  214. /**
  215. * Removes a single instance of the specified element from this
  216. * collection, if it is present (optional operation). More formally,
  217. * removes an element <tt>e</tt> such that <tt>(o==null ? e==null :
  218. * o.equals(e))</tt>, if this collection contains one or more such
  219. * elements. Returns true if this collection contained the specified
  220. * element (or equivalently, if this collection changed as a result of the
  221. * call).
  222. *
  223. * @param o element to be removed from this collection, if present.
  224. * @return <tt>true</tt> if this collection changed as a result of the
  225. * call
  226. *
  227. * @throws ClassCastException if the type of the specified element
  228. * is incompatible with this collection (optional).
  229. * @throws NullPointerException if the specified element is null and this
  230. * collection does not support null elements (optional).
  231. * @throws UnsupportedOperationException remove is not supported by this
  232. * collection.
  233. */
  234. boolean remove(Object o);
  235. // Bulk Operations
  236. /**
  237. * Returns <tt>true</tt> if this collection contains all of the elements
  238. * in the specified collection.
  239. *
  240. * @param c collection to be checked for containment in this collection.
  241. * @return <tt>true</tt> if this collection contains all of the elements
  242. * in the specified collection
  243. * @throws ClassCastException if the types of one or more elements
  244. * in the specified collection are incompatible with this
  245. * collection (optional).
  246. * @throws NullPointerException if the specified collection contains one
  247. * or more null elements and this collection does not support null
  248. * elements (optional).
  249. * @throws NullPointerException if the specified collection is
  250. * <tt>null</tt>.
  251. * @see #contains(Object)
  252. */
  253. boolean containsAll(Collection c);
  254. /**
  255. * Adds all of the elements in the specified collection to this collection
  256. * (optional operation). The behavior of this operation is undefined if
  257. * the specified collection is modified while the operation is in progress.
  258. * (This implies that the behavior of this call is undefined if the
  259. * specified collection is this collection, and this collection is
  260. * nonempty.)
  261. *
  262. * @param c elements to be inserted into this collection.
  263. * @return <tt>true</tt> if this collection changed as a result of the
  264. * call
  265. *
  266. * @throws UnsupportedOperationException if this collection does not
  267. * support the <tt>addAll</tt> method.
  268. * @throws ClassCastException if the class of an element of the specified
  269. * collection prevents it from being added to this collection.
  270. * @throws NullPointerException if the specified collection contains one
  271. * or more null elements and this collection does not support null
  272. * elements, or if the specified collection is <tt>null</tt>.
  273. * @throws IllegalArgumentException some aspect of an element of the
  274. * specified collection prevents it from being added to this
  275. * collection.
  276. * @see #add(Object)
  277. */
  278. boolean addAll(Collection c);
  279. /**
  280. *
  281. * Removes all this collection's elements that are also contained in the
  282. * specified collection (optional operation). After this call returns,
  283. * this collection will contain no elements in common with the specified
  284. * collection.
  285. *
  286. * @param c elements to be removed from this collection.
  287. * @return <tt>true</tt> if this collection changed as a result of the
  288. * call
  289. *
  290. * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
  291. * is not supported by this collection.
  292. * @throws ClassCastException if the types of one or more elements
  293. * in this collection are incompatible with the specified
  294. * collection (optional).
  295. * @throws NullPointerException if this collection contains one or more
  296. * null elements and the specified collection does not support
  297. * null elements (optional).
  298. * @throws NullPointerException if the specified collection is
  299. * <tt>null</tt>.
  300. * @see #remove(Object)
  301. * @see #contains(Object)
  302. */
  303. boolean removeAll(Collection c);
  304. /**
  305. * Retains only the elements in this collection that are contained in the
  306. * specified collection (optional operation). In other words, removes from
  307. * this collection all of its elements that are not contained in the
  308. * specified collection.
  309. *
  310. * @param c elements to be retained in this collection.
  311. * @return <tt>true</tt> if this collection changed as a result of the
  312. * call
  313. *
  314. * @throws UnsupportedOperationException if the <tt>retainAll</tt> method
  315. * is not supported by this Collection.
  316. * @throws ClassCastException if the types of one or more elements
  317. * in this collection are incompatible with the specified
  318. * collection (optional).
  319. * @throws NullPointerException if this collection contains one or more
  320. * null elements and the specified collection does not support null
  321. * elements (optional).
  322. * @throws NullPointerException if the specified collection is
  323. * <tt>null</tt>.
  324. * @see #remove(Object)
  325. * @see #contains(Object)
  326. */
  327. boolean retainAll(Collection c);
  328. /**
  329. * Removes all of the elements from this collection (optional operation).
  330. * This collection will be empty after this method returns unless it
  331. * throws an exception.
  332. *
  333. * @throws UnsupportedOperationException if the <tt>clear</tt> method is
  334. * not supported by this collection.
  335. */
  336. void clear();
  337. // Comparison and hashing
  338. /**
  339. * Compares the specified object with this collection for equality. <p>
  340. *
  341. * While the <tt>Collection</tt> interface adds no stipulations to the
  342. * general contract for the <tt>Object.equals</tt>, programmers who
  343. * implement the <tt>Collection</tt> interface "directly" (in other words,
  344. * create a class that is a <tt>Collection</tt> but is not a <tt>Set</tt>
  345. * or a <tt>List</tt>) must exercise care if they choose to override the
  346. * <tt>Object.equals</tt>. It is not necessary to do so, and the simplest
  347. * course of action is to rely on <tt>Object</tt>'s implementation, but
  348. * the implementer may wish to implement a "value comparison" in place of
  349. * the default "reference comparison." (The <tt>List</tt> and
  350. * <tt>Set</tt> interfaces mandate such value comparisons.)<p>
  351. *
  352. * The general contract for the <tt>Object.equals</tt> method states that
  353. * equals must be symmetric (in other words, <tt>a.equals(b)</tt> if and
  354. * only if <tt>b.equals(a)</tt>). The contracts for <tt>List.equals</tt>
  355. * and <tt>Set.equals</tt> state that lists are only equal to other lists,
  356. * and sets to other sets. Thus, a custom <tt>equals</tt> method for a
  357. * collection class that implements neither the <tt>List</tt> nor
  358. * <tt>Set</tt> interface must return <tt>false</tt> when this collection
  359. * is compared to any list or set. (By the same logic, it is not possible
  360. * to write a class that correctly implements both the <tt>Set</tt> and
  361. * <tt>List</tt> interfaces.)
  362. *
  363. * @param o Object to be compared for equality with this collection.
  364. * @return <tt>true</tt> if the specified object is equal to this
  365. * collection
  366. *
  367. * @see Object#equals(Object)
  368. * @see Set#equals(Object)
  369. * @see List#equals(Object)
  370. */
  371. boolean equals(Object o);
  372. /**
  373. * Returns the hash code value for this collection. While the
  374. * <tt>Collection</tt> interface adds no stipulations to the general
  375. * contract for the <tt>Object.hashCode</tt> method, programmers should
  376. * take note that any class that overrides the <tt>Object.equals</tt>
  377. * method must also override the <tt>Object.hashCode</tt> method in order
  378. * to satisfy the general contract for the <tt>Object.hashCode</tt>method.
  379. * In particular, <tt>c1.equals(c2)</tt> implies that
  380. * <tt>c1.hashCode()==c2.hashCode()</tt>.
  381. *
  382. * @return the hash code value for this collection
  383. *
  384. * @see Object#hashCode()
  385. * @see Object#equals(Object)
  386. */
  387. int hashCode();
  388. }