简体   繁体   中英

How to write a put method for a HashMap in Java?

I've been writing a put method for my HashMap code and I cannot seem to understand what is the problem.

My instructions read as:

If the given key is already present in the HashMap, the old value should be replaced with the new value. If a collision occurs, linear probing should be used to find the next "open" array index. If an additional element will be added to the map and the load factor is greater than or equal to the MAX_LOAD_FACTOR, then the map should be rehashed before identifying the bucket to try to store the additional element.

Therefore, I'm not sure if it's my put method that isn't working properly, or my rehash one. Therefore, I am going to post both of them. Hopefully it makes sense.

public class HashMap<K,V>
{
private final double MAX_LOAD_FACTOR = 0.75;
private HashEntry[] elementData;
private final HashEntry REMOVED = new HashEntry(null, null);
private int size;

public HashMap()
{
    this.elementData = new HashMap.HashEntry[10];
    size = 0;
}
public void put(K key, V value)
{
int hash = hashFunction(key);
    if (!containsKey(key))
    {
        if(loadFactor() >= MAX_LOAD_FACTOR)
        {
            rehash();
        }
        while(elementData[hash] != null)
        {
            hash = (hash + 1) % elementData.length;
        }
        elementData[hash] = new HashEntry(key, value);
        size++;
    }
    else
    {
        while(elementData[hash] != null || (elementData[hash] != REMOVED && !elementData[hash].getKey().equals(key)))
        {
            hash = (hash + 1) % elementData.length;
        }
            elementData[hash].value = value;
    }
}
 private int hashFunction(Object key)
{
    return Math.abs(key.hashCode()) % elementData.length;
}
private double loadFactor()
{
    return (double) size / elementData.length;
}
private void rehash()
{
    HashEntry[] oldElementData = elementData;
    elementData = new HashMap.HashEntry[2 * oldElementData.length];
    size = 0;
    for (int i = 0; i < oldElementData.length; i++)
    {
        HashEntry current = oldElementData[i];
        if(current != null)
        {
            put(current.getKey(), current.getValue());
        }
    }
}
 public class HashEntry
{
    private K key;
    private V value;

    public  HashEntry(K key, V value)
    {
        this.key = key;
        this.value = value;
    }
    public K getKey()
    {
        return key;
    }
    public V getValue()
    {
        return value;
    }
 }
}

drop down any ideas that will help out. Thanks.

I found my issue that I need to reassign the hash after I rehashed, therefore I have created this.

public void put(K key, V value)
{
    int hash = hashFunction(key);
    if (!containsKey(key))
    {
        if(loadFactor() >= MAX_LOAD_FACTOR)
        {
            rehash();
        }
        hash = hashFunction(key);
        while(elementData[hash] != null && !elementData[hash].equals(REMOVED))
        {
                hash = (hash + 1) % elementData.length;
        }
        elementData[hash] = new HashEntry(key, value);
        size++;
    }
    else
    {
        while(elementData[hash] != null && (!elementData[hash].equals(REMOVED) && !elementData[hash].getKey().equals(key)))
        {
            hash = (hash + 1) % elementData.length;
        }
            elementData[hash].value = value;
    }
}

That's about it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM