I had an interesting problem, and I'm sure this is a HashMap error. Consider the following debugging code (AMap is HashMap, key is the value passed to this method)
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()); }
Now on this map I insert one key (Channel) and a value. Later, I will try to return the value using get() and run this debugging code, which in my case gives this output:
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)
As you can see, fetching from HashMap does not work directly, but through the loop I get the same key, that is, it simply cannot be found there using get() . My question is what might cause this? How to get() not find a key that exists?
I would provide some sample code, but I cannot reproduce this myself.
Any suggestions on what could be causing this?
Thelq source share