Class ChainHashMap<K,V>

  • All Implemented Interfaces:
    Map<K,V>


    public class ChainHashMap<K,V>
    extends AbstractHashMap<K,V>
    Map implementation using hash table with separate chaining.
    • Constructor Summary

      Constructors 
      Constructor and Description
      ChainHashMap()
      Creates a hash table with capacity 11 and prime factor 109345121.
      ChainHashMap(int cap)
      Creates a hash table with given capacity and prime factor 109345121.
      ChainHashMap(int cap, int p)
      Creates a hash table with the given capacity and prime factor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      protected V bucketGet(int h, K k)
      Returns value associated with key k in bucket with hash value h.
      protected V bucketPut(int h, K k, V v)
      Associates key k with value v in bucket with hash value h, returning the previously associated value, if any.
      protected V bucketRemove(int h, K k)
      Removes entry having key k from bucket with hash value h, returning the previously associated value, if found.
      protected void createTable()
      Creates an empty table having length equal to current capacity.
      java.lang.Iterable<Entry<K,V>> entrySet()
      Returns an iterable collection of all key-value entries of the map.
      • Methods inherited from class java.lang.Object

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

      • table

        private UnsortedTableMap<K,V>[] table
        a fixed capacity array of UnsortedTableMap that serve as buckets
    • Constructor Detail

      • ChainHashMap

        public ChainHashMap()
        Creates a hash table with capacity 11 and prime factor 109345121.
      • ChainHashMap

        public ChainHashMap(int cap)
        Creates a hash table with given capacity and prime factor 109345121.
      • ChainHashMap

        public ChainHashMap(int cap,
                            int p)
        Creates a hash table with the given capacity and prime factor.
    • Method Detail

      • createTable

        protected void createTable()
        Creates an empty table having length equal to current capacity.
        Specified by:
        createTable in class AbstractHashMap<K,V>
      • bucketGet

        protected V bucketGet(int h,
                              K k)
        Returns value associated with key k in bucket with hash value h. If no such entry exists, returns null.
        Specified by:
        bucketGet in class AbstractHashMap<K,V>
        Parameters:
        h - the hash value of the relevant bucket
        k - the key of interest
        Returns:
        associate value (or null, if no such entry)
      • bucketPut

        protected V bucketPut(int h,
                              K k,
                              V v)
        Associates key k with value v in bucket with hash value h, returning the previously associated value, if any.
        Specified by:
        bucketPut in class AbstractHashMap<K,V>
        Parameters:
        h - the hash value of the relevant bucket
        k - the key of interest
        v - the value to be associated
        Returns:
        previous value associated with k (or null, if no such entry)
      • bucketRemove

        protected V bucketRemove(int h,
                                 K k)
        Removes entry having key k from bucket with hash value h, returning the previously associated value, if found.
        Specified by:
        bucketRemove in class AbstractHashMap<K,V>
        Parameters:
        h - the hash value of the relevant bucket
        k - the key of interest
        Returns:
        previous value associated with k (or null, if no such entry)
      • entrySet

        public java.lang.Iterable<Entry<K,V>> entrySet()
        Returns an iterable collection of all key-value entries of the map.
        Returns:
        iterable collection of the map's entries