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