0

I wrote a remove method for a HashMap and I'm getting errors that it's not returning the correct value.

The instructions read:

The remove(Object) method should remove and return the value associated with the given key if the key is found in the map. The removed entry should be replaced with the REMOVED attribute. If the map does not contain the key, this method has no effect.

Therefore, here is part of my code that is relevant to my code so it's not long code.

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 V remove(Object key)
{
    int h = hashFunction(key);
    while (elementData[h] != elementData[0] && elementData[h] != elementData[h].getKey())
    {
        h = (h + 1) % elementData.length;
    }
    if (elementData[h] == elementData[h].getKey())
    {
        elementData[h] = REMOVED;   // "removed" flag value
        size--;
        return elementData[h].getValue();
    }
    return elementData[h].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;
    }
 }
}

The message I'm receiving from my error is that my size is incorrect and also, I'm returning the wrong value. Could I get help on this? I don't know where the issue occurs.

Adan Vivero
  • 422
  • 12
  • 36
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas May 04 '19 at 20:53
  • why do you implement your own hash map and don't use the one from java standard? I think your mistake is comparing `elementData[h] == key` the key is not the same as the hashmap entry. But I see there more problems with indices, you will get some ArrayIndexOutOfBoundException ... – fairtrax May 04 '19 at 20:54
  • @fairtrax what do you mean implement my own hash map? – Adan Vivero May 04 '19 at 21:07
  • @fairtrax I updated my code, but It's still not correct. – Adan Vivero May 04 '19 at 21:09
  • you still dont compare the keys, but the entry structure with key. Why do you write the code? Just use the existing from java! https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html – fairtrax May 04 '19 at 21:23
  • @fairtrax Because it's his task to write a hash map. Isn't it obvious from the question? – kaqqao May 04 '19 at 21:42
  • I changed up my method, now I'm getting a message that my size is incorrect, I don't see how, but could you guys spot out the problem? – Adan Vivero May 06 '19 at 00:30

1 Answers1

1
public V remove(Object key)
{
    int h = hashFunction(key);

    while (elementData[h] != null)
    {
        if (elementData[h].getKey() != null && elementData[h].getKey().equals(key)) // linear probing to search
        {
            V store;
            store = elementData[h].value;
            elementData[h] = REMOVED;
            size--;
            return store;
        }
        h = (h + 1) % elementData.length;
    }
    return null;
}

I almost had it, I was just suppose to return null and at the end and store the value in a different variable.

Adan Vivero
  • 422
  • 12
  • 36