map - Java TreeMap contains a key but a containsKey call returns false (even the key is exactly the same unchanged object) -


why possible loop keyset of treemap , getting .containskey == false?

for (object thisobject : map.keyset()) {     if (!map.containskey(thisobject)) {         system.out.println("this line should never reached.");     } } 

after lot, lot of different iterations , calls line gets hit. map.get(thisobject) return null. debug shows key (same reference, value , hash) , actual value in map. map small (25 elements) treemap<long, double>

update:

as guessed @rgettman theres custom sort comparator used @ construction of treemap (didn't see because constructed class). comparator (i guess) copy pasted here

changing comparator:

  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 problem. reason why problem showing after millions of operation there no cases map had 2 similar values 2 different keys, unlikely in context.

so at:

25151l, 1.7583805400614032 24827l, 1.7583805400614032 

it fails.

thanks help!

you must have made changes backing entryset()/map.entry thereby changing key orders, thereby having failed search containskey.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -