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