Class HashSet<T>

java.lang.Object
HashSet<T>
All Implemented Interfaces:
Cloneable, Iterable<T>, Collection<T>, Deque<T>, List<T>, Queue<T>

public class HashSet<T> extends Object implements Deque<T>, Cloneable, List<T>
Resizable implementation of the Deque and List interface via a standard HashSet implementation. Does not allow for insertion of null elements.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The number of items in this HashSet
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty HashSet.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int index, T item)
    This method is unsupported, except for the case when index == 0 || index == size
    boolean
    add(T item)
    Inserts the specified element into the queue represented by this HashSet if it is possible to do so (ie, if this item isn't already contained in the set), returning true upon success.
    boolean
    addAll(int index, Collection<? extends T> c)
    Unsupported method.
    boolean
    addAll(Collection<? extends T> c)
    Adds all of the elements in the specified collection at the end of this HashSet, as if by calling addLast(T) on each one, in the order that they are returned by the collection's iterator.
    void
    addFirst(T item)
    Inserts the specified element at the front of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).
    void
    addLast(T item)
    Inserts the specified element at the end of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).
    void
    Removes all of the elements from this collection.
    Creates and returns a shallow copy of this object.
    boolean
    Returns true if this HashSet contains the specified element.
    boolean
    Returns true if this collection contains all of the elements in the specified collection.
    Returns an iterator over the elements in this HashSet in reverse sequential order.
    Equivalent to getFirst().
    get(int index)
    Returns the element at the specified position in this list.
    get(T item)
    Returns the item in this HashSet that is equal to the specified item, or null if no such item exists
    Retrieves, but does not remove, the first element of this HashSet.
    Retrieves, but does not remove, the last element of this HashSet.
    int
    Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
    boolean
    Returns true if this collection contains no elements.
    Returns an iterator over this HashSet in the proper sequence.
    int
    Returns a list iterator over the elements in this list (in proper sequence).
    listIterator(int index)
    Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
    boolean
    offer(T item)
    Equivalent to add(Object).
    boolean
    offerFirst(T item)
    Inserts the specified element at the front of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).
    boolean
    offerLast(T item)
    Equivalent to add(Object).
    Equivalent to getFirst().
    Equivalent to getFirst().
    Equivalent to getLast().
    Equivalent to removeFirst().
    Equivalent to removeFirst().
    Equivalent to removeLast().
    pop()
    Equivalent to removeFirst().
    void
    push(T item)
    Equivalent to addFirst(T).
    Equivalent to removeFirst().
    remove(int index)
    Removes the element at the specified position in this list.
    boolean
    Removes the specified element from this HashSet.
    boolean
    Removes all of this collection's elements that are also contained in the specified collection.
    Retrieves and removes the first element of this HashSet.
    boolean
    Equivalent to remove(Object)
    Retrieves and removes the last element of this HashSet.
    boolean
    Equivalent to remove(Object)
    boolean
    Retains only the elements in this collection that are contained in the specified collection.
    set(int index, T element)
    Unsupported method.
    int
    Returns the number of items in this HashSet.
    subList(int fromIndex, int toIndex)
    Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
    Returns an array containing all of the elements in this collection in the correct order.
    <A> A[]
    toArray(A[] a)
    Returns an array containing all of the elements in this collection in the correct order; the runtime type of the returned array is that of the specified array.
    Returns a string representation of the object.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.Collection

    parallelStream, removeIf, stream, toArray

    Methods inherited from interface java.lang.Iterable

    forEach

    Methods inherited from interface java.util.List

    equals, hashCode, replaceAll, sort, spliterator
  • Field Details

    • size

      protected int size
      The number of items in this HashSet
  • Constructor Details

    • HashSet

      public HashSet()
      Creates an empty HashSet.
  • Method Details

    • size

      public int size()
      Returns the number of items in this HashSet.
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface Deque<T>
      Specified by:
      size in interface List<T>
      Returns:
      the number of items in this HashSet.
    • iterator

      public Iterator<T> iterator()
      Returns an iterator over this HashSet in the proper sequence.
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Deque<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface List<T>
      Returns:
      an iterator over this HashSet in the proper sequence.
    • get

      public T get(T item)
      Returns the item in this HashSet that is equal to the specified item, or null if no such item exists

      Note that these need not be explicitly the same item.

      Parameters:
      item - the item to be found in the HashSet
      Returns:
      the item in this HashSet that is equal to the specified item, or null if no such item exists
    • getFirst

      public T getFirst()
      Retrieves, but does not remove, the first element of this HashSet.
      Specified by:
      getFirst in interface Deque<T>
      Returns:
      the first element of this HashSet.
    • getLast

      public T getLast()
      Retrieves, but does not remove, the last element of this HashSet.
      Specified by:
      getLast in interface Deque<T>
      Returns:
      the last element of this HashSet.
    • contains

      public boolean contains(Object o)
      Returns true if this HashSet contains the specified element.

      More formally, returns true if and only if this HashSet contains an element e such that Objects.equals(o, e).

      Specified by:
      contains in interface Collection<T>
      Specified by:
      contains in interface Deque<T>
      Specified by:
      contains in interface List<T>
      Parameters:
      o - element whose presence in this HashSet is to be tested
      Returns:
      true if this HashSet contains the specified element.
    • containsAll

      public boolean containsAll(Collection<?> items)
      Returns true if this collection contains all of the elements in the specified collection.
      Specified by:
      containsAll in interface Collection<T>
      Specified by:
      containsAll in interface List<T>
      Parameters:
      items - collection to be checked for containment in this collection
      Returns:
      true if this collection contains all of the elements in the specified collection.
    • removeFirst

      public T removeFirst()
      Retrieves and removes the first element of this HashSet.
      Specified by:
      removeFirst in interface Deque<T>
      Returns:
      the first element of this HashSet.
    • removeLast

      public T removeLast()
      Retrieves and removes the last element of this HashSet.
      Specified by:
      removeLast in interface Deque<T>
      Returns:
      the last element of this HashSet.
    • add

      public boolean add(T item)
      Inserts the specified element into the queue represented by this HashSet if it is possible to do so (ie, if this item isn't already contained in the set), returning true upon success.
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface Deque<T>
      Specified by:
      add in interface List<T>
      Specified by:
      add in interface Queue<T>
      Parameters:
      item - the item to add
      Returns:
      true if item was successfully added to the set.
    • toString

      public String toString()
      Returns a string representation of the object.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the object.
    • isEmpty

      public boolean isEmpty()
      Returns true if this collection contains no elements.
      Specified by:
      isEmpty in interface Collection<T>
      Specified by:
      isEmpty in interface List<T>
      Returns:
      true if this collection contains no elements.
    • toArray

      public Object[] toArray()
      Returns an array containing all of the elements in this collection in the correct order. The returned array's runtime component type is Object.
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface List<T>
      Returns:
      an array containing all of the elements in this collection in the correct order.
    • toArray

      public <A> A[] toArray(A[] a)
      Returns an array containing all of the elements in this collection in the correct order; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface List<T>
      Parameters:
      a - the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
      Returns:
      an array containing all of the elements in this collection in the correct order.
    • remove

      public boolean remove(Object o)
      Removes the specified element from this HashSet. If the HashSet does not contain the element, it is unchanged. More formally, removes the element e such that Objects.equals(o, e) (if such an element exists). Returns true if this HashSet contained the specified element (or equivalently, if this HashSet changed as a result of the call).
      Specified by:
      remove in interface Collection<T>
      Specified by:
      remove in interface Deque<T>
      Specified by:
      remove in interface List<T>
      Parameters:
      o - element to be removed from this HashSet, if present
      Returns:
      true if this HashSet contained the specified element.
    • addAll

      public boolean addAll(Collection<? extends T> c)
      Adds all of the elements in the specified collection at the end of this HashSet, as if by calling addLast(T) on each one, in the order that they are returned by the collection's iterator.
      Specified by:
      addAll in interface Collection<T>
      Specified by:
      addAll in interface Deque<T>
      Specified by:
      addAll in interface List<T>
      Parameters:
      c - the elements to be inserted into this HashSet
      Returns:
      true if this HashSet changed as a result of the call.
    • removeAll

      public boolean removeAll(Collection<?> c)
      Removes all of this collection's elements that are also contained in the specified collection. After this call returns, this collection will contain no elements in common with the specified collection.
      Specified by:
      removeAll in interface Collection<T>
      Specified by:
      removeAll in interface List<T>
      Parameters:
      c - collection containing elements to be removed from this collection
      Returns:
      true if this collection changed as a result of the call.
    • retainAll

      public boolean retainAll(Collection<?> c)
      Retains only the elements in this collection that are contained in the specified collection. In other words, removes from this collection all of its elements that are not contained in the specified collection.
      Specified by:
      retainAll in interface Collection<T>
      Specified by:
      retainAll in interface List<T>
      Parameters:
      c - collection containing elements to be retained in this collection
      Returns:
      true if this collection changed as a result of the call.
    • clear

      public void clear()
      Removes all of the elements from this collection. The collection will be empty after this method returns.
      Specified by:
      clear in interface Collection<T>
      Specified by:
      clear in interface List<T>
    • addFirst

      public void addFirst(T item)
      Inserts the specified element at the front of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).
      Specified by:
      addFirst in interface Deque<T>
    • addLast

      public void addLast(T item)
      Inserts the specified element at the end of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).
      Specified by:
      addLast in interface Deque<T>
    • offerFirst

      public boolean offerFirst(T item)
      Inserts the specified element at the front of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).
      Specified by:
      offerFirst in interface Deque<T>
      Parameters:
      item - the element to add
      Returns:
      true if the element was added to this HashSet, else false.
    • offerLast

      public boolean offerLast(T item)
      Equivalent to add(Object).
      Specified by:
      offerLast in interface Deque<T>
      Parameters:
      item - the element to add
      Returns:
      true if the element was added to this HashSet, else false.
    • pollFirst

      public T pollFirst()
      Equivalent to removeFirst().
      Specified by:
      pollFirst in interface Deque<T>
      Returns:
      the first element of this HashSet.
    • pollLast

      public T pollLast()
      Equivalent to removeLast().
      Specified by:
      pollLast in interface Deque<T>
      Returns:
      the last element of this HashSet.
    • peekFirst

      public T peekFirst()
      Equivalent to getFirst().
      Specified by:
      peekFirst in interface Deque<T>
      Returns:
      the first element of this HashSet.
    • peekLast

      public T peekLast()
      Equivalent to getLast().
      Specified by:
      peekLast in interface Deque<T>
      Returns:
      the last element of this HashSet.
    • removeFirstOccurrence

      public boolean removeFirstOccurrence(Object o)
      Equivalent to remove(Object)
      Specified by:
      removeFirstOccurrence in interface Deque<T>
      Parameters:
      o - element to be removed from this HashSet, if present
      Returns:
      true if this HashSet contained the specified element.
    • removeLastOccurrence

      public boolean removeLastOccurrence(Object o)
      Equivalent to remove(Object)
      Specified by:
      removeLastOccurrence in interface Deque<T>
      Parameters:
      o - element to be removed from this HashSet, if present
      Returns:
      true if this HashSet contained the specified element.
    • offer

      public boolean offer(T item)
      Equivalent to add(Object).
      Specified by:
      offer in interface Deque<T>
      Specified by:
      offer in interface Queue<T>
      Parameters:
      item - the element to add
      Returns:
      true if the element was added to this HashSet, else false.
    • remove

      public T remove()
      Equivalent to removeFirst().
      Specified by:
      remove in interface Deque<T>
      Specified by:
      remove in interface Queue<T>
      Returns:
      the first element of this HashSet.
    • poll

      public T poll()
      Equivalent to removeFirst().
      Specified by:
      poll in interface Deque<T>
      Specified by:
      poll in interface Queue<T>
      Returns:
      the first element of this HashSet.
    • element

      public T element()
      Equivalent to getFirst().
      Specified by:
      element in interface Deque<T>
      Specified by:
      element in interface Queue<T>
      Returns:
      the first element of this HashSet.
    • peek

      public T peek()
      Equivalent to getFirst().
      Specified by:
      peek in interface Deque<T>
      Specified by:
      peek in interface Queue<T>
      Returns:
      the first element of this HashSet.
    • push

      public void push(T item)
      Equivalent to addFirst(T).
      Specified by:
      push in interface Deque<T>
    • pop

      public T pop()
      Equivalent to removeFirst().
      Specified by:
      pop in interface Deque<T>
      Returns:
      the first element of this HashSet.
    • descendingIterator

      public Iterator<T> descendingIterator()
      Returns an iterator over the elements in this HashSet in reverse sequential order.
      Specified by:
      descendingIterator in interface Deque<T>
      Returns:
      an iterator over the elements in this HashSet in reverse sequential order.
    • clone

      public HashSet<T> clone()
      Creates and returns a shallow copy of this object.
      Overrides:
      clone in class Object
      Returns:
      a shallow copy of this object.
    • addAll

      public boolean addAll(int index, Collection<? extends T> c)
      Unsupported method.
      Specified by:
      addAll in interface List<T>
      Returns:
      N/A: this method isn't supported
      Throws:
      UnsupportedOperationException
    • get

      public T get(int index)
      Returns the element at the specified position in this list.
      Specified by:
      get in interface List<T>
      Parameters:
      index - of the element to return
      Returns:
      the element at the specified position in this list
    • set

      public T set(int index, T element)
      Unsupported method.
      Specified by:
      set in interface List<T>
      Returns:
      N/A: this method isn't supported
      Throws:
      UnsupportedOperationException
    • add

      public void add(int index, T item)
      This method is unsupported, except for the case when index == 0 || index == size
      Specified by:
      add in interface List<T>
    • remove

      public T remove(int index)
      Removes the element at the specified position in this list.
      Specified by:
      remove in interface List<T>
      Parameters:
      index - the index of the element to be removed
      Returns:
      the element previously at the specified position
    • indexOf

      public int indexOf(Object o)
      Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
      Specified by:
      indexOf in interface List<T>
      Parameters:
      o - element to search for
      Returns:
      the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
    • lastIndexOf

      public int lastIndexOf(Object o)
      Specified by:
      lastIndexOf in interface List<T>
      Parameters:
      o - element to search for
      Returns:
      the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
    • listIterator

      public ListIterator<T> listIterator()
      Returns a list iterator over the elements in this list (in proper sequence).
      Specified by:
      listIterator in interface List<T>
      Returns:
      a list iterator over the elements in this list (in proper sequence).
    • listIterator

      public ListIterator<T> listIterator(int index)
      Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.
      Specified by:
      listIterator in interface List<T>
      Parameters:
      index - index of the first element to be returned from the list iterator (by a call to next)
      Returns:
      a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list
    • subList

      public List<T> subList(int fromIndex, int toIndex)
      Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.)
      Specified by:
      subList in interface List<T>
      Parameters:
      fromIndex - low endpoint (inclusive) of the subList
      toIndex - high endpoint (exclusive) of the subList
      Returns:
      a view of the specified range within this list