0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/A/**
0N/A * A collection that contains no duplicate elements. More formally, sets
0N/A * contain no pair of elements <code>e1</code> and <code>e2</code> such that
0N/A * <code>e1.equals(e2)</code>, and at most one null element. As implied by
0N/A * its name, this interface models the mathematical <i>set</i> abstraction.
0N/A *
0N/A * <p>The <tt>Set</tt> interface places additional stipulations, beyond those
0N/A * inherited from the <tt>Collection</tt> interface, on the contracts of all
0N/A * constructors and on the contracts of the <tt>add</tt>, <tt>equals</tt> and
0N/A * <tt>hashCode</tt> methods. Declarations for other inherited methods are
0N/A * also included here for convenience. (The specifications accompanying these
0N/A * declarations have been tailored to the <tt>Set</tt> interface, but they do
0N/A * not contain any additional stipulations.)
0N/A *
0N/A * <p>The additional stipulation on constructors is, not surprisingly,
0N/A * that all constructors must create a set that contains no duplicate elements
0N/A * (as defined above).
0N/A *
0N/A * <p>Note: Great care must be exercised if mutable objects are used as set
0N/A * elements. The behavior of a set is not specified if the value of an object
0N/A * is changed in a manner that affects <tt>equals</tt> comparisons while the
0N/A * object is an element in the set. A special case of this prohibition is
0N/A * that it is not permissible for a set to contain itself as an element.
0N/A *
0N/A * <p>Some set implementations have restrictions on the elements that
0N/A * they may contain. For example, some implementations prohibit null elements,
0N/A * and some have restrictions on the types of their elements. Attempting to
0N/A * add an ineligible element throws an unchecked exception, typically
0N/A * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
0N/A * to query the presence of an ineligible element may throw an exception,
0N/A * or it may simply return false; some implementations will exhibit the former
0N/A * behavior and some will exhibit the latter. More generally, attempting an
0N/A * operation on an ineligible element whose completion would not result in
0N/A * the insertion of an ineligible element into the set may throw an
0N/A * exception or it may succeed, at the option of the implementation.
0N/A * Such exceptions are marked as "optional" in the specification for this
0N/A * interface.
0N/A *
0N/A * <p>This interface is a member of the
0N/A * <a href="{@docRoot}/../technotes/guides/collections/index.html">
0N/A * Java Collections Framework</a>.
0N/A *
0N/A * @param <E> the type of elements maintained by this set
0N/A *
0N/A * @author Josh Bloch
0N/A * @author Neal Gafter
0N/A * @see Collection
0N/A * @see List
0N/A * @see SortedSet
0N/A * @see HashSet
0N/A * @see TreeSet
0N/A * @see AbstractSet
0N/A * @see Collections#singleton(java.lang.Object)
0N/A * @see Collections#EMPTY_SET
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic interface Set<E> extends Collection<E> {
0N/A // Query Operations
0N/A
0N/A /**
0N/A * Returns the number of elements in this set (its cardinality). If this
0N/A * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
0N/A * <tt>Integer.MAX_VALUE</tt>.
0N/A *
0N/A * @return the number of elements in this set (its cardinality)
0N/A */
0N/A int size();
0N/A
0N/A /**
0N/A * Returns <tt>true</tt> if this set contains no elements.
0N/A *
0N/A * @return <tt>true</tt> if this set contains no elements
0N/A */
0N/A boolean isEmpty();
0N/A
0N/A /**
0N/A * Returns <tt>true</tt> if this set contains the specified element.
0N/A * More formally, returns <tt>true</tt> if and only if this set
0N/A * contains an element <tt>e</tt> such that
0N/A * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
0N/A *
0N/A * @param o element whose presence in this set is to be tested
0N/A * @return <tt>true</tt> if this set contains the specified element
0N/A * @throws ClassCastException if the type of the specified element
4106N/A * is incompatible with this set
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if the specified element is null and this
4106N/A * set does not permit null elements
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A */
0N/A boolean contains(Object o);
0N/A
0N/A /**
0N/A * Returns an iterator over the elements in this set. The elements are
0N/A * returned in no particular order (unless this set is an instance of some
0N/A * class that provides a guarantee).
0N/A *
0N/A * @return an iterator over the elements in this set
0N/A */
0N/A Iterator<E> iterator();
0N/A
0N/A /**
0N/A * Returns an array containing all of the elements in this set.
0N/A * If this set makes any guarantees as to what order its elements
0N/A * are returned by its iterator, this method must return the
0N/A * elements in the same order.
0N/A *
0N/A * <p>The returned array will be "safe" in that no references to it
0N/A * are maintained by this set. (In other words, this method must
0N/A * allocate a new array even if this set is backed by an array).
0N/A * The caller is thus free to modify the returned array.
0N/A *
0N/A * <p>This method acts as bridge between array-based and collection-based
0N/A * APIs.
0N/A *
0N/A * @return an array containing all the elements in this set
0N/A */
0N/A Object[] toArray();
0N/A
0N/A /**
0N/A * Returns an array containing all of the elements in this set; the
0N/A * runtime type of the returned array is that of the specified array.
0N/A * If the set fits in the specified array, it is returned therein.
0N/A * Otherwise, a new array is allocated with the runtime type of the
0N/A * specified array and the size of this set.
0N/A *
0N/A * <p>If this set fits in the specified array with room to spare
0N/A * (i.e., the array has more elements than this set), the element in
0N/A * the array immediately following the end of the set is set to
0N/A * <tt>null</tt>. (This is useful in determining the length of this
0N/A * set <i>only</i> if the caller knows that this set does not contain
0N/A * any null elements.)
0N/A *
0N/A * <p>If this set makes any guarantees as to what order its elements
0N/A * are returned by its iterator, this method must return the elements
0N/A * in the same order.
0N/A *
0N/A * <p>Like the {@link #toArray()} method, this method acts as bridge between
0N/A * array-based and collection-based APIs. Further, this method allows
0N/A * precise control over the runtime type of the output array, and may,
0N/A * under certain circumstances, be used to save allocation costs.
0N/A *
0N/A * <p>Suppose <tt>x</tt> is a set known to contain only strings.
0N/A * The following code can be used to dump the set into a newly allocated
0N/A * array of <tt>String</tt>:
0N/A *
0N/A * <pre>
0N/A * String[] y = x.toArray(new String[0]);</pre>
0N/A *
0N/A * Note that <tt>toArray(new Object[0])</tt> is identical in function to
0N/A * <tt>toArray()</tt>.
0N/A *
0N/A * @param a the array into which the elements of this set are to be
0N/A * stored, if it is big enough; otherwise, a new array of the same
0N/A * runtime type is allocated for this purpose.
0N/A * @return an array containing all the elements in this set
0N/A * @throws ArrayStoreException if the runtime type of the specified array
0N/A * is not a supertype of the runtime type of every element in this
0N/A * set
0N/A * @throws NullPointerException if the specified array is null
0N/A */
0N/A <T> T[] toArray(T[] a);
0N/A
0N/A
0N/A // Modification Operations
0N/A
0N/A /**
0N/A * Adds the specified element to this set if it is not already present
0N/A * (optional operation). More formally, adds the specified element
0N/A * <tt>e</tt> to this set if the set contains no element <tt>e2</tt>
0N/A * such that
0N/A * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
0N/A * If this set already contains the element, the call leaves the set
0N/A * unchanged and returns <tt>false</tt>. In combination with the
0N/A * restriction on constructors, this ensures that sets never contain
0N/A * duplicate elements.
0N/A *
0N/A * <p>The stipulation above does not imply that sets must accept all
0N/A * elements; sets may refuse to add any particular element, including
0N/A * <tt>null</tt>, and throw an exception, as described in the
0N/A * specification for {@link Collection#add Collection.add}.
0N/A * Individual set implementations should clearly document any
0N/A * restrictions on the elements that they may contain.
0N/A *
0N/A * @param e element to be added to this set
0N/A * @return <tt>true</tt> if this set did not already contain the specified
0N/A * element
0N/A * @throws UnsupportedOperationException if the <tt>add</tt> operation
0N/A * is not supported by this set
0N/A * @throws ClassCastException if the class of the specified element
0N/A * prevents it from being added to this set
0N/A * @throws NullPointerException if the specified element is null and this
0N/A * set does not permit null elements
0N/A * @throws IllegalArgumentException if some property of the specified element
0N/A * prevents it from being added to this set
0N/A */
0N/A boolean add(E e);
0N/A
0N/A
0N/A /**
0N/A * Removes the specified element from this set if it is present
0N/A * (optional operation). More formally, removes an element <tt>e</tt>
0N/A * such that
0N/A * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, if
0N/A * this set contains such an element. Returns <tt>true</tt> if this set
0N/A * contained the element (or equivalently, if this set changed as a
0N/A * result of the call). (This set will not contain the element once the
0N/A * call returns.)
0N/A *
0N/A * @param o object to be removed from this set, if present
0N/A * @return <tt>true</tt> if this set contained the specified element
0N/A * @throws ClassCastException if the type of the specified element
4106N/A * is incompatible with this set
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if the specified element is null and this
4106N/A * set does not permit null elements
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws UnsupportedOperationException if the <tt>remove</tt> operation
0N/A * is not supported by this set
0N/A */
0N/A boolean remove(Object o);
0N/A
0N/A
0N/A // Bulk Operations
0N/A
0N/A /**
0N/A * Returns <tt>true</tt> if this set contains all of the elements of the
0N/A * specified collection. If the specified collection is also a set, this
0N/A * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
0N/A *
0N/A * @param c collection to be checked for containment in this set
0N/A * @return <tt>true</tt> if this set contains all of the elements of the
0N/A * specified collection
0N/A * @throws ClassCastException if the types of one or more elements
0N/A * in the specified collection are incompatible with this
4106N/A * set
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if the specified collection contains one
0N/A * or more null elements and this set does not permit null
4106N/A * elements
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>),
4106N/A * or if the specified collection is null
0N/A * @see #contains(Object)
0N/A */
0N/A boolean containsAll(Collection<?> c);
0N/A
0N/A /**
0N/A * Adds all of the elements in the specified collection to this set if
0N/A * they're not already present (optional operation). If the specified
0N/A * collection is also a set, the <tt>addAll</tt> operation effectively
0N/A * modifies this set so that its value is the <i>union</i> of the two
0N/A * sets. The behavior of this operation is undefined if the specified
0N/A * collection is modified while the operation is in progress.
0N/A *
0N/A * @param c collection containing elements to be added to this set
0N/A * @return <tt>true</tt> if this set changed as a result of the call
0N/A *
0N/A * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
0N/A * is not supported by this set
0N/A * @throws ClassCastException if the class of an element of the
0N/A * specified collection prevents it from being added to this set
0N/A * @throws NullPointerException if the specified collection contains one
0N/A * or more null elements and this set does not permit null
0N/A * elements, or if the specified collection is null
0N/A * @throws IllegalArgumentException if some property of an element of the
0N/A * specified collection prevents it from being added to this set
0N/A * @see #add(Object)
0N/A */
0N/A boolean addAll(Collection<? extends E> c);
0N/A
0N/A /**
0N/A * Retains only the elements in this set that are contained in the
0N/A * specified collection (optional operation). In other words, removes
0N/A * from this set all of its elements that are not contained in the
0N/A * specified collection. If the specified collection is also a set, this
0N/A * operation effectively modifies this set so that its value is the
0N/A * <i>intersection</i> of the two sets.
0N/A *
0N/A * @param c collection containing elements to be retained in this set
0N/A * @return <tt>true</tt> if this set changed as a result of the call
0N/A * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
0N/A * is not supported by this set
0N/A * @throws ClassCastException if the class of an element of this set
4106N/A * is incompatible with the specified collection
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if this set contains a null element and the
4106N/A * specified collection does not permit null elements
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>),
0N/A * or if the specified collection is null
0N/A * @see #remove(Object)
0N/A */
0N/A boolean retainAll(Collection<?> c);
0N/A
0N/A /**
0N/A * Removes from this set all of its elements that are contained in the
0N/A * specified collection (optional operation). If the specified
0N/A * collection is also a set, this operation effectively modifies this
0N/A * set so that its value is the <i>asymmetric set difference</i> of
0N/A * the two sets.
0N/A *
0N/A * @param c collection containing elements to be removed from this set
0N/A * @return <tt>true</tt> if this set changed as a result of the call
0N/A * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
0N/A * is not supported by this set
0N/A * @throws ClassCastException if the class of an element of this set
4106N/A * is incompatible with the specified collection
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if this set contains a null element and the
4106N/A * specified collection does not permit null elements
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>),
0N/A * or if the specified collection is null
0N/A * @see #remove(Object)
0N/A * @see #contains(Object)
0N/A */
0N/A boolean removeAll(Collection<?> c);
0N/A
0N/A /**
0N/A * Removes all of the elements from this set (optional operation).
0N/A * The set will be empty after this call returns.
0N/A *
0N/A * @throws UnsupportedOperationException if the <tt>clear</tt> method
0N/A * is not supported by this set
0N/A */
0N/A void clear();
0N/A
0N/A
0N/A // Comparison and hashing
0N/A
0N/A /**
0N/A * Compares the specified object with this set for equality. Returns
0N/A * <tt>true</tt> if the specified object is also a set, the two sets
0N/A * have the same size, and every member of the specified set is
0N/A * contained in this set (or equivalently, every member of this set is
0N/A * contained in the specified set). This definition ensures that the
0N/A * equals method works properly across different implementations of the
0N/A * set interface.
0N/A *
0N/A * @param o object to be compared for equality with this set
0N/A * @return <tt>true</tt> if the specified object is equal to this set
0N/A */
0N/A boolean equals(Object o);
0N/A
0N/A /**
0N/A * Returns the hash code value for this set. The hash code of a set is
0N/A * defined to be the sum of the hash codes of the elements in the set,
0N/A * where the hash code of a <tt>null</tt> element is defined to be zero.
0N/A * This ensures that <tt>s1.equals(s2)</tt> implies that
0N/A * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
0N/A * and <tt>s2</tt>, as required by the general contract of
0N/A * {@link Object#hashCode}.
0N/A *
0N/A * @return the hash code value for this set
0N/A * @see Object#equals(Object)
0N/A * @see Set#equals(Object)
0N/A */
0N/A int hashCode();
0N/A}