我遇到了一個有趣的問題,我敢肯定是HashMap
的錯。請看下面的調試代碼(AMAP是一個HashMap,關鍵是傳遞給此方法的值)HashMap說鑰匙不存在,即使它
System.out.println("getBValues - Given: " + key);
System.out.println("getBValues - Contains Key: " + AMap.containsKey(key));
System.out.println("getBValues - Value: " + AMap.get(key));
for(Map.Entry<A,HashSet<B>> entry : AMap.entrySet()) {
System.out.println("getBValues(key) - Equal: " + (key.equals(entry.getKey())));
System.out.println("getBValues(key) - HashCode Equal: "+(key.hashCode() == entry.getKey().hashCode()));
System.out.println("getBValues(key) - Key: " + entry.getKey());
System.out.println("getBValues(key) - Value: " + entry.getValue());
}
現在,在這個地圖我插入一個鍵(頻道)和值。後來我試着和get()
獲得價值背部和運行這個調試代碼,在我的情況下,給出了這樣的輸出:
getBValues - Given: Channel(...)
getBValues - Contains Key: false <--- Doesnt contain key?!
getBValues - Value: null <--- Null (bad)
getBValues(key) - Equal: true <--- Given key and AMap key is equal
getBValues(key) - HashCode Equal: true
getBValues(key) - Key: Channel(Same...)
getBValues(key) - Value: [] <--- Not null (This is the expected result)
正如你所看到的,取出由HashMap中的鍵直接不工作,但我通過循環得到完全一樣的密鑰,這意味着它只能在get()
找不到。我的問題是什麼會造成這種情況? get()
如何找不到存在的密鑰?
我會提供一些這樣的代碼示例,但我似乎無法獨立重現此。
有什麼建議可能會導致這種情況?
*「......我很確定是HashMap的錯誤」*。跟着我重複一遍。 「錯誤在**我的**代碼中。」 - http://storm.stackexchange.com/questions/1785/what-should-every-programmer-know-about-programming/1842#1842 – 2011-05-14 04:54:09
@Stephen我的意思是更多的「在HashMap中的某些'功能'造成這個「 – TheLQ 2011-05-14 04:59:01