Source Home >> Java Source 1.6.0 >> java.util.Set V 0.09
  • 001/*
  • 002 * @(#)Set.java 1.39 06/04/21
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.util;
  • 009
  • 010/**
  • 011 * A collection that contains no duplicate elements. More formally, sets
  • 012 * contain no pair of elements <code>e1</code> and <code>e2</code> such that
  • 013 * <code>e1.equals(e2)</code>, and at most one null element. As implied by
  • 014 * its name, this interface models the mathematical <i>set</i> abstraction.
  • 015 *
  • 016 * <p>The <tt>Set</tt> interface places additional stipulations, beyond those
  • 017 * inherited from the <tt>Collection</tt> interface, on the contracts of all
  • 018 * constructors and on the contracts of the <tt>add</tt>, <tt>equals</tt> and
  • 019 * <tt>hashCode</tt> methods. Declarations for other inherited methods are
  • 020 * also included here for convenience. (The specifications accompanying these
  • 021 * declarations have been tailored to the <tt>Set</tt> interface, but they do
  • 022 * not contain any additional stipulations.)
  • 023 *
  • 024 * <p>The additional stipulation on constructors is, not surprisingly,
  • 025 * that all constructors must create a set that contains no duplicate elements
  • 026 * (as defined above).
  • 027 *
  • 028 * <p>Note: Great care must be exercised if mutable objects are used as set
  • 029 * elements. The behavior of a set is not specified if the value of an object
  • 030 * is changed in a manner that affects <tt>equals</tt> comparisons while the
  • 031 * object is an element in the set. A special case of this prohibition is
  • 032 * that it is not permissible for a set to contain itself as an element.
  • 033 *
  • 034 * <p>Some set implementations have restrictions on the elements that
  • 035 * they may contain. For example, some implementations prohibit null elements,
  • 036 * and some have restrictions on the types of their elements. Attempting to
  • 037 * add an ineligible element throws an unchecked exception, typically
  • 038 * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
  • 039 * to query the presence of an ineligible element may throw an exception,
  • 040 * or it may simply return false; some implementations will exhibit the former
  • 041 * behavior and some will exhibit the latter. More generally, attempting an
  • 042 * operation on an ineligible element whose completion would not result in
  • 043 * the insertion of an ineligible element into the set may throw an
  • 044 * exception or it may succeed, at the option of the implementation.
  • 045 * Such exceptions are marked as "optional" in the specification for this
  • 046 * interface.
  • 047 *
  • 048 * <p>This interface is a member of the
  • 049 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  • 050 * Java Collections Framework</a>.
  • 051 *
  • 052 * @param <E> the type of elements maintained by this set
  • 053 *
  • 054 * @author Josh Bloch
  • 055 * @author Neal Gafter
  • 056 * @version 1.39, 04/21/06
  • 057 * @see Collection
  • 058 * @see List
  • 059 * @see SortedSet
  • 060 * @see HashSet
  • 061 * @see TreeSet
  • 062 * @see AbstractSet
  • 063 * @see Collections#singleton(java.lang.Object)
  • 064 * @see Collections#EMPTY_SET
  • 065 * @since 1.2
  • 066 */
  • 067
  • 068public interface Set<E> extends Collection<E> {
  • 069 // Query Operations
  • 070
  • 071 /**
  • 072 * Returns the number of elements in this set (its cardinality). If this
  • 073 * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  • 074 * <tt>Integer.MAX_VALUE</tt>.
  • 075 *
  • 076 * @return the number of elements in this set (its cardinality)
  • 077 */
  • 078 int size();
  • 079
  • 080 /**
  • 081 * Returns <tt>true</tt> if this set contains no elements.
  • 082 *
  • 083 * @return <tt>true</tt> if this set contains no elements
  • 084 */
  • 085 boolean isEmpty();
  • 086
  • 087 /**
  • 088 * Returns <tt>true</tt> if this set contains the specified element.
  • 089 * More formally, returns <tt>true</tt> if and only if this set
  • 090 * contains an element <tt>e</tt> such that
  • 091 * <tt>(o==null ? e==null : o.equals(e))</tt>.
  • 092 *
  • 093 * @param o element whose presence in this set is to be tested
  • 094 * @return <tt>true</tt> if this set contains the specified element
  • 095 * @throws ClassCastException if the type of the specified element
  • 096 * is incompatible with this set (optional)
  • 097 * @throws NullPointerException if the specified element is null and this
  • 098 * set does not permit null elements (optional)
  • 099 */
  • 100 boolean contains(Object o);
  • 101
  • 102 /**
  • 103 * Returns an iterator over the elements in this set. The elements are
  • 104 * returned in no particular order (unless this set is an instance of some
  • 105 * class that provides a guarantee).
  • 106 *
  • 107 * @return an iterator over the elements in this set
  • 108 */
  • 109 Iterator<E> iterator();
  • 110
  • 111 /**
  • 112 * Returns an array containing all of the elements in this set.
  • 113 * If this set makes any guarantees as to what order its elements
  • 114 * are returned by its iterator, this method must return the
  • 115 * elements in the same order.
  • 116 *
  • 117 * <p>The returned array will be "safe" in that no references to it
  • 118 * are maintained by this set. (In other words, this method must
  • 119 * allocate a new array even if this set is backed by an array).
  • 120 * The caller is thus free to modify the returned array.
  • 121 *
  • 122 * <p>This method acts as bridge between array-based and collection-based
  • 123 * APIs.
  • 124 *
  • 125 * @return an array containing all the elements in this set
  • 126 */
  • 127 Object[] toArray();
  • 128
  • 129 /**
  • 130 * Returns an array containing all of the elements in this set; the
  • 131 * runtime type of the returned array is that of the specified array.
  • 132 * If the set fits in the specified array, it is returned therein.
  • 133 * Otherwise, a new array is allocated with the runtime type of the
  • 134 * specified array and the size of this set.
  • 135 *
  • 136 * <p>If this set fits in the specified array with room to spare
  • 137 * (i.e., the array has more elements than this set), the element in
  • 138 * the array immediately following the end of the set is set to
  • 139 * <tt>null</tt>. (This is useful in determining the length of this
  • 140 * set <i>only</i> if the caller knows that this set does not contain
  • 141 * any null elements.)
  • 142 *
  • 143 * <p>If this set makes any guarantees as to what order its elements
  • 144 * are returned by its iterator, this method must return the elements
  • 145 * in the same order.
  • 146 *
  • 147 * <p>Like the {@link #toArray()} method, this method acts as bridge between
  • 148 * array-based and collection-based APIs. Further, this method allows
  • 149 * precise control over the runtime type of the output array, and may,
  • 150 * under certain circumstances, be used to save allocation costs.
  • 151 *
  • 152 * <p>Suppose <tt>x</tt> is a set known to contain only strings.
  • 153 * The following code can be used to dump the set into a newly allocated
  • 154 * array of <tt>String</tt>:
  • 155 *
  • 156 * <pre>
  • 157 * String[] y = x.toArray(new String[0]);</pre>
  • 158 *
  • 159 * Note that <tt>toArray(new Object[0])</tt> is identical in function to
  • 160 * <tt>toArray()</tt>.
  • 161 *
  • 162 * @param a the array into which the elements of this set are to be
  • 163 * stored, if it is big enough; otherwise, a new array of the same
  • 164 * runtime type is allocated for this purpose.
  • 165 * @return an array containing all the elements in this set
  • 166 * @throws ArrayStoreException if the runtime type of the specified array
  • 167 * is not a supertype of the runtime type of every element in this
  • 168 * set
  • 169 * @throws NullPointerException if the specified array is null
  • 170 */
  • 171 <T> T[] toArray(T[] a);
  • 172
  • 173
  • 174 // Modification Operations
  • 175
  • 176 /**
  • 177 * Adds the specified element to this set if it is not already present
  • 178 * (optional operation). More formally, adds the specified element
  • 179 * <tt>e</tt> to this set if the set contains no element <tt>e2</tt>
  • 180 * such that
  • 181 * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
  • 182 * If this set already contains the element, the call leaves the set
  • 183 * unchanged and returns <tt>false</tt>. In combination with the
  • 184 * restriction on constructors, this ensures that sets never contain
  • 185 * duplicate elements.
  • 186 *
  • 187 * <p>The stipulation above does not imply that sets must accept all
  • 188 * elements; sets may refuse to add any particular element, including
  • 189 * <tt>null</tt>, and throw an exception, as described in the
  • 190 * specification for {@link Collection#add Collection.add}.
  • 191 * Individual set implementations should clearly document any
  • 192 * restrictions on the elements that they may contain.
  • 193 *
  • 194 * @param e element to be added to this set
  • 195 * @return <tt>true</tt> if this set did not already contain the specified
  • 196 * element
  • 197 * @throws UnsupportedOperationException if the <tt>add</tt> operation
  • 198 * is not supported by this set
  • 199 * @throws ClassCastException if the class of the specified element
  • 200 * prevents it from being added to this set
  • 201 * @throws NullPointerException if the specified element is null and this
  • 202 * set does not permit null elements
  • 203 * @throws IllegalArgumentException if some property of the specified element
  • 204 * prevents it from being added to this set
  • 205 */
  • 206 boolean add(E e);
  • 207
  • 208
  • 209 /**
  • 210 * Removes the specified element from this set if it is present
  • 211 * (optional operation). More formally, removes an element <tt>e</tt>
  • 212 * such that
  • 213 * <tt>(o==null ? e==null : o.equals(e))</tt>, if
  • 214 * this set contains such an element. Returns <tt>true</tt> if this set
  • 215 * contained the element (or equivalently, if this set changed as a
  • 216 * result of the call). (This set will not contain the element once the
  • 217 * call returns.)
  • 218 *
  • 219 * @param o object to be removed from this set, if present
  • 220 * @return <tt>true</tt> if this set contained the specified element
  • 221 * @throws ClassCastException if the type of the specified element
  • 222 * is incompatible with this set (optional)
  • 223 * @throws NullPointerException if the specified element is null and this
  • 224 * set does not permit null elements (optional)
  • 225 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
  • 226 * is not supported by this set
  • 227 */
  • 228 boolean remove(Object o);
  • 229
  • 230
  • 231 // Bulk Operations
  • 232
  • 233 /**
  • 234 * Returns <tt>true</tt> if this set contains all of the elements of the
  • 235 * specified collection. If the specified collection is also a set, this
  • 236 * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
  • 237 *
  • 238 * @param c collection to be checked for containment in this set
  • 239 * @return <tt>true</tt> if this set contains all of the elements of the
  • 240 * specified collection
  • 241 * @throws ClassCastException if the types of one or more elements
  • 242 * in the specified collection are incompatible with this
  • 243 * set (optional)
  • 244 * @throws NullPointerException if the specified collection contains one
  • 245 * or more null elements and this set does not permit null
  • 246 * elements (optional), or if the specified collection is null
  • 247 * @see #contains(Object)
  • 248 */
  • 249 boolean containsAll(Collection<?> c);
  • 250
  • 251 /**
  • 252 * Adds all of the elements in the specified collection to this set if
  • 253 * they're not already present (optional operation). If the specified
  • 254 * collection is also a set, the <tt>addAll</tt> operation effectively
  • 255 * modifies this set so that its value is the <i>union</i> of the two
  • 256 * sets. The behavior of this operation is undefined if the specified
  • 257 * collection is modified while the operation is in progress.
  • 258 *
  • 259 * @param c collection containing elements to be added to this set
  • 260 * @return <tt>true</tt> if this set changed as a result of the call
  • 261 *
  • 262 * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
  • 263 * is not supported by this set
  • 264 * @throws ClassCastException if the class of an element of the
  • 265 * specified collection prevents it from being added to this set
  • 266 * @throws NullPointerException if the specified collection contains one
  • 267 * or more null elements and this set does not permit null
  • 268 * elements, or if the specified collection is null
  • 269 * @throws IllegalArgumentException if some property of an element of the
  • 270 * specified collection prevents it from being added to this set
  • 271 * @see #add(Object)
  • 272 */
  • 273 boolean addAll(Collection<? extends E> c);
  • 274
  • 275 /**
  • 276 * Retains only the elements in this set that are contained in the
  • 277 * specified collection (optional operation). In other words, removes
  • 278 * from this set all of its elements that are not contained in the
  • 279 * specified collection. If the specified collection is also a set, this
  • 280 * operation effectively modifies this set so that its value is the
  • 281 * <i>intersection</i> of the two sets.
  • 282 *
  • 283 * @param c collection containing elements to be retained in this set
  • 284 * @return <tt>true</tt> if this set changed as a result of the call
  • 285 * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
  • 286 * is not supported by this set
  • 287 * @throws ClassCastException if the class of an element of this set
  • 288 * is incompatible with the specified collection (optional)
  • 289 * @throws NullPointerException if this set contains a null element and the
  • 290 * specified collection does not permit null elements (optional),
  • 291 * or if the specified collection is null
  • 292 * @see #remove(Object)
  • 293 */
  • 294 boolean retainAll(Collection<?> c);
  • 295
  • 296 /**
  • 297 * Removes from this set all of its elements that are contained in the
  • 298 * specified collection (optional operation). If the specified
  • 299 * collection is also a set, this operation effectively modifies this
  • 300 * set so that its value is the <i>asymmetric set difference</i> of
  • 301 * the two sets.
  • 302 *
  • 303 * @param c collection containing elements to be removed from this set
  • 304 * @return <tt>true</tt> if this set changed as a result of the call
  • 305 * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
  • 306 * is not supported by this set
  • 307 * @throws ClassCastException if the class of an element of this set
  • 308 * is incompatible with the specified collection (optional)
  • 309 * @throws NullPointerException if this set contains a null element and the
  • 310 * specified collection does not permit null elements (optional),
  • 311 * or if the specified collection is null
  • 312 * @see #remove(Object)
  • 313 * @see #contains(Object)
  • 314 */
  • 315 boolean removeAll(Collection<?> c);
  • 316
  • 317 /**
  • 318 * Removes all of the elements from this set (optional operation).
  • 319 * The set will be empty after this call returns.
  • 320 *
  • 321 * @throws UnsupportedOperationException if the <tt>clear</tt> method
  • 322 * is not supported by this set
  • 323 */
  • 324 void clear();
  • 325
  • 326
  • 327 // Comparison and hashing
  • 328
  • 329 /**
  • 330 * Compares the specified object with this set for equality. Returns
  • 331 * <tt>true</tt> if the specified object is also a set, the two sets
  • 332 * have the same size, and every member of the specified set is
  • 333 * contained in this set (or equivalently, every member of this set is
  • 334 * contained in the specified set). This definition ensures that the
  • 335 * equals method works properly across different implementations of the
  • 336 * set interface.
  • 337 *
  • 338 * @param o object to be compared for equality with this set
  • 339 * @return <tt>true</tt> if the specified object is equal to this set
  • 340 */
  • 341 boolean equals(Object o);
  • 342
  • 343 /**
  • 344 * Returns the hash code value for this set. The hash code of a set is
  • 345 * defined to be the sum of the hash codes of the elements in the set,
  • 346 * where the hash code of a <tt>null</tt> element is defined to be zero.
  • 347 * This ensures that <tt>s1.equals(s2)</tt> implies that
  • 348 * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
  • 349 * and <tt>s2</tt>, as required by the general contract of
  • 350 * {@link Object#hashCode}.
  • 351 *
  • 352 * @return the hash code value for this set
  • 353 * @see Object#equals(Object)
  • 354 * @see Set#equals(Object)
  • 355 */
  • 356 int hashCode();
  • 357}

文件:Set.java
包名:java.util
类名:Set
继承:Collection
接口: