我遇到了一个有趣的问题,我很确定是HashMap的错.考虑以下调试代码(AMap是HashMap,key是传递给此方法的值)
- 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());
- }
现在在这个Map中我插入一个键(Channel)和值.后来我尝试用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中获取密钥不起作用,但循环通过I得到完全相同的密钥,这意味着它就在get()中找不到它.我的问题是什么会导致这个? get()如何找不到存在的密钥?
我会提供一些示例代码,但我似乎无法独立重现.
有关可能导致此问题的任何建议?