How to use Sets as keys in Java Maps

前端 未结 4 823
借酒劲吻你
借酒劲吻你 2020-12-06 04:58

I have a Map that uses a Set for the key type, like this:

Map, Val> map;

When I query map.containsKey(myBunchOfThing

4条回答
  •  天涯浪人
    2020-12-06 05:41

    Did you modify the set after insertion? If so, it's possible the set got sorted into a different bucket than the one it's looking in. When iterating, it does find your set, because it looks in the whole map.

    I believe the contract for HashMap states you're not allowed to modify the hashcode for objects used as a key,

提交回复
热议问题