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