Why is it possible to create a keySet TreeMap loop and get .containsKey == false ?
for (Object thisObject : map.keySet()) { if (!map.containsKey(thisObject)) { System.out.println("This line should be never reached."); } }
After many, many different iterations and challenges this line gets. A map.get(thisObject) will return null . But debug shows that the key ( same link, value and hash ) and the actual value are on the map. The map is a small (25 elements) TreeMap<Long, Double>
UPDATE:
As @rgettman guessed, this is a custom Comparator type used when building the TreeMap (I didnโt see it because it was created from another class). This comparator was just (I think) a copy inserted from here
Comparator change:
public int compare(Object a, Object b) { if((Double)base.get(a) > (Double)base.get(b)) { return 1; } else if((Double)base.get(a) == (Double)base.get(b)) { return 0; } else { return -1; } }
to
... } else if(base.get(a).equals(base.get(b))) { return 0; ...
fixes the problem. The reason this problem appeared immediately after millions of operations was because there were no cases where the card had two identical values โโfor two different keys, since this is very unlikely in the context.
So, with:
25151l, 1.7583805400614032 24827l, 1.7583805400614032
he does not work.
Thanks for your help!
source share