Interface Set<E>

All Superinterfaces:
Collection<E>, Iterable<E>
All Known Subinterfaces:
NavigableSet<E>, SortedSet<E>
All Known Implementing Classes:
AbstractSet, ComponentSelector, 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
    boolean
    add(E object)
    Adds the specified object to this set.
    boolean
    addAll(Collection<? extends E> collection)
    Adds the objects in the specified collection which do not exist yet in this set.
    void
    Removes all elements from this set, leaving it empty.
    boolean
    contains(Object object)
    Searches this set for the specified object.
    boolean
    containsAll(Collection<?> collection)
    Searches this set for all objects in the specified collection.
    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.
    int
    Returns the hash code for this set.
    boolean
    Returns true if this set has no elements.
    Returns an iterator on the elements of this set.
    boolean
    remove(Object object)
    Removes the specified object from this set.
    boolean
    removeAll(Collection<?> collection)
    Removes all objects in the specified collection from this set.
    boolean
    retainAll(Collection<?> collection)
    Removes all objects from this set that are not contained in the specified collection.
    int
    Returns the number of elements in this set.
    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

      boolean add(E object)

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

      Parameters
      • object: the object to add.
      Returns

      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.

      Specified by:
      add in interface Collection<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if adding to this Collection is not supported.

      • ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection.

      • IllegalArgumentException: if the object cannot be added to this Collection.

      • NullPointerException: if null elements cannot be added to the Collection.

    • addAll

      boolean addAll(Collection<? extends E> collection)

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

      Parameters
      • collection: the collection of objects.
      Returns

      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.

      Specified by:
      addAll in interface Collection<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if adding to this Collection is not supported.

      • ClassCastException: @throws ClassCastException if the class of an object is inappropriate for this Collection.

      • IllegalArgumentException: if an object cannot be added to this Collection.

      • NullPointerException: @throws NullPointerException if collection is null, or if it contains null elements and this Collection does not support such elements.

    • clear

      void clear()

      Removes all elements from this set, leaving it empty.

      Throws
      • UnsupportedOperationException: when removing from this set is not supported.
      See also
      • #isEmpty

      • #size

      Specified by:
      clear in interface Collection<E>
    • contains

      boolean contains(Object object)

      Searches this set for the specified object.

      Parameters
      • object: the object to search for.
      Returns
      Specified by:
      contains in interface Collection<E>
      Returns:
      true if object is an element of this set, false otherwise.
    • containsAll

      boolean containsAll(Collection<?> collection)

      Searches this set for all objects in the specified collection.

      Parameters
      • collection: the collection of objects.
      Returns
      Specified by:
      containsAll in interface Collection<E>
      Returns:
      true if all objects in the specified collection are elements of this set, false otherwise.
    • equals

      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: the object to compare with this object.
      Returns
      Specified by:
      equals in interface Collection<E>
      Overrides:
      equals in class Object
      Returns:

      boolean true if the object is the same as this object, and false if 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.

      Returns

      the hash code of this set.

      See also
      • #equals
      Specified by:
      hashCode in interface Collection<E>
      Overrides:
      hashCode in class Object
    • isEmpty

      boolean isEmpty()

      Returns true if this set has no elements.

      Returns
      Specified by:
      isEmpty in interface Collection<E>
      Returns:

      true if this set has no elements, false otherwise.

      See also
      • #size
    • iterator

      Iterator<E> iterator()

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

      Returns

      an iterator on the elements of this set.

      See also
      • Iterator
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
    • remove

      boolean remove(Object object)

      Removes the specified object from this set.

      Parameters
      • object: the object to remove.
      Returns

      true if this set was modified, false otherwise.

      Throws
      • UnsupportedOperationException: when removing from this set is not supported.
      Specified by:
      remove in interface Collection<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if removing from this Collection is not supported.

      • ClassCastException: if the object passed is not of the correct type.

      • NullPointerException: @throws NullPointerException if object is null and this Collection doesn't support null elements.

    • removeAll

      boolean removeAll(Collection<?> collection)

      Removes all objects in the specified collection from this set.

      Parameters
      • collection: the collection of objects to remove.
      Returns

      true if this set was modified, false otherwise.

      Throws
      • UnsupportedOperationException: when removing from this set is not supported.
      Specified by:
      removeAll in interface Collection<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if removing from this Collection is not supported.

      • ClassCastException: @throws ClassCastException if one or more elements of collection isn't of the correct type.

      • NullPointerException: @throws NullPointerException if collection contains at least one null element and this Collection doesn't support null elements.

      • NullPointerException: if collection is null.

    • retainAll

      boolean retainAll(Collection<?> collection)

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

      Parameters
      • collection: the collection of objects to retain.
      Returns

      true if this set was modified, false otherwise.

      Throws
      • UnsupportedOperationException: when removing from this set is not supported.
      Specified by:
      retainAll in interface Collection<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if removing from this Collection is not supported.

      • ClassCastException: @throws ClassCastException if one or more elements of collection isn't of the correct type.

      • NullPointerException: @throws NullPointerException if collection contains at least one null element and this Collection doesn't support null elements.

      • NullPointerException: if collection is null.

    • size

      int size()

      Returns the number of elements in this set.

      Returns

      the number of elements in this set.

      Specified by:
      size in interface Collection<E>
      Returns:
      how many objects this Collection contains, or Integer.MAX_VALUE if there are more than Integer.MAX_VALUE elements in this Collection.
    • toArray

      Object[] toArray()

      Returns an array containing all elements contained in this set.

      Returns

      an array of the elements from this set.

      Specified by:
      toArray in interface Collection<E>
    • 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.

      Parameters
      • array: the array.
      Returns

      an array of the elements from this set.

      Throws
      • ArrayStoreException: @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[])
      Specified by:
      toArray in interface Collection<E>