Package java.util
Interface Set<E>
- All Superinterfaces:
Collection<E>,Iterable<E>
- All Known Subinterfaces:
NavigableSet<E>,SortedSet<E>
- All Known Implementing Classes:
AbstractSet,ConcurrentSkipListSet,CopyOnWriteArraySet,EnumSet,HashSet,LinkedHashSet,TreeSet
public interface Set<E> extends Collection<E>
A
Set is a data structure which does not allow duplicate elements.- Since:
- 1.2
-
Method Summary
Modifier and Type Method Description booleanadd(E object)Adds the specified object to this set.booleanaddAll(Collection<? extends E> collection)Adds the objects in the specified collection which do not exist yet in this set.voidclear()Removes all elements from this set, leaving it empty.booleancontains(Object object)Searches this set for the specified object.booleancontainsAll(Collection<?> collection)Searches this set for all objects in the specified collection.booleanequals(Object object)Compares the specified object to this set, and returns true if they represent the same object using a class specific comparison.inthashCode()Returns the hash code for this set.booleanisEmpty()Returns true if this set has no elements.Iterator<E>iterator()Returns an iterator on the elements of this set.booleanremove(Object object)Removes the specified object from this set.booleanremoveAll(Collection<?> collection)Removes all objects in the specified collection from this set.booleanretainAll(Collection<?> collection)Removes all objects from this set that are not contained in the specified collection.intsize()Returns the number of elements in this set.Object[]toArray()Returns an array containing all elements contained in this set.<T> T[]toArray(T[] array)Returns an array containing all elements contained in this set.
-
Method Details
-
add
Adds the specified object to this set. The set is not modified if it already contains the object.- Specified by:
addin interfaceCollection<E>- Parameters:
object- the object to add.- Returns:
trueif this set is modified,falseotherwise.- Throws:
UnsupportedOperationException- when adding to this set is not supported.ClassCastException- when the class of the object is inappropriate for this set.IllegalArgumentException- when the object cannot be added to this set.
-
addAll
Adds the objects in the specified collection which do not exist yet in this set.- Specified by:
addAllin interfaceCollection<E>- Parameters:
collection- the collection of objects.- Returns:
trueif this set is modified,falseotherwise.- Throws:
UnsupportedOperationException- when adding to this set is not supported.ClassCastException- when the class of an object is inappropriate for this set.IllegalArgumentException- when an object cannot be added to this set.
-
clear
void clear()Removes all elements from this set, leaving it empty.- Specified by:
clearin interfaceCollection<E>- Throws:
UnsupportedOperationException- when removing from this set is not supported.- See Also:
isEmpty(),size()
-
contains
Searches this set for the specified object.- Specified by:
containsin interfaceCollection<E>- Parameters:
object- the object to search for.- Returns:
trueif object is an element of this set,falseotherwise.
-
containsAll
Searches this set for all objects in the specified collection.- Specified by:
containsAllin interfaceCollection<E>- Parameters:
collection- the collection of objects.- Returns:
trueif all objects in the specified collection are elements of this set,falseotherwise.
-
equals
Compares the specified object to this set, and returns true if they represent the same object using a class specific comparison. Equality for a set means that both sets have the same size and the same elements.- Specified by:
equalsin interfaceCollection<E>- Overrides:
equalsin classObject- Parameters:
object- the object to compare with this object.- Returns:
- boolean
trueif the object is the same as this object, andfalseif it is different from this object. - See Also:
hashCode()
-
hashCode
int hashCode()Returns the hash code for this set. Two set which are equal must return the same value.- Specified by:
hashCodein interfaceCollection<E>- Overrides:
hashCodein classObject- Returns:
- the hash code of this set.
- See Also:
equals(java.lang.Object)
-
isEmpty
boolean isEmpty()Returns true if this set has no elements.- Specified by:
isEmptyin interfaceCollection<E>- Returns:
trueif this set has no elements,falseotherwise.- See Also:
size()
-
iterator
Returns an iterator on the elements of this set. The elements are unordered. -
remove
Removes the specified object from this set.- Specified by:
removein interfaceCollection<E>- Parameters:
object- the object to remove.- Returns:
trueif this set was modified,falseotherwise.- Throws:
UnsupportedOperationException- when removing from this set is not supported.
-
removeAll
Removes all objects in the specified collection from this set.- Specified by:
removeAllin interfaceCollection<E>- Parameters:
collection- the collection of objects to remove.- Returns:
trueif this set was modified,falseotherwise.- Throws:
UnsupportedOperationException- when removing from this set is not supported.
-
retainAll
Removes all objects from this set that are not contained in the specified collection.- Specified by:
retainAllin interfaceCollection<E>- Parameters:
collection- the collection of objects to retain.- Returns:
trueif this set was modified,falseotherwise.- Throws:
UnsupportedOperationException- when removing from this set is not supported.
-
size
int size()Returns the number of elements in this set.- Specified by:
sizein interfaceCollection<E>- Returns:
- the number of elements in this set.
-
toArray
Object[] toArray()Returns an array containing all elements contained in this set.- Specified by:
toArrayin interfaceCollection<E>- Returns:
- an array of the elements from this set.
-
toArray
<T> T[] toArray(T[] array)Returns an array containing all elements contained in this set. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than this set, the array element following the collection elements is set to null.- Specified by:
toArrayin interfaceCollection<E>- Parameters:
array- the array.- Returns:
- an array of the elements from this set.
- Throws:
ArrayStoreException- when the type of an element in this set cannot be stored in the type of the specified array.- See Also:
Collection.toArray(Object[])
-