简体   繁体   English

如何在Java中为HashMap编写put方法?

[英]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. 我一直在为我的HashMap代码编写一个put方法,我似乎无法理解这是什么问题。

My instructions read as: 我的说明如下:

If the given key is already present in the HashMap, the old value should be replaced with the new value. 如果给定键已存在于HashMap中,则旧值应替换为新值。 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. 如果将向地图添加其他元素且加载因子大于或等于MAX_LOAD_FACTOR,则应在识别存储桶之前重新映射该映射以尝试存储其他元素。

Therefore, I'm not sure if it's my put method that isn't working properly, or my rehash one. 因此,我不确定这是我的put方法无法正常工作,还是我的rehash方法。 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 就是这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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