1. /*
  2. * @(#)Set.java 1.23 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. * A collection that contains no duplicate elements. More formally, sets
  13. * contain no pair of elements <code>e1</code> and <code>e2</code> such that
  14. * <code>e1.equals(e2)</code>, and at most one null element. As implied by
  15. * its name, this interface models the mathematical <i>set</i> abstraction.<p>
  16. *
  17. * The <tt>Set</tt> interface places additional stipulations, beyond those
  18. * inherited from the <tt>Collection</tt> interface, on the contracts of all
  19. * constructors and on the contracts of the <tt>add</tt>, <tt>equals</tt> and
  20. * <tt>hashCode</tt> methods. Declarations for other inherited methods are
  21. * also included here for convenience. (The specifications accompanying these
  22. * declarations have been tailored to the <tt>Set</tt> interface, but they do
  23. * not contain any additional stipulations.)<p>
  24. *
  25. * The additional stipulation on constructors is, not surprisingly,
  26. * that all constructors must create a set that contains no duplicate elements
  27. * (as defined above).<p>
  28. *
  29. * Note: Great care must be exercised if mutable objects are used as set
  30. * elements. The behavior of a set is not specified if the value of an object
  31. * is changed in a manner that affects equals comparisons while the object is
  32. * an element in the set. A special case of this prohibition is that it is
  33. * not permissible for a set to contain itself as an element.
  34. *
  35. * @author Josh Bloch
  36. * @version 1.23, 02/02/00
  37. * @see Collection
  38. * @see List
  39. * @see SortedSet
  40. * @see HashSet
  41. * @see TreeSet
  42. * @see AbstractSet
  43. * @see Collections#singleton(java.lang.Object)
  44. * @see Collections#EMPTY_SET
  45. * @since 1.2
  46. */
  47. public interface Set extends Collection {
  48. // Query Operations
  49. /**
  50. * Returns the number of elements in this set (its cardinality). If this
  51. * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  52. * <tt>Integer.MAX_VALUE</tt>.
  53. *
  54. * @return the number of elements in this set (its cardinality).
  55. */
  56. int size();
  57. /**
  58. * Returns <tt>true</tt> if this set contains no elements.
  59. *
  60. * @return <tt>true</tt> if this set contains no elements.
  61. */
  62. boolean isEmpty();
  63. /**
  64. * Returns <tt>true</tt> if this set contains the specified element. More
  65. * formally, returns <tt>true</tt> if and only if this set contains an
  66. * element <code>e</code> such that <code>(o==null ? e==null :
  67. * o.equals(e))</code>.
  68. *
  69. * @param o element whose presence in this set is to be tested.
  70. * @return <tt>true</tt> if this set contains the specified element.
  71. */
  72. boolean contains(Object o);
  73. /**
  74. * Returns an iterator over the elements in this set. The elements are
  75. * returned in no particular order (unless this set is an instance of some
  76. * class that provides a guarantee).
  77. *
  78. * @return an iterator over the elements in this set.
  79. */
  80. Iterator iterator();
  81. /**
  82. * Returns an array containing all of the elements in this set.
  83. * Obeys the general contract of the <tt>Collection.toArray</tt> method.
  84. *
  85. * @return an array containing all of the elements in this set.
  86. */
  87. Object[] toArray();
  88. /**
  89. * Returns an array containing all of the elements in this set whose
  90. * runtime type is that of the specified array. Obeys the general
  91. * contract of the <tt>Collection.toArray(Object[])</tt> method.
  92. *
  93. * @param a the array into which the elements of this set are to
  94. * be stored, if it is big enough; otherwise, a new array of the
  95. * same runtime type is allocated for this purpose.
  96. * @return an array containing the elements of this set.
  97. * @throws ArrayStoreException the runtime type of a is not a supertype
  98. * of the runtime type of every element in this set.
  99. */
  100. Object[] toArray(Object a[]);
  101. // Modification Operations
  102. /**
  103. * Adds the specified element to this set if it is not already present
  104. * (optional operation). More formally, adds the specified element,
  105. * <code>o</code>, to this set if this set contains no element
  106. * <code>e</code> such that <code>(o==null ? e==null :
  107. * o.equals(e))</code>. If this set already contains the specified
  108. * element, the call leaves this set unchanged and returns <tt>false</tt>.
  109. * In combination with the restriction on constructors, this ensures that
  110. * sets never contain duplicate elements.<p>
  111. *
  112. * The stipulation above does not imply that sets must accept all
  113. * elements; sets may refuse to add any particular element, including
  114. * <tt>null</tt>, and throwing an exception, as described in the
  115. * specification for <tt>Collection.add</tt>. Individual set
  116. * implementations should clearly document any restrictions on the the
  117. * elements that they may contain.
  118. *
  119. * @param o element to be added to this set.
  120. * @return <tt>true</tt> if this set did not already contain the specified
  121. * element.
  122. *
  123. * @throws UnsupportedOperationException if the <tt>add</tt> method is not
  124. * supported by this set.
  125. * @throws ClassCastException if the class of the specified element
  126. * prevents it from being added to this set.
  127. * @throws IllegalArgumentException if some aspect of this element
  128. * prevents it from being added to this set.
  129. */
  130. boolean add(Object o);
  131. /**
  132. * Removes the specified element from this set if it is present (optional
  133. * operation). More formally, removes an element <code>e</code> such that
  134. * <code>(o==null ? e==null : o.equals(e))</code>, if the set contains
  135. * such an element. Returns <tt>true</tt> if the set contained the
  136. * specified element (or equivalently, if the set changed as a result of
  137. * the call). (The set will not contain the specified element once the
  138. * call returns.)
  139. *
  140. * @param o object to be removed from this set, if present.
  141. * @return true if the set contained the specified element.
  142. * @throws UnsupportedOperationException if the <tt>remove</tt> method is
  143. * not supported by this set.
  144. */
  145. boolean remove(Object o);
  146. // Bulk Operations
  147. /**
  148. * Returns <tt>true</tt> if this set contains all of the elements of the
  149. * specified collection. If the specified collection is also a set, this
  150. * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
  151. *
  152. * @param c collection to be checked for containment in this set.
  153. * @return <tt>true</tt> if this set contains all of the elements of the
  154. * specified collection.
  155. */
  156. boolean containsAll(Collection c);
  157. /**
  158. * Adds all of the elements in the specified collection to this set if
  159. * they're not already present (optional operation). If the specified
  160. * collection is also a set, the <tt>addAll</tt> operation effectively
  161. * modifies this set so that its value is the <i>union</i> of the two
  162. * sets. The behavior of this operation is unspecified if the specified
  163. * collection is modified while the operation is in progress.
  164. *
  165. * @param c collection whose elements are to be added to this set.
  166. * @return <tt>true</tt> if this set changed as a result of the call.
  167. *
  168. * @throws UnsupportedOperationException if the <tt>addAll</tt> method is
  169. * not supported by this set.
  170. * @throws ClassCastException if the class of some element of the
  171. * specified collection prevents it from being added to this
  172. * set.
  173. * @throws IllegalArgumentException if some aspect of some element of the
  174. * specified collection prevents it from being added to this
  175. * set.
  176. * @see #add(Object)
  177. */
  178. boolean addAll(Collection c);
  179. /**
  180. * Retains only the elements in this set that are contained in the
  181. * specified collection (optional operation). In other words, removes
  182. * from this set all of its elements that are not contained in the
  183. * specified collection. If the specified collection is also a set, this
  184. * operation effectively modifies this set so that its value is the
  185. * <i>intersection</i> of the two sets.
  186. *
  187. * @param c collection that defines which elements this set will retain.
  188. * @return <tt>true</tt> if this collection changed as a result of the
  189. * call.
  190. * @throws UnsupportedOperationException if the <tt>retainAll</tt> method
  191. * is not supported by this Collection.
  192. *
  193. * @see #remove(Object)
  194. */
  195. boolean retainAll(Collection c);
  196. /**
  197. * Removes from this set all of its elements that are contained in the
  198. * specified collection (optional operation). If the specified
  199. * collection is also a set, this operation effectively modifies this
  200. * set so that its value is the <i>asymmetric set difference</i> of
  201. * the two sets.
  202. *
  203. * @param c collection that defines which elements will be removed from
  204. * this set.
  205. * @return <tt>true</tt> if this set changed as a result of the call.
  206. *
  207. * @throws UnsupportedOperationException if the <tt>removeAll</tt>
  208. * method is not supported by this Collection.
  209. *
  210. * @see #remove(Object) */
  211. boolean removeAll(Collection c);
  212. /**
  213. * Removes all of the elements from this set (optional operation).
  214. * This set will be empty after this call returns (unless it throws an
  215. * exception).
  216. *
  217. * @throws UnsupportedOperationException if the <tt>clear</tt> method
  218. * is not supported by this set.
  219. */
  220. void clear();
  221. // Comparison and hashing
  222. /**
  223. * Compares the specified object with this set for equality. Returns
  224. * <tt>true</tt> if the specified object is also a set, the two sets
  225. * have the same size, and every member of the specified set is
  226. * contained in this set (or equivalently, every member of this set is
  227. * contained in the specified set). This definition ensures that the
  228. * equals method works properly across different implementations of the
  229. * set interface.
  230. *
  231. * @param o Object to be compared for equality with this set.
  232. * @return <tt>true</tt> if the specified Object is equal to this set.
  233. */
  234. boolean equals(Object o);
  235. /**
  236. *
  237. * Returns the hash code value for this set. The hash code of a set is
  238. * defined to be the sum of the hash codes of the elements in the set,
  239. * where the hashcode of a <tt>null</tt> element is defined to be zero.
  240. * This ensures that <code>s1.equals(s2)</code> implies that
  241. * <code>s1.hashCode()==s2.hashCode()</code> for any two sets
  242. * <code>s1</code> and <code>s2</code>, as required by the general
  243. * contract of the <tt>Object.hashCode</tt> method.
  244. *
  245. * @return the hash code value for this set.
  246. * @see Object#hashCode()
  247. * @see Object#equals(Object)
  248. * @see Set#equals(Object)
  249. */
  250. int hashCode();
  251. }