Package cpsc331.collections
Class ChainHashMap<K,V>
- java.lang.Object
-
- cpsc331.collections.ChainHashMap<K,V>
-
-
Constructor Summary
Constructors Constructor Description ChainHashMap()
Default constructor uses a hash table with size 23.ChainHashMap(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.
-
-
-
Constructor Detail
-
ChainHashMap
public ChainHashMap()
Default constructor uses a hash table with size 23.
-
ChainHashMap
public ChainHashMap(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 interfaceMapping<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 nulljava.util.NoSuchElementException
- if the key is not null, but no value is defined for this key
Precondition:
- The Mapping Invariant is satisfied.
- A value k with type K has been provided as input.
- The Mapping Invariant is satisfied.
- This Mapping has not been changed.
- If k is null then a NullPointerException is thrown.
- 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 interfaceMapping<K,V>
- Parameters:
k
- the key for which a value is to be definedv
- the value that is to be defined to this key- Throws:
java.lang.NullPointerException
- if k is null
Precondition:
- The Mapping Invariant is satisfied.
- A value k with type K and v with type V are provided as input.
- The Mapping Invariant is satisfied.
- If k is null then a NullPointerException is thrown.
- 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 interfaceMapping<K,V>
- Parameters:
k
- the key for which a value is to be undefined- Throws:
java.lang.NullPointerException
- if k is nulljava.util.NoSuchElementException
- if no value was defined for this key
Precondition:
- The Dictionary Invariant is satisfied.
- A value k with type K is provided as input.
- The Mapping Invariant is satisfied.
- If k is null then a NullPointerException is thrown.
- 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.
-
-