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