java.util
Interface Set

All Superinterfaces:
Collection
All Known Implementing Classes:
AbstractSet, HashSet

public interface Set
extends Collection

A collection that contains no duplicates. In other words, for two set elements e1 and e2, e1.equals(e2) returns false. There are additional stipulations on add, equals and hashCode, as well as the requirements that constructors do not permit duplicate elements. The Set interface is incompatible with List; you cannot implement both simultaneously.

Note: Be careful about using mutable objects in sets. In particular, if a mutable object changes to become equal to another set element, you have violated the contract. As a special case of this, a Set is not allowed to be an element of itself, without risking undefined behavior.

Since:
1.2
See Also:
Collection, List, SortedSet, HashSet, TreeSet, LinkedHashSet, AbstractSet, Collections#singleton(Object), Collections#EMPTY_SET

Method Summary
 boolean add(Object o)
          Adds the specified element to the set if it is not already present (optional operation).
 boolean addAll(Collection c)
          Adds all of the elements of the given collection to this set (optional operation).
 void clear()
          Removes all elements from this set (optional operation).
 boolean contains(Object o)
          Returns true if the set contains the specified element.
 boolean containsAll(Collection c)
          Returns true if this set contains all elements in the specified collection.
 boolean equals(Object o)
          Compares the specified object to this for equality.
 int hashCode()
          Returns the hash code for this set.
 boolean isEmpty()
          Returns true if the set contains no elements.
 Iterator iterator()
          Returns an iterator over the set.
 boolean remove(Object o)
          Removes the specified element from this set (optional operation).
 boolean removeAll(Collection c)
          Removes from this set all elements contained in the specified collection (optional operation).
 boolean retainAll(Collection c)
          Retains only the elements in this set that are also in the specified collection (optional operation).
 int size()
          Returns the number of elements in the set.
 Object[] toArray()
          Returns an array containing the elements of this set.
 Object[] toArray(Object[] a)
          Returns an array containing the elements of this set, of the same runtime type of the argument.
 

Method Detail

add

boolean add(Object o)
Adds the specified element to the set if it is not already present (optional operation). In particular, the comparison algorithm is o == null ? e == null : o.equals(e). Sets need not permit all values, and may document what exceptions will be thrown if a value is not permitted.

Specified by:
add in interface Collection
Parameters:
o - the object to add
Returns:
true if the object was not previously in the set
Throws:
UnsupportedOperationException - if this operation is not allowed
ClassCastException - if the class of o prevents it from being added
IllegalArgumentException - if some aspect of o prevents it from being added
NullPointerException - if null is not permitted in this set

addAll

boolean addAll(Collection c)
Adds all of the elements of the given collection to this set (optional operation). If the argument is also a Set, this returns the mathematical union of the two. The behavior is unspecified if the set is modified while this is taking place.

Specified by:
addAll in interface Collection
Parameters:
c - the collection to add
Returns:
true if the set changed as a result
Throws:
UnsupportedOperationException - if this operation is not allowed
ClassCastException - if the class of an element prevents it from being added
IllegalArgumentException - if something about an element prevents it from being added
NullPointerException - if null is not permitted in this set, or if the argument c is null
See Also:
add(Object)

clear

void clear()
Removes all elements from this set (optional operation). This set will be empty afterwords, unless an exception occurs.

Specified by:
clear in interface Collection
Throws:
UnsupportedOperationException - if this operation is not allowed

contains

boolean contains(Object o)
Returns true if the set contains the specified element. In other words, this looks for o == null ? e == null : o.equals(e).

Specified by:
contains in interface Collection
Parameters:
o - the object to look for
Returns:
true if it is found in the set
Throws:
ClassCastException - if the type of o is not a valid type for this set.
NullPointerException - if o is null and this set doesn't support null values.

containsAll

boolean containsAll(Collection c)
Returns true if this set contains all elements in the specified collection. If the argument is also a set, this is the subset relationship.

Specified by:
containsAll in interface Collection
Parameters:
c - the collection to check membership in
Returns:
true if all elements in this set are in c
Throws:
NullPointerException - if c is null
ClassCastException - if the type of any element in c is not a valid type for this set.
NullPointerException - if some element of c is null and this set doesn't support null values.
See Also:
contains(Object)

equals

boolean equals(Object o)
Compares the specified object to this for equality. For sets, the object must be a set, the two must have the same size, and every element in one must be in the other.

Specified by:
equals in interface Collection
Overrides:
equals in class Object
Parameters:
o - the object to compare to
Returns:
true if it is an equal set
See Also:
Object.hashCode()

hashCode

int hashCode()
Returns the hash code for this set. In order to satisfy the contract of equals, this is the sum of the hashcode of all elements in the set.

Specified by:
hashCode in interface Collection
Overrides:
hashCode in class Object
Returns:
the sum of the hashcodes of all set elements
See Also:
equals(Object)

isEmpty

boolean isEmpty()
Returns true if the set contains no elements.

Specified by:
isEmpty in interface Collection
Returns:
true if the set is empty

iterator

Iterator iterator()
Returns an iterator over the set. The iterator has no specific order, unless further specified.

Specified by:
iterator in interface Collection
Returns:
a set iterator

remove

boolean remove(Object o)
Removes the specified element from this set (optional operation). If an element e exists, o == null ? e == null : o.equals(e), it is removed from the set.

Specified by:
remove in interface Collection
Parameters:
o - the object to remove
Returns:
true if the set changed (an object was removed)
Throws:
UnsupportedOperationException - if this operation is not allowed
ClassCastException - if the type of o is not a valid type for this set.
NullPointerException - if o is null and this set doesn't allow the removal of a null value.

removeAll

boolean removeAll(Collection c)
Removes from this set all elements contained in the specified collection (optional operation). If the argument is a set, this returns the asymmetric set difference of the two sets.

Specified by:
removeAll in interface Collection
Parameters:
c - the collection to remove from this set
Returns:
true if this set changed as a result
Throws:
UnsupportedOperationException - if this operation is not allowed
NullPointerException - if c is null
ClassCastException - if the type of any element in c is not a valid type for this set.
NullPointerException - if some element of c is null and this set doesn't support removing null values.
See Also:
remove(Object)

retainAll

boolean retainAll(Collection c)
Retains only the elements in this set that are also in the specified collection (optional operation). If the argument is also a set, this performs the intersection of the two sets.

Specified by:
retainAll in interface Collection
Parameters:
c - the collection to keep
Returns:
true if this set was modified
Throws:
UnsupportedOperationException - if this operation is not allowed
NullPointerException - if c is null
ClassCastException - if the type of any element in c is not a valid type for this set.
NullPointerException - if some element of c is null and this set doesn't support retaining null values.
See Also:
remove(Object)

size

int size()
Returns the number of elements in the set. If there are more than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE. This is the cardinality of the set.

Specified by:
size in interface Collection
Returns:
the number of elements

toArray

Object[] toArray()
Returns an array containing the elements of this set. If the set makes a guarantee about iteration order, the array has the same order. The array is distinct from the set; modifying one does not affect the other.

Specified by:
toArray in interface Collection
Returns:
an array of this set's elements
See Also:
toArray(Object[])

toArray

Object[] toArray(Object[] a)
Returns an array containing the elements of this set, of the same runtime type of the argument. If the given set is large enough, it is reused, and null is inserted in the first unused slot. Otherwise, reflection is used to build a new array. If the set makes a guarantee about iteration order, the array has the same order. The array is distinct from the set; modifying one does not affect the other.

Specified by:
toArray in interface Collection
Parameters:
a - the array to determine the return type; if it is big enough it is used and returned
Returns:
an array holding the elements of the set
Throws:
ArrayStoreException - if the runtime type of a is not a supertype of all elements in the set
NullPointerException - if a is null
See Also:
toArray()