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