簡體   English   中英

在“put”期間,Java Hashtable使用新密鑰覆蓋現有密鑰

[英]Java Hashtable overwrites existing key with new key during 'put'

我正在嘗試使用Eclipse將條目添加到Java中的哈希表中。 在put操作期間,只有一個鍵被新鍵和值覆蓋。 哈希表的計數被正確維護,但其中一個(鍵,值)對丟失。

這是我的示例代碼:

ArrayList<Double> list;
Hashtable<Val,ArrayList<Double>> numbers = new Hashtable<Val,ArrayList<Double>>();

while((line = brMyHashval.readLine()) != null)
{
    if(!(line.isEmpty()))
    {               
        String[] temp;
        temp = line.split(" ");      
        eDouble = Double.parseDouble(temp[2].toString());

        Val key = new Val(Double.parseDouble(temp[0].toString()) ,Double.parseDouble(temp[1].toString()) );

        if(!(numbers.containsKey(key)))
        {
            list = new ArrayList<Double>();
            numbers.put(key, list);

        }
        else
        {
            list = numbers.get(key);
        }
        list.add(eDouble); 
     }
}

我曾經習慣在eclipse中內置'hashcode'和'equals'方法來比較類對象。

輸入文本文件:

1.0 2.0 9.0
3.0 4.0 9.0
5.0 6.0 9.0
1.0 2.0 8.0
5.0 6.0 8.0
1.0 2.0 7.0
**7.0 8.0 7.0** // After this point a new hash entry gets added for key(7,8), But key (1,2) get deleted from the hashtable, though count gets increased to 4.
3.0 4.0 7.0
5.0 6.0 10.0
1.0 2.0 10.0
1.0 3.0 10.0
1.0 4.0 10.0

為什么密鑰會在特定時刻被刪除。

[編輯] hashcode和equals:我用eclipse自動導入這些方法//(x,y)是(a,b)

  class Val

{
    double x;
    double y;

Val(double X, double Y)
{
    x = X;
    y = Y;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    long temp;
    temp = Double.doubleToLongBits(x);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    temp = Double.doubleToLongBits(y);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Val other = (Val) obj;
    if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
        return false;
    if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
        return false;
    return true;
}

}

問題是您正在使用調試器來檢查HashMap的內容。

我假設密鑰(1,2)和(7,8)都保存在用於保存密鑰的HashTable的同一個槽中。 當(7,8)被添加時,(1,2)被移動到(7,8)的“后面” - 你必須檢查(7,8)條目的next條目。

在此輸入圖像描述

將以下內容添加到代碼的末尾,以查看HashMap中的真實內容:

    for (Val key : numbers.keySet()) {
        System.out.printf("%.1f %.1f: %s%n", key.x, key.y, numbers.get(key));
    }

上面的Sumindra意味着您要將自定義類用作Map中的鍵,您必須使用指定的equals()和hashCode()方法。 做以下(例如:

public boolean equals(K other) {
    return a == other.a && b == other.b;
}

public int hashCode() {
    return new Double(a).hashCode() ^ new Double(b).hashCode();
}

這保證了:

  • 如果兩個K對象具有相同的成員,則它們會相等
  • 如果兩個K對象具有相同的成員,則它們具有相同的hashCode

這是Map鍵對象的要求。

確保哈希和等於滿足他們的要求。

每個實例應該有一個唯一的哈希值,如果它們只相等,則equals應為true。 誤報意味着誤報值映射到同一個鍵。 看到這個鏈接。

我無法重現你的問題,這是我正在運行的正確代碼(沒有簡化,因為其他答案使它盡可能接近你的原始問題)。

public class HashProblem {

    public static class Val {
        private double x;
        private double y;

        public Val(double x, double y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            long temp;
            temp = Double.doubleToLongBits(x);
            result = prime * result + (int) (temp ^ (temp >>> 32));
            temp = Double.doubleToLongBits(y);
            result = prime * result + (int) (temp ^ (temp >>> 32));
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Val other = (Val) obj;
            if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
                return false;
            if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
                return false;
            return true;
        }
    }

    public static void main(String... args) throws Exception {
        ArrayList<Double> list;
        String line;
        BufferedReader brMyHashval = new BufferedReader(new InputStreamReader(new FileInputStream("HashProblem.txt")));
        Hashtable<Val, ArrayList<Double>> numbers = new Hashtable<Val, ArrayList<Double>>();

        while ((line = brMyHashval.readLine()) != null) {
            if (!(line.isEmpty())) {
                String[] temp;
                temp = line.split(" ");
                Double eDouble = Double.parseDouble(temp[2].toString());

                Val key = new Val(Double.parseDouble(temp[0].toString()), Double.parseDouble(temp[1].toString()));

                if (!(numbers.containsKey(key))) {
                    list = new ArrayList<Double>();
                    numbers.put(key, list);
                    System.err.println("Created " + key.x + " " + key.y);
                } else {
                    list = numbers.get(key);
                }
                list.add(eDouble);
                System.err.println("Inserted into " + key.x + " " + key.y + " value " + eDouble + " size " + list.size() + " " + list);
            }
        }
    }

我從日志記錄中獲得的輸出是

Created 1.0 2.0
Inserted into 1.0 2.0 value 9.0 size 1 [9.0] 
Created 3.0 4.0
Inserted into 3.0 4.0 value 9.0 size 1 [9.0]
Created 5.0 6.0
Inserted into 5.0 6.0 value 9.0 size 1 [9.0]
Inserted into 1.0 2.0 value 8.0 size 2 [9.0, 8.0]
Inserted into 5.0 6.0 value 8.0 size 2 [9.0, 8.0]
Inserted into 1.0 2.0 value 7.0 size 3 [9.0, 8.0, 7.0]
Created 7.0 8.0
Inserted into 7.0 8.0 value 7.0 size 1 [7.0]
Inserted into 3.0 4.0 value 7.0 size 2 [9.0, 7.0]
Inserted into 5.0 6.0 value 10.0 size 3 [9.0, 8.0, 10.0]
Inserted into 1.0 2.0 value 10.0 size 4 [9.0, 8.0, 7.0, 10.0]
Created 1.0 3.0
Inserted into 1.0 3.0 value 10.0 size 1 [10.0]
Created 1.0 4.0
Inserted into 1.0 4.0 value 10.0 size 1 [10.0]

這不是你所期望的嗎?

其他答案有關於簡化hashCode和equals的好處。 此外,您不需要對已經是字符串的對象執行toString()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM