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