Java TreeMap contains the key, but the call to containsKey returns false (even the key is exactly the same immutable object)

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!

+4
source share
3 answers

I just executed the code, this case made me return true .

  TreeMap<Long,Double> otm = new TreeMap<Long, Double>(); otm.put(1L, 1.0); otm.put(2L, 2.0); for (Object thisObject : otm.keySet()) { System.out.println(otm.containsKey(thisObject)); } 

Could you provide us with the data that you entered in TreeMap. Thanks

This is an implementation of putKey (Object key) from JavaDocs

containsKey

Boolean ContainsKey (object key)

Returns true if this map contains a mapping for the specified key.

More formally returns true if and only if this map contains a mapping for key k such that, (key == null? K == null: key.equals (k)) . (There can be at most one such mapping.)

Parameters:

 key - key whose presence in this map is to be tested Returns: true if this map contains a mapping for the specified key Throws: ClassCastException - if the key is of an inappropriate type for this map (optional) NullPointerException - if the specified key is null and this map does not permit null keys (optional 

Hope this helps.

0
source

You should have made changes to the backup entrySet()/Map.Entry , thereby changing the order of the keys, thereby obtaining an unsuccessful containsKey search.

0
source

Most of these implementation classes rely on both hashCode () and equals (), which are supported and correct.

If you really have the same hash of objects, try equality. The answer suggested by me is that they do not match.

Otherwise, the script should be small enough for you to publish objects and / or their corresponding hashcode and equals method.

0
source

Source: https://habr.com/ru/post/1495793/


All Articles