I was using ByteArray as key for my Map in the project I am working on and noticed that it always returns false when I check containsKey
on the map with the exact same key. Then I tried to make it into a minimal working example like the following:
val key1 = byteArrayOf(49, 53, 48, 56, 55, 52, 48, 58, 100, 48, 56, 50, 52, 57, 97, 48, 52, 49, 50, 48, 100, 97, 101, 50, 96, 57, 56, 100)
val key2 = byteArrayOf(49, 53, 48, 56, 55, 52, 48, 58, 100, 48, 56, 50, 52, 57, 97, 48, 52, 49, 50, 48, 100, 97, 101, 50, 96, 57, 56, 100)
val myMap = mutableMapOf<ByteArray, Int>(key1 to 1, key2 to 2)
myMap.containsKey(key1)
The same implementation in my project returns false, but it returns true in this MWE. I noticed a similar behavior when I do myMap.putIfAbsent(key, defaultValue)
. In my project, it adds another entry to the Map with exactly the same key but when I construct a MWE, it does not.
Here’s a screen shot of the map with 2 identical keys:
I know from this post that you cannot use plain ==
to compare 2 byte arrays. However, the same behavior in Map makes me have to come up with ugly work-arounds.
And I am baffled by the inconsistency in behavior between in my project and in the MWE.