Class OpenHashMap<K,​V>

  • All Implemented Interfaces:
    Mapping<K,​V>

    public class OpenHashMap<K,​V>
    extends java.lang.Object
    implements Mapping<K,​V>
    Implements a Mapping using a hash table with open addressing.
    • Constructor Summary

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

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      V get​(K k)
      Returns the value associated with a given key, throwing a NoSuchElementException if no value is currently associated with the given key.

      void remove​(K k)
      Removes the ordered pair with a given input key k, returning a NoSuchElementException and leaving the Mapping unchanged if no such ordered pair exists.

      void set​(K k, V v)
      Sets the value associated with an input key to be an input value — replacing the value formerly associated with this key if one already exists.

      • Methods inherited from class java.lang.Object

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

      • OpenHashMap

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

        public OpenHashMap​(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

      • get

        public V get​(K k)
              throws java.lang.NullPointerException,
                     java.util.NoSuchElementException
        Description copied from interface: Mapping
        Returns the value associated with a given key, throwing a NoSuchElementException if no value is currently associated with the given key.

        Specified by:
        get in interface Mapping<K,​V>
        Parameters:
        k - the key whose value is to be returned
        Returns:
        the value of this key
        Throws:
        java.lang.NullPointerException - if the input key is null
        java.util.NoSuchElementException - if the key is not null, but no value is defined for this key

        Precondition:
        1. The Mapping Invariant is satisfied.
        2. A value k with type K has been provided as input.
        Postcondition:
        1. The Mapping Invariant is satisfied.
        2. This Mapping has not been changed.
        3. If k is null then a NullPointerException is thrown.
        4. Otherwise (that is, if k is not null), if this Mapping includes some ordered pair whose first entry is the input key k, then the value v that is the second entry of this ordered pair (that is, the value associated with k) is returned as output. A NoSuchElementException is thrown otherwise.
      • set

        public void set​(K k,
                        V v)
                 throws java.lang.NullPointerException
        Description copied from interface: Mapping
        Sets the value associated with an input key to be an input value — replacing the value formerly associated with this key if one already exists.

        Specified by:
        set in interface Mapping<K,​V>
        Parameters:
        k - the key for which a value is to be defined
        v - the value that is to be defined to this key
        Throws:
        java.lang.NullPointerException - if k is null

        Precondition:
        1. The Mapping Invariant is satisfied.
        2. A value k with type K and v with type V are provided as input.
        Postcondition:
        1. The Mapping Invariant is satisfied.
        2. If k is null then a NullPointerException is thrown.
        3. Otherwise (that is, if k is not null), if this Mapping included an ordered pair whose first entry is the input value k, then this is replaced by the ordered pair (k, v). Otherwise a new ordered pair (k, v) is added. No other changes to this Mapping are made.
      • remove

        public void remove​(K k)
                    throws java.lang.NullPointerException,
                           java.util.NoSuchElementException
        Description copied from interface: Mapping
        Removes the ordered pair with a given input key k, returning a NoSuchElementException and leaving the Mapping unchanged if no such ordered pair exists.

        Specified by:
        remove in interface Mapping<K,​V>
        Parameters:
        k - the key for which a value is to be undefined
        Throws:
        java.lang.NullPointerException - if k is null
        java.util.NoSuchElementException - if no value was defined for this key

        Precondition:
        1. The Dictionary Invariant is satisfied.
        2. A value k with type K is provided as input.
        Postcondition:
        1. The Mapping Invariant is satisfied.
        2. If k is null then a NullPointerException is thrown.
        3. Otherwise (that is, if k is not null) if this Mapping includes an ordered pair (k, v) whose first entry is the input key k then this ordered pair is removed; no other changes are made. Otherwise a NoSuchElementException is thrown and this Mapping is not changed at all.