Question
Which practice avoids a common mistake with Comparable vs Comparator?
- Ignore the Comparable vs Comparator issue and rely on team discipline instead of APIs or contracts.
- Silence the Comparable vs Comparator problem by using raw types, broad catches, or shared mutable state.
- Prefer the version of Comparable vs Comparator that makes behavior less predictable as long as the code compiles.
- Do not let comparison logic disagree with equality semantics when the results feed sorted sets or maps.
Hint
Look for the option that protects correctness instead of hiding the problem.
Answer and rationale
Correct answer: D. Do not let comparison logic disagree with equality semantics when the results feed sorted sets or maps.
Do not let comparison logic disagree with equality semantics when the results feed sorted sets or maps. This is a common failure mode in real Java code and a frequent interview follow-up.
Track: Java