1 /*
   2  * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 /**
  29  * A collection that contains no duplicate elements.  More formally, sets
  30  * contain no pair of elements <code>e1</code> and <code>e2</code> such that
  31  * <code>e1.equals(e2)</code>, and at most one null element.  As implied by
  32  * its name, this interface models the mathematical <i>set</i> abstraction.
  33  *
  34  * <p>The <tt>Set</tt> interface places additional stipulations, beyond those
  35  * inherited from the <tt>Collection</tt> interface, on the contracts of all
  36  * constructors and on the contracts of the <tt>add</tt>, <tt>equals</tt> and
  37  * <tt>hashCode</tt> methods.  Declarations for other inherited methods are
  38  * also included here for convenience.  (The specifications accompanying these
  39  * declarations have been tailored to the <tt>Set</tt> interface, but they do
  40  * not contain any additional stipulations.)
  41  *
  42  * <p>The additional stipulation on constructors is, not surprisingly,
  43  * that all constructors must create a set that contains no duplicate elements
  44  * (as defined above).
  45  *
  46  * <p>Note: Great care must be exercised if mutable objects are used as set
  47  * elements.  The behavior of a set is not specified if the value of an object
  48  * is changed in a manner that affects <tt>equals</tt> comparisons while the
  49  * object is an element in the set.  A special case of this prohibition is
  50  * that it is not permissible for a set to contain itself as an element.
  51  *
  52  * <p>Some set implementations have restrictions on the elements that
  53  * they may contain.  For example, some implementations prohibit null elements,
  54  * and some have restrictions on the types of their elements.  Attempting to
  55  * add an ineligible element throws an unchecked exception, typically
  56  * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>.  Attempting
  57  * to query the presence of an ineligible element may throw an exception,
  58  * or it may simply return false; some implementations will exhibit the former
  59  * behavior and some will exhibit the latter.  More generally, attempting an
  60  * operation on an ineligible element whose completion would not result in
  61  * the insertion of an ineligible element into the set may throw an
  62  * exception or it may succeed, at the option of the implementation.
  63  * Such exceptions are marked as "optional" in the specification for this
  64  * interface.
  65  *
  66  * <p>This interface is a member of the
  67  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  68  * Java Collections Framework</a>.
  69  *
  70  * @param <E> the type of elements maintained by this set
  71  *
  72  * @author  Josh Bloch
  73  * @author  Neal Gafter
  74  * @see Collection
  75  * @see List
  76  * @see SortedSet
  77  * @see HashSet
  78  * @see TreeSet
  79  * @see AbstractSet
  80  * @see Collections#singleton(java.lang.Object)
  81  * @see Collections#EMPTY_SET
  82  * @since 1.2
  83  */
  84 
  85 public interface Set<E> extends Collection<E> {
  86     // Query Operations
  87 
  88     /**
  89      * Returns the number of elements in this set (its cardinality).  If this
  90      * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  91      * <tt>Integer.MAX_VALUE</tt>.
  92      *
  93      * @return the number of elements in this set (its cardinality)
  94      */
  95     int size();
  96 
  97     /**
  98      * Returns <tt>true</tt> if this set contains no elements.
  99      *
 100      * @return <tt>true</tt> if this set contains no elements
 101      */
 102     boolean isEmpty();
 103 
 104     /**
 105      * Returns <tt>true</tt> if this set contains the specified element.
 106      * More formally, returns <tt>true</tt> if and only if this set
 107      * contains an element <tt>e</tt> such that
 108      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
 109      *
 110      * @param o element whose presence in this set is to be tested
 111      * @return <tt>true</tt> if this set contains the specified element
 112      * @throws ClassCastException if the type of the specified element
 113      *         is incompatible with this set (optional)
 114      * @throws NullPointerException if the specified element is null and this
 115      *         set does not permit null elements (optional)
 116      */
 117     boolean contains(Object o);
 118 
 119     /**
 120      * Returns an iterator over the elements in this set.  The elements are
 121      * returned in no particular order (unless this set is an instance of some
 122      * class that provides a guarantee).
 123      *
 124      * @return an iterator over the elements in this set
 125      */
 126     Iterator<E> iterator();
 127 
 128     /**
 129      * Returns an array containing all of the elements in this set.
 130      * If this set makes any guarantees as to what order its elements
 131      * are returned by its iterator, this method must return the
 132      * elements in the same order.
 133      *
 134      * <p>The returned array will be "safe" in that no references to it
 135      * are maintained by this set.  (In other words, this method must
 136      * allocate a new array even if this set is backed by an array).
 137      * The caller is thus free to modify the returned array.
 138      *
 139      * <p>This method acts as bridge between array-based and collection-based
 140      * APIs.
 141      *
 142      * @return an array containing all the elements in this set
 143      */
 144     Object[] toArray();
 145 
 146     /**
 147      * Returns an array containing all of the elements in this set; the
 148      * runtime type of the returned array is that of the specified array.
 149      * If the set fits in the specified array, it is returned therein.
 150      * Otherwise, a new array is allocated with the runtime type of the
 151      * specified array and the size of this set.
 152      *
 153      * <p>If this set fits in the specified array with room to spare
 154      * (i.e., the array has more elements than this set), the element in
 155      * the array immediately following the end of the set is set to
 156      * <tt>null</tt>.  (This is useful in determining the length of this
 157      * set <i>only</i> if the caller knows that this set does not contain
 158      * any null elements.)
 159      *
 160      * <p>If this set makes any guarantees as to what order its elements
 161      * are returned by its iterator, this method must return the elements
 162      * in the same order.
 163      *
 164      * <p>Like the {@link #toArray()} method, this method acts as bridge between
 165      * array-based and collection-based APIs.  Further, this method allows
 166      * precise control over the runtime type of the output array, and may,
 167      * under certain circumstances, be used to save allocation costs.
 168      *
 169      * <p>Suppose <tt>x</tt> is a set known to contain only strings.
 170      * The following code can be used to dump the set into a newly allocated
 171      * array of <tt>String</tt>:
 172      *
 173      * <pre>
 174      *     String[] y = x.toArray(new String[0]);</pre>
 175      *
 176      * Note that <tt>toArray(new Object[0])</tt> is identical in function to
 177      * <tt>toArray()</tt>.
 178      *
 179      * @param a the array into which the elements of this set are to be
 180      *        stored, if it is big enough; otherwise, a new array of the same
 181      *        runtime type is allocated for this purpose.
 182      * @return an array containing all the elements in this set
 183      * @throws ArrayStoreException if the runtime type of the specified array
 184      *         is not a supertype of the runtime type of every element in this
 185      *         set
 186      * @throws NullPointerException if the specified array is null
 187      */
 188     <T> T[] toArray(T[] a);
 189 
 190 
 191     // Modification Operations
 192 
 193     /**
 194      * Adds the specified element to this set if it is not already present
 195      * (optional operation).  More formally, adds the specified element
 196      * <tt>e</tt> to this set if the set contains no element <tt>e2</tt>
 197      * such that
 198      * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
 199      * If this set already contains the element, the call leaves the set
 200      * unchanged and returns <tt>false</tt>.  In combination with the
 201      * restriction on constructors, this ensures that sets never contain
 202      * duplicate elements.
 203      *
 204      * <p>The stipulation above does not imply that sets must accept all
 205      * elements; sets may refuse to add any particular element, including
 206      * <tt>null</tt>, and throw an exception, as described in the
 207      * specification for {@link Collection#add Collection.add}.
 208      * Individual set implementations should clearly document any
 209      * restrictions on the elements that they may contain.
 210      *
 211      * @param e element to be added to this set
 212      * @return <tt>true</tt> if this set did not already contain the specified
 213      *         element
 214      * @throws UnsupportedOperationException if the <tt>add</tt> operation
 215      *         is not supported by this set
 216      * @throws ClassCastException if the class of the specified element
 217      *         prevents it from being added to this set
 218      * @throws NullPointerException if the specified element is null and this
 219      *         set does not permit null elements
 220      * @throws IllegalArgumentException if some property of the specified element
 221      *         prevents it from being added to this set
 222      */
 223     boolean add(E e);
 224 
 225 
 226     /**
 227      * Removes the specified element from this set if it is present
 228      * (optional operation).  More formally, removes an element <tt>e</tt>
 229      * such that
 230      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, if
 231      * this set contains such an element.  Returns <tt>true</tt> if this set
 232      * contained the element (or equivalently, if this set changed as a
 233      * result of the call).  (This set will not contain the element once the
 234      * call returns.)
 235      *
 236      * @param o object to be removed from this set, if present
 237      * @return <tt>true</tt> if this set contained the specified element
 238      * @throws ClassCastException if the type of the specified element
 239      *         is incompatible with this set (optional)
 240      * @throws NullPointerException if the specified element is null and this
 241      *         set does not permit null elements (optional)
 242      * @throws UnsupportedOperationException if the <tt>remove</tt> operation
 243      *         is not supported by this set
 244      */
 245     boolean remove(Object o);
 246 
 247 
 248     // Bulk Operations
 249 
 250     /**
 251      * Returns <tt>true</tt> if this set contains all of the elements of the
 252      * specified collection.  If the specified collection is also a set, this
 253      * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
 254      *
 255      * @param  c collection to be checked for containment in this set
 256      * @return <tt>true</tt> if this set contains all of the elements of the
 257      *         specified collection
 258      * @throws ClassCastException if the types of one or more elements
 259      *         in the specified collection are incompatible with this
 260      *         set (optional)
 261      * @throws NullPointerException if the specified collection contains one
 262      *         or more null elements and this set does not permit null
 263      *         elements (optional), or if the specified collection is null
 264      * @see    #contains(Object)
 265      */
 266     boolean containsAll(Collection<?> c);
 267 
 268     /**
 269      * Adds all of the elements in the specified collection to this set if
 270      * they're not already present (optional operation).  If the specified
 271      * collection is also a set, the <tt>addAll</tt> operation effectively
 272      * modifies this set so that its value is the <i>union</i> of the two
 273      * sets.  The behavior of this operation is undefined if the specified
 274      * collection is modified while the operation is in progress.
 275      *
 276      * @param  c collection containing elements to be added to this set
 277      * @return <tt>true</tt> if this set changed as a result of the call
 278      *
 279      * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
 280      *         is not supported by this set
 281      * @throws ClassCastException if the class of an element of the
 282      *         specified collection prevents it from being added to this set
 283      * @throws NullPointerException if the specified collection contains one
 284      *         or more null elements and this set does not permit null
 285      *         elements, or if the specified collection is null
 286      * @throws IllegalArgumentException if some property of an element of the
 287      *         specified collection prevents it from being added to this set
 288      * @see #add(Object)
 289      */
 290     boolean addAll(Collection<? extends E> c);
 291 
 292     /**
 293      * Retains only the elements in this set that are contained in the
 294      * specified collection (optional operation).  In other words, removes
 295      * from this set all of its elements that are not contained in the
 296      * specified collection.  If the specified collection is also a set, this
 297      * operation effectively modifies this set so that its value is the
 298      * <i>intersection</i> of the two sets.
 299      *
 300      * @param  c collection containing elements to be retained in this set
 301      * @return <tt>true</tt> if this set changed as a result of the call
 302      * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
 303      *         is not supported by this set
 304      * @throws ClassCastException if the class of an element of this set
 305      *         is incompatible with the specified collection (optional)
 306      * @throws NullPointerException if this set contains a null element and the
 307      *         specified collection does not permit null elements (optional),
 308      *         or if the specified collection is null
 309      * @see #remove(Object)
 310      */
 311     boolean retainAll(Collection<?> c);
 312 
 313     /**
 314      * Removes from this set all of its elements that are contained in the
 315      * specified collection (optional operation).  If the specified
 316      * collection is also a set, this operation effectively modifies this
 317      * set so that its value is the <i>asymmetric set difference</i> of
 318      * the two sets.
 319      *
 320      * @param  c collection containing elements to be removed from this set
 321      * @return <tt>true</tt> if this set changed as a result of the call
 322      * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
 323      *         is not supported by this set
 324      * @throws ClassCastException if the class of an element of this set
 325      *         is incompatible with the specified collection (optional)
 326      * @throws NullPointerException if this set contains a null element and the
 327      *         specified collection does not permit null elements (optional),
 328      *         or if the specified collection is null
 329      * @see #remove(Object)
 330      * @see #contains(Object)
 331      */
 332     boolean removeAll(Collection<?> c);
 333 
 334     /**
 335      * Removes all of the elements from this set (optional operation).
 336      * The set will be empty after this call returns.
 337      *
 338      * @throws UnsupportedOperationException if the <tt>clear</tt> method
 339      *         is not supported by this set
 340      */
 341     void clear();
 342 
 343 
 344     // Comparison and hashing
 345 
 346     /**
 347      * Compares the specified object with this set for equality.  Returns
 348      * <tt>true</tt> if the specified object is also a set, the two sets
 349      * have the same size, and every member of the specified set is
 350      * contained in this set (or equivalently, every member of this set is
 351      * contained in the specified set).  This definition ensures that the
 352      * equals method works properly across different implementations of the
 353      * set interface.
 354      *
 355      * @param o object to be compared for equality with this set
 356      * @return <tt>true</tt> if the specified object is equal to this set
 357      */
 358     boolean equals(Object o);
 359 
 360     /**
 361      * Returns the hash code value for this set.  The hash code of a set is
 362      * defined to be the sum of the hash codes of the elements in the set,
 363      * where the hash code of a <tt>null</tt> element is defined to be zero.
 364      * This ensures that <tt>s1.equals(s2)</tt> implies that
 365      * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
 366      * and <tt>s2</tt>, as required by the general contract of
 367      * {@link Object#hashCode}.
 368      *
 369      * @return the hash code value for this set
 370      * @see Object#equals(Object)
 371      * @see Set#equals(Object)
 372      */
 373     int hashCode();
 374 }