Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Set

public interface Set
implements Collection<E>

java.util.Set<E>
Known Indirect Subclasses


A Set is a data structure which does not allow duplicate elements.

Summary

Public methods

abstract boolean add(E object)

Adds the specified object to this set.

abstract boolean addAll(Collection<? extends E> collection)

Adds the objects in the specified collection which do not exist yet in this set.

abstract void clear()

Removes all elements from this set, leaving it empty.

abstract boolean contains(Object object)

Searches this set for the specified object.

abstract boolean containsAll(Collection<?> collection)

Searches this set for all objects in the specified collection.

abstract boolean equals(Object object)

Compares the specified object to this set, and returns true if they represent the same object using a class specific comparison.

abstract int hashCode()

Returns the hash code for this set.

abstract boolean isEmpty()

Returns true if this set has no elements.

abstract Iterator<E> iterator()

Returns an iterator on the elements of this set.

abstract boolean remove(Object object)

Removes the specified object from this set.

abstract boolean removeAll(Collection<?> collection)

Removes all objects in the specified collection from this set.

abstract boolean retainAll(Collection<?> collection)

Removes all objects from this set that are not contained in the specified collection.

abstract int size()

Returns the number of elements in this set.

abstract <T> T[] toArray(T[] array)

Returns an array containing all elements contained in this set.

abstract Object[] toArray()

Returns an array containing all elements contained in this set.

Inherited methods

From interface java.util.Collection
From interface java.lang.Iterable

Public methods

add

Added in API level 1
boolean add (E object)

Adds the specified object to this set. The set is not modified if it already contains the object.

Parameters
object E: the object to add.
Returns
boolean true if this set is modified, false otherwise.
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

Added in API level 1
boolean addAll (Collection<? extends E> collection)

Adds the objects in the specified collection which do not exist yet in this set.

Parameters
collection Collection: the collection of objects.
Returns
boolean true if this set is modified, false otherwise.
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

Added in API level 1
void clear ()

Removes all elements from this set, leaving it empty.

Throws
UnsupportedOperationException when removing from this set is not supported.

See also:

contains

Added in API level 1
boolean contains (Object object)

Searches this set for the specified object.

Parameters
object Object: the object to search for.
Returns
boolean true if object is an element of this set, false otherwise.

containsAll

Added in API level 1
boolean containsAll (Collection<?> collection)

Searches this set for all objects in the specified collection.

Parameters
collection Collection: the collection of objects.
Returns
boolean true if all objects in the specified collection are elements of this set, false otherwise.

equals

Added in API level 1
boolean equals (Object object)

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.

Parameters
object Object: the object to compare with this object.
Returns
boolean boolean true if the object is the same as this object, and false if it is different from this object.

See also:

hashCode

Added in API level 1
int hashCode ()

Returns the hash code for this set. Two set which are equal must return the same value.

Returns
int the hash code of this set.

See also:

isEmpty

Added in API level 1
boolean isEmpty ()

Returns true if this set has no elements.

Returns
boolean true if this set has no elements, false otherwise.

See also:

iterator

Added in API level 1
Iterator<E> iterator ()

Returns an iterator on the elements of this set. The elements are unordered.

Returns
Iterator<E> an iterator on the elements of this set.

See also:

remove

Added in API level 1
boolean remove (Object object)

Removes the specified object from this set.

Parameters
object Object: the object to remove.
Returns
boolean true if this set was modified, false otherwise.
Throws
UnsupportedOperationException when removing from this set is not supported.

removeAll

Added in API level 1
boolean removeAll (Collection<?> collection)

Removes all objects in the specified collection from this set.

Parameters
collection Collection: the collection of objects to remove.
Returns
boolean true if this set was modified, false otherwise.
Throws
UnsupportedOperationException when removing from this set is not supported.

retainAll

Added in API level 1
boolean retainAll (Collection<?> collection)

Removes all objects from this set that are not contained in the specified collection.

Parameters
collection Collection: the collection of objects to retain.
Returns
boolean true if this set was modified, false otherwise.
Throws
UnsupportedOperationException when removing from this set is not supported.

size

Added in API level 1
int size ()

Returns the number of elements in this set.

Returns
int the number of elements in this set.

toArray

Added in API level 1
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.

Parameters
array T: the array.
Returns
T[] 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:

toArray

Added in API level 1
Object[] toArray ()

Returns an array containing all elements contained in this set.

Returns
Object[] an array of the elements from this set.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.