1. /*
  2. * @(#)Set.java 1.29 03/01/23
  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. * 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. * <p>Some set implementations have restrictions on the elements that
  33. * they may contain. For example, some implementations prohibit null elements,
  34. * and some have restrictions on the types of their elements. Attempting to
  35. * add an ineligible element throws an unchecked exception, typically
  36. * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
  37. * to query the presence of an ineligible element may throw an exception,
  38. * or it may simply return false; some implementations will exhibit the former
  39. * behavior and some will exhibit the latter. More generally, attempting an
  40. * operation on an ineligible element whose completion would not result in
  41. * the insertion of an ineligible element into the set may throw an
  42. * exception or it may succeed, at the option of the implementation.
  43. * Such exceptions are marked as "optional" in the specification for this
  44. * interface.
  45. *
  46. * <p>This interface is a member of the
  47. * <a href="{@docRoot}/../guide/collections/index.html">
  48. * Java Collections Framework</a>.
  49. *
  50. * @author Josh Bloch
  51. * @version 1.29, 01/23/03
  52. * @see Collection
  53. * @see List
  54. * @see SortedSet
  55. * @see HashSet
  56. * @see TreeSet
  57. * @see AbstractSet
  58. * @see Collections#singleton(java.lang.Object)
  59. * @see Collections#EMPTY_SET
  60. * @since 1.2
  61. */
  62. public interface Set extends Collection {
  63. // Query Operations
  64. /**
  65. * Returns the number of elements in this set (its cardinality). If this
  66. * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  67. * <tt>Integer.MAX_VALUE</tt>.
  68. *
  69. * @return the number of elements in this set (its cardinality).
  70. */
  71. int size();
  72. /**
  73. * Returns <tt>true</tt> if this set contains no elements.
  74. *
  75. * @return <tt>true</tt> if this set contains no elements.
  76. */
  77. boolean isEmpty();
  78. /**
  79. * Returns <tt>true</tt> if this set contains the specified element. More
  80. * formally, returns <tt>true</tt> if and only if this set contains an
  81. * element <code>e</code> such that <code>(o==null ? e==null :
  82. * o.equals(e))</code>.
  83. *
  84. * @param o element whose presence in this set is to be tested.
  85. * @return <tt>true</tt> if this set contains the specified element.
  86. * @throws ClassCastException if the type of the specified element
  87. * is incompatible with this set (optional).
  88. * @throws NullPointerException if the specified element is null and this
  89. * set does not support null elements (optional).
  90. */
  91. boolean contains(Object o);
  92. /**
  93. * Returns an iterator over the elements in this set. The elements are
  94. * returned in no particular order (unless this set is an instance of some
  95. * class that provides a guarantee).
  96. *
  97. * @return an iterator over the elements in this set.
  98. */
  99. Iterator iterator();
  100. /**
  101. * Returns an array containing all of the elements in this set.
  102. * Obeys the general contract of the <tt>Collection.toArray</tt> method.
  103. *
  104. * @return an array containing all of the elements in this set.
  105. */
  106. Object[] toArray();
  107. /**
  108. * Returns an array containing all of the elements in this set; the
  109. * runtime type of the returned array is that of the specified array.
  110. * Obeys the general contract of the
  111. * <tt>Collection.toArray(Object[])</tt> method.
  112. *
  113. * @param a the array into which the elements of this set are to
  114. * be stored, if it is big enough; otherwise, a new array of the
  115. * same runtime type is allocated for this purpose.
  116. * @return an array containing the elements of this set.
  117. * @throws ArrayStoreException the runtime type of a is not a supertype
  118. * of the runtime type of every element in this set.
  119. * @throws NullPointerException if the specified array is <tt>null</tt>.
  120. */
  121. Object[] toArray(Object a[]);
  122. // Modification Operations
  123. /**
  124. * Adds the specified element to this set if it is not already present
  125. * (optional operation). More formally, adds the specified element,
  126. * <code>o</code>, to this set if this set contains no element
  127. * <code>e</code> such that <code>(o==null ? e==null :
  128. * o.equals(e))</code>. If this set already contains the specified
  129. * element, the call leaves this set unchanged and returns <tt>false</tt>.
  130. * In combination with the restriction on constructors, this ensures that
  131. * sets never contain duplicate elements.<p>
  132. *
  133. * The stipulation above does not imply that sets must accept all
  134. * elements; sets may refuse to add any particular element, including
  135. * <tt>null</tt>, and throwing an exception, as described in the
  136. * specification for <tt>Collection.add</tt>. Individual set
  137. * implementations should clearly document any restrictions on the the
  138. * elements that they may contain.
  139. *
  140. * @param o element to be added to this set.
  141. * @return <tt>true</tt> if this set did not already contain the specified
  142. * element.
  143. *
  144. * @throws UnsupportedOperationException if the <tt>add</tt> method is not
  145. * supported by this set.
  146. * @throws ClassCastException if the class of the specified element
  147. * prevents it from being added to this set.
  148. * @throws NullPointerException if the specified element is null and this
  149. * set does not support null elements.
  150. * @throws IllegalArgumentException if some aspect of the specified element
  151. * prevents it from being added to this set.
  152. */
  153. boolean add(Object o);
  154. /**
  155. * Removes the specified element from this set if it is present (optional
  156. * operation). More formally, removes an element <code>e</code> such that
  157. * <code>(o==null ? e==null : o.equals(e))</code>, if the set contains
  158. * such an element. Returns <tt>true</tt> if the set contained the
  159. * specified element (or equivalently, if the set changed as a result of
  160. * the call). (The set will not contain the specified element once the
  161. * call returns.)
  162. *
  163. * @param o object to be removed from this set, if present.
  164. * @return true if the set contained the specified element.
  165. * @throws ClassCastException if the type of the specified element
  166. * is incompatible with this set (optional).
  167. * @throws NullPointerException if the specified element is null and this
  168. * set does not support null elements (optional).
  169. * @throws UnsupportedOperationException if the <tt>remove</tt> method is
  170. * not supported by this set.
  171. */
  172. boolean remove(Object o);
  173. // Bulk Operations
  174. /**
  175. * Returns <tt>true</tt> if this set contains all of the elements of the
  176. * specified collection. If the specified collection is also a set, this
  177. * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
  178. *
  179. * @param c collection to be checked for containment in this set.
  180. * @return <tt>true</tt> if this set contains all of the elements of the
  181. * specified collection.
  182. * @throws ClassCastException if the types of one or more elements
  183. * in the specified collection are incompatible with this
  184. * set (optional).
  185. * @throws NullPointerException if the specified collection contains one
  186. * or more null elements and this set does not support null
  187. * elements (optional).
  188. * @throws NullPointerException if the specified collection is
  189. * <tt>null</tt>.
  190. * @see #contains(Object)
  191. */
  192. boolean containsAll(Collection c);
  193. /**
  194. * Adds all of the elements in the specified collection to this set if
  195. * they're not already present (optional operation). If the specified
  196. * collection is also a set, the <tt>addAll</tt> operation effectively
  197. * modifies this set so that its value is the <i>union</i> of the two
  198. * sets. The behavior of this operation is unspecified if the specified
  199. * collection is modified while the operation is in progress.
  200. *
  201. * @param c collection whose elements are to be added to this set.
  202. * @return <tt>true</tt> if this set changed as a result of the call.
  203. *
  204. * @throws UnsupportedOperationException if the <tt>addAll</tt> method is
  205. * not supported by this set.
  206. * @throws ClassCastException if the class of some element of the
  207. * specified collection prevents it from being added to this
  208. * set.
  209. * @throws NullPointerException if the specified collection contains one
  210. * or more null elements and this set does not support null
  211. * elements, or if the specified collection is <tt>null</tt>.
  212. * @throws IllegalArgumentException if some aspect of some element of the
  213. * specified collection prevents it from being added to this
  214. * set.
  215. * @see #add(Object)
  216. */
  217. boolean addAll(Collection c);
  218. /**
  219. * Retains only the elements in this set that are contained in the
  220. * specified collection (optional operation). In other words, removes
  221. * from this set all of its elements that are not contained in the
  222. * specified collection. If the specified collection is also a set, this
  223. * operation effectively modifies this set so that its value is the
  224. * <i>intersection</i> of the two sets.
  225. *
  226. * @param c collection that defines which elements this set will retain.
  227. * @return <tt>true</tt> if this collection changed as a result of the
  228. * call.
  229. * @throws UnsupportedOperationException if the <tt>retainAll</tt> method
  230. * is not supported by this Collection.
  231. * @throws ClassCastException if the types of one or more elements in this
  232. * set are incompatible with the specified collection
  233. * (optional).
  234. * @throws NullPointerException if this set contains a null element and
  235. * the specified collection does not support null elements
  236. * (optional).
  237. * @throws NullPointerException if the specified collection is
  238. * <tt>null</tt>.
  239. * @see #remove(Object)
  240. */
  241. boolean retainAll(Collection c);
  242. /**
  243. * Removes from this set all of its elements that are contained in the
  244. * specified collection (optional operation). If the specified
  245. * collection is also a set, this operation effectively modifies this
  246. * set so that its value is the <i>asymmetric set difference</i> of
  247. * the two sets.
  248. *
  249. * @param c collection that defines which elements will be removed from
  250. * this set.
  251. * @return <tt>true</tt> if this set changed as a result of the call.
  252. *
  253. * @throws UnsupportedOperationException if the <tt>removeAll</tt>
  254. * method is not supported by this Collection.
  255. * @throws ClassCastException if the types of one or more elements in this
  256. * set are incompatible with the specified collection
  257. * (optional).
  258. * @throws NullPointerException if this set contains a null element and
  259. * the specified collection does not support null elements
  260. * (optional).
  261. * @throws NullPointerException if the specified collection is
  262. * <tt>null</tt>.
  263. * @see #remove(Object)
  264. */
  265. boolean removeAll(Collection c);
  266. /**
  267. * Removes all of the elements from this set (optional operation).
  268. * This set will be empty after this call returns (unless it throws an
  269. * exception).
  270. *
  271. * @throws UnsupportedOperationException if the <tt>clear</tt> method
  272. * is not supported by this set.
  273. */
  274. void clear();
  275. // Comparison and hashing
  276. /**
  277. * Compares the specified object with this set for equality. Returns
  278. * <tt>true</tt> if the specified object is also a set, the two sets
  279. * have the same size, and every member of the specified set is
  280. * contained in this set (or equivalently, every member of this set is
  281. * contained in the specified set). This definition ensures that the
  282. * equals method works properly across different implementations of the
  283. * set interface.
  284. *
  285. * @param o Object to be compared for equality with this set.
  286. * @return <tt>true</tt> if the specified Object is equal to this set.
  287. */
  288. boolean equals(Object o);
  289. /**
  290. *
  291. * Returns the hash code value for this set. The hash code of a set is
  292. * defined to be the sum of the hash codes of the elements in the set,
  293. * where the hashcode of a <tt>null</tt> element is defined to be zero.
  294. * This ensures that <code>s1.equals(s2)</code> implies that
  295. * <code>s1.hashCode()==s2.hashCode()</code> for any two sets
  296. * <code>s1</code> and <code>s2</code>, as required by the general
  297. * contract of the <tt>Object.hashCode</tt> method.
  298. *
  299. * @return the hash code value for this set.
  300. * @see Object#hashCode()
  301. * @see Object#equals(Object)
  302. * @see Set#equals(Object)
  303. */
  304. int hashCode();
  305. }