Open gvlasov opened 9 years ago
@yegor256 please dispatch this issue
@Suseika but how can we solve this problem?
@yegor256 We can (the following are steps, not alternatives):
Map
passed to ArrayMap(Map)
constructor has non-deterministic ordering, then ArrayMap
's ordering will be non-deterministic as well.Of course, that will mean that we will be unable to do new ArayMap<>(hashMap)
and have it iterated in a predictable manner, but that feature was unreliable in the first place.
@Suseika as far as I understood your original ticket description, the problem exists only when we pass non-Comparable
elements as keys, right?
@yegor256 That's right.
@Suseika so in this case we don't really care what's going on in the map. We can't sort properly anyhow. This usage of String
is just our internal workaround. There is no guarantee to the user about anything. I don't see what exactly is a bug?
If I understand it correctly,
ArrayMap
innards are sorted in order to have deterministic ordering of its elements.If that's true, then there is an error.
ArrayMap#Cmp
comparator compares string representation of objects if they are notComparable
. String representation is not a deterministic function, because ifObject#toString()
is not overridden, then it is a function ofObject#hashCode()
, which is not deterministic by definition. Thus, ordering ofArrayMap
is not deterministic if its contents are notComparable
and don't overrideObject#toString()
in a deterministic manner.