Set.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Set.java -- A collection that prohibits duplicates
  2. Copyright (C) 1998, 2001, 2004, 2005
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. /**
  34. * A collection that contains no duplicates. In other words, for two set
  35. * elements e1 and e2, <code>e1.equals(e2)</code> returns false. There
  36. * are additional stipulations on <code>add</code>, <code>equals</code>
  37. * and <code>hashCode</code>, as well as the requirements that constructors
  38. * do not permit duplicate elements. The Set interface is incompatible with
  39. * List; you cannot implement both simultaneously.
  40. * <p>
  41. *
  42. * Note: Be careful about using mutable objects in sets. In particular,
  43. * if a mutable object changes to become equal to another set element, you
  44. * have violated the contract. As a special case of this, a Set is not
  45. * allowed to be an element of itself, without risking undefined behavior.
  46. *
  47. * @author Original author unknown
  48. * @author Eric Blake (ebb9@email.byu.edu)
  49. * @see Collection
  50. * @see List
  51. * @see SortedSet
  52. * @see HashSet
  53. * @see TreeSet
  54. * @see LinkedHashSet
  55. * @see AbstractSet
  56. * @see Collections#singleton(Object)
  57. * @see Collections#EMPTY_SET
  58. * @since 1.2
  59. * @status updated to 1.4
  60. */
  61. public interface Set<E> extends Collection<E>
  62. {
  63. /**
  64. * Adds the specified element to the set if it is not already present
  65. * (optional operation). In particular, the comparison algorithm is
  66. * <code>o == null ? e == null : o.equals(e)</code>. Sets need not permit
  67. * all values, and may document what exceptions will be thrown if
  68. * a value is not permitted.
  69. *
  70. * @param o the object to add
  71. * @return true if the object was not previously in the set
  72. * @throws UnsupportedOperationException if this operation is not allowed
  73. * @throws ClassCastException if the class of o prevents it from being added
  74. * @throws IllegalArgumentException if some aspect of o prevents it from
  75. * being added
  76. * @throws NullPointerException if null is not permitted in this set
  77. */
  78. boolean add(E o);
  79. /**
  80. * Adds all of the elements of the given collection to this set (optional
  81. * operation). If the argument is also a Set, this returns the mathematical
  82. * <i>union</i> of the two. The behavior is unspecified if the set is
  83. * modified while this is taking place.
  84. *
  85. * @param c the collection to add
  86. * @return true if the set changed as a result
  87. * @throws UnsupportedOperationException if this operation is not allowed
  88. * @throws ClassCastException if the class of an element prevents it from
  89. * being added
  90. * @throws IllegalArgumentException if something about an element prevents
  91. * it from being added
  92. * @throws NullPointerException if null is not permitted in this set, or
  93. * if the argument c is null
  94. * @see #add(Object)
  95. */
  96. boolean addAll(Collection<? extends E> c);
  97. /**
  98. * Removes all elements from this set (optional operation). This set will
  99. * be empty afterwords, unless an exception occurs.
  100. *
  101. * @throws UnsupportedOperationException if this operation is not allowed
  102. */
  103. void clear();
  104. /**
  105. * Returns true if the set contains the specified element. In other words,
  106. * this looks for <code>o == null ? e == null : o.equals(e)</code>.
  107. *
  108. * @param o the object to look for
  109. * @return true if it is found in the set
  110. * @throws ClassCastException if the type of o is not a valid type
  111. * for this set.
  112. * @throws NullPointerException if o is null and this set doesn't
  113. * support null values.
  114. */
  115. boolean contains(Object o);
  116. /**
  117. * Returns true if this set contains all elements in the specified
  118. * collection. If the argument is also a set, this is the <i>subset</i>
  119. * relationship.
  120. *
  121. * @param c the collection to check membership in
  122. * @return true if all elements in this set are in c
  123. * @throws NullPointerException if c is null
  124. * @throws ClassCastException if the type of any element in c is not
  125. * a valid type for this set.
  126. * @throws NullPointerException if some element of c is null and this
  127. * set doesn't support null values.
  128. * @see #contains(Object)
  129. */
  130. boolean containsAll(Collection<?> c);
  131. /**
  132. * Compares the specified object to this for equality. For sets, the object
  133. * must be a set, the two must have the same size, and every element in
  134. * one must be in the other.
  135. *
  136. * @param o the object to compare to
  137. * @return true if it is an equal set
  138. */
  139. boolean equals(Object o);
  140. /**
  141. * Returns the hash code for this set. In order to satisfy the contract of
  142. * equals, this is the sum of the hashcode of all elements in the set.
  143. *
  144. * @return the sum of the hashcodes of all set elements
  145. * @see #equals(Object)
  146. */
  147. int hashCode();
  148. /**
  149. * Returns true if the set contains no elements.
  150. *
  151. * @return true if the set is empty
  152. */
  153. boolean isEmpty();
  154. /**
  155. * Returns an iterator over the set. The iterator has no specific order,
  156. * unless further specified.
  157. *
  158. * @return a set iterator
  159. */
  160. Iterator<E> iterator();
  161. /**
  162. * Removes the specified element from this set (optional operation). If
  163. * an element e exists, <code>o == null ? e == null : o.equals(e)</code>,
  164. * it is removed from the set.
  165. *
  166. * @param o the object to remove
  167. * @return true if the set changed (an object was removed)
  168. * @throws UnsupportedOperationException if this operation is not allowed
  169. * @throws ClassCastException if the type of o is not a valid type
  170. * for this set.
  171. * @throws NullPointerException if o is null and this set doesn't allow
  172. * the removal of a null value.
  173. */
  174. boolean remove(Object o);
  175. /**
  176. * Removes from this set all elements contained in the specified collection
  177. * (optional operation). If the argument is a set, this returns the
  178. * <i>asymmetric set difference</i> of the two sets.
  179. *
  180. * @param c the collection to remove from this set
  181. * @return true if this set changed as a result
  182. * @throws UnsupportedOperationException if this operation is not allowed
  183. * @throws NullPointerException if c is null
  184. * @throws ClassCastException if the type of any element in c is not
  185. * a valid type for this set.
  186. * @throws NullPointerException if some element of c is null and this
  187. * set doesn't support removing null values.
  188. * @see #remove(Object)
  189. */
  190. boolean removeAll(Collection<?> c);
  191. /**
  192. * Retains only the elements in this set that are also in the specified
  193. * collection (optional operation). If the argument is also a set, this
  194. * performs the <i>intersection</i> of the two sets.
  195. *
  196. * @param c the collection to keep
  197. * @return true if this set was modified
  198. * @throws UnsupportedOperationException if this operation is not allowed
  199. * @throws NullPointerException if c is null
  200. * @throws ClassCastException if the type of any element in c is not
  201. * a valid type for this set.
  202. * @throws NullPointerException if some element of c is null and this
  203. * set doesn't support retaining null values.
  204. * @see #remove(Object)
  205. */
  206. boolean retainAll(Collection<?> c);
  207. /**
  208. * Returns the number of elements in the set. If there are more
  209. * than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE. This is
  210. * the <i>cardinality</i> of the set.
  211. *
  212. * @return the number of elements
  213. */
  214. int size();
  215. /**
  216. * Returns an array containing the elements of this set. If the set
  217. * makes a guarantee about iteration order, the array has the same
  218. * order. The array is distinct from the set; modifying one does not
  219. * affect the other.
  220. *
  221. * @return an array of this set's elements
  222. * @see #toArray(Object[])
  223. */
  224. Object[] toArray();
  225. /**
  226. * Returns an array containing the elements of this set, of the same runtime
  227. * type of the argument. If the given set is large enough, it is reused,
  228. * and null is inserted in the first unused slot. Otherwise, reflection
  229. * is used to build a new array. If the set makes a guarantee about iteration
  230. * order, the array has the same order. The array is distinct from the set;
  231. * modifying one does not affect the other.
  232. *
  233. * @param a the array to determine the return type; if it is big enough
  234. * it is used and returned
  235. * @return an array holding the elements of the set
  236. * @throws ArrayStoreException if the runtime type of a is not a supertype
  237. * of all elements in the set
  238. * @throws NullPointerException if a is null
  239. * @see #toArray()
  240. */
  241. <T> T[] toArray(T[] a);
  242. }