Skip to main content
Engineering LibreTexts

11.1: Exercise 9

  • Page ID
    12792
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In MyHashMap.java, I provide the outline of a hash table that grows when needed. Here’s the beginning of the definition:

    public class MyHashMap<K, V> extends MyBetterMap<K, V> implements Map<K, V> {
    
        // average number of entries per sub-map before we rehash
        private static final double FACTOR = 1.0;
    
        @Override
        public V put(K key, V value) {
            V oldValue = super.put(key, value);
    
            // check if the number of elements per sub-map exceeds the threshold
            if (size() > maps.size() * FACTOR) {
                rehash();
            }
            return oldValue;
        }
    }
    

    MyHashMap extends MyBetterMap, so it inherits the methods defined there. The only method it overrides is put which calls put in the superclass — that is, it calls the version of put in MyBetterMap — and then it checks whether it has to rehash. Calling size returns the total number of entries, n. Calling maps.size returns the number of embedded maps, k.

    The constant FACTOR, which is called the load factor, determines the maximum number of entries per sub-map, on average. If \( n > (k * FACTOR) \), that means \( \dfrac{n}{k} > FACTOR \), which means the number of entries per sub-map exceeds the threshold, so we call rehash.

    Run ant build to compile the source files. Then run ant MyHashMapTest. It should fail because the implementation of rehash throws an exception. Your job is to fill it in.

    Fill in the body of rehash to collect the entries in the table, resize the table, and then put the entries back in. I provide two methods that might come in handy: MyBetterMap.makeMaps and MyLinearMap.getEntries. Your solution should double the number of maps, k, each time it is called.


    This page titled 11.1: Exercise 9 is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?