我遇到了一个有趣的问题,我敢肯定是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