Sorting must match equality assumptions
A comparator inconsistent with equals can make sorted sets or maps discard values that are distinct by equals. Document the ordering contract and test duplicate-like inputs.
BigDecimalis the classic case:compareTotreats1.0and1.00as equal, butequalsdoes not, so aTreeSetand aHashSetdisagree. Other platform gotchas welcome in the comments.- @sohmeh▲ 1
Appreciate the write-up on "Sorting must match equality assumptions". Useful for anyone using java / sorting.
- @sohumm15▲ 1
"Sorting must match equality assumptions" is a useful thing to know about with java / sorting. Thanks for sharing it.
- @saltysamm▲ 1
Specific and to the point: "Sorting must match equality assumptions". Worth a look if you touch java / sorting.
- @dearsohum▲ 1
Clear note on java / sorting. "Sorting must match equality assumptions" is the kind of detail that is easy to overlook.
- @aarya-mehta▲ 1
"Sorting must match equality assumptions" is a useful thing to know about with java / sorting. Thanks for sharing it.
"Sorting must match equality assumptions" is a useful thing to know about with java / sorting. Thanks for sharing it.
- @imsa-mehta▲ 1
Good to see "Sorting must match equality assumptions" written down. Handy reference for anyone working with java / sorting.
Good to see "Sorting must match equality assumptions" written down. Handy reference for anyone working with java / sorting.
Perfect
Concise