Class ChainHashSet<E>

  • All Implemented Interfaces:
    Set<E>

    public class ChainHashSet<E>
    extends java.lang.Object
    implements Set<E>
    Implements a Set using a hash table with chaining.
    • Constructor Summary

      Constructors 
      Constructor Description
      ChainHashSet()
      Default constructor uses a hash table with size 23.
      ChainHashSet​(int s)
      Constructor receives table size as input.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(E e)
      Reports whether a given element belongs to this set.

      void include​(E e)
      Adds an input element to this set, throwing an ElementFoundException if this element already belongs to it.

      void remove​(E e)
      Removes an input element to this set, throwing a NoSuchElementException if this element does not belong to it.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ChainHashSet

        public ChainHashSet()
        Default constructor uses a hash table with size 23.
      • ChainHashSet

        public ChainHashSet​(int s)
        Constructor receives table size as input.

        Parameters:
        s - size of hash table to be used
        Throws:
        java.lang.IllegalArgumentException - if s is not positive
    • Method Detail

      • contains

        public boolean contains​(E e)
                         throws java.lang.NullPointerException
        Description copied from interface: Set
        Reports whether a given element belongs to this set.

        Specified by:
        contains in interface Set<E>
        Parameters:
        e - the element to be searched for
        Returns:
        true if this element is found; false otherwise

        Precondition:

        1. The Set Invariant is satisfied.
        2. A value e with type E has been given as input.
        Postcondition:

        1. The Set Invariant is satisfied.
        2. This set has not been changed.
        3. If the given element e is null then a NullPointerException is thrown.
        4. Otherwise (that is, if the given element is not null), if the given element e belongs to this set then “true” is returned; “false” is returned otherwise.
        Throws:
        java.lang.NullPointerException - if the given element is null
      • include

        public void include​(E e)
                     throws java.lang.NullPointerException,
                            ElementFoundException
        Description copied from interface: Set
        Adds an input element to this set, throwing an ElementFoundException if this element already belongs to it.

        Specified by:
        include in interface Set<E>
        Parameters:
        e - the element to be added to this set
        Throws:
        java.lang.NullPointerException - if the given element is null
        ElementFoundException - if this element is not null but is already in the set

        Precondition:

        1. The Set Invariant is satisfied.
        2. A value e with type E has been given as input.
        Postcondition:

        1. The Set Invariant is satisfied.
        2. If the input element is null then a NullPointerException is thrown.
        3. Otherwise (that is, if the input element is not null), if the input element e was not already included in this set then e has been added to it, and no other changes have been made. An ElementFoundException is thrown, otherwise, and this set has not been changed.
      • remove

        public void remove​(E e)
                    throws java.lang.NullPointerException,
                           java.util.NoSuchElementException
        Description copied from interface: Set
        Removes an input element to this set, throwing a NoSuchElementException if this element does not belong to it.

        Specified by:
        remove in interface Set<E>
        Parameters:
        e - the element to be removed from this set
        Throws:
        java.lang.NullPointerException - if the given element is null
        java.util.NoSuchElementException - if this element was not in this set

        Precondition:

        1. The Set Invariant is satisfied.
        2. A value e with type E has been given as input.
        Postcondition:

        1. The Set Invariant is satisfied.
        2. If the input element is null then a NullPointerException is thrown.
        3. Otherwise (that is, if the given element is null), if the input element e was already included in this set then this element is removed from it. A NoSuchElementException is thrown otherwise, and this set is not changed.