#Help with hash map

44 messages · Page 1 of 1 (latest)

nimble pier
#

Hi, can someone help me with this hash map implementation, I tried this line on the TODO but it shows an error.


    // DEFAULT_CAPACITY has to be a prime number
    private static final int DEFAULT_CAPACITY = 23;
    private static final float DEFAULT_LOAD_FACTOR = 0.5f;
    private static final int CAPACITY_INCREASE_FACTOR = 2;

    private Node<KeyType, DataType>[] map;
    private int size = 0;
    private int capacity;
    private final float loadFactor; // Compression factor

    public HashMap() { this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); }

    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    /** TODO
     * Define capacity, loadFactor and map
     */
    public HashMap(int initialCapacity, float loadFactor) {
        this.loadFactor = Math.abs(loadFactor);
        return new HashMap<Integer, Integer>(initialCapacity, loadFactor); ---> ERROR
    }```
mystic citrusBOT
#

This post has been reserved for your question.

Hey @nimble pier! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

deep panther
#

you don't return from constructors

nimble pier
deep panther
#

you have to use them yourself directly

#

you have to make the implementation, after all

nimble pier
deep panther
#

yeah

nimble pier
# deep panther yeah

not sure how to define the map, what do I put after the equal? bc when I try this.map; it doesn't work

deep panther
#

well, what should its capacity be?

#

this is the thing that's holding your data

nimble pier
#

ok thanks

nimble pier
# deep panther this is the thing that's holding your data
        int keyHash = key.hashCode() % capacity;
        return Math.abs(keyHash);
    }
    private boolean needRehash() {
        return size > capacity * loadFactor;
    }
    public int size() {
        return size;
    }
    public int capacity(){
        return capacity;
    }
    public boolean isEmpty() {
        return size == 0;
    } 

/** TODO Worst Case : O(m + n)
     * m = Capacity of the hashmap
     * n = number of elements in the hashmap
     * Increases capacity to the next prime number after capacity * CAPACITY_INCREASE_FACTOR and
     * reassigns all contained values
     */
    private void rehash() {
}``` Do you have any idea to implement the function rehash?
deep panther
#

copy it into a new array

nimble pier
deep panther
#

you need to make a new map then copy the old map's contents into there

nimble pier
deep panther
#

copy straight into the new array

nimble pier
deep panther
#

you need to first make a new map, then just do map[i] = oldMap[i]

#

you aren't using nextPrime there

nimble pier
#
  
  map[i] = oldMap[i];```?
#

In the instructions of the rehash exercise it said this : /** TODO Worst Case : O(m + n) * m = Capacity of the hashmap * n = number of elements in the hashmap * Increases capacity to the next prime number after capacity * CAPACITY_INCREASE_FACTOR and * reassigns all contained values */

deep panther
#

no, you need to make it a new, larger array

#

that's what increasing the capacity is

nimble pier
#

it's what I tried to do here nextPrime( 2 * map.length); I doubled the capacity

deep panther
#

does nextPrime set map?

nimble pier
#

no, not sure how to set the new map with a larger array

#
        HashMap<KeyType, DataType>[] oldMap = map;
        for( int i = 0; i < oldMap.length; i++ )
            map[i] = oldMap[i];
    }``` I have this... what do I modify or erase?
deep panther
#

you need to make a new map, like you did in the constructor

nimble pier
#

ok so I have this ```HashMap<Integer, Integer> newMap = new HashMap<Integer, Integer>;

deep panther
#

no

#

you don't do new HashMap ever

#

don't make your structure recursive

#

your internal map is an array, make that

#

...have you not done it in the constructor?

nimble pier
#
        this.loadFactor = Math.abs(loadFactor);
        this.capacity = initialCapacity;
    }``` I only had this before
deep panther
#

you need to initialize the map

#

__initial__Capacity

deep panther
#

yes, initialize it to an array of some size

deep panther
nimble pier
#

this.map = new map[initialCapacity]; ??