Question
Which practice avoids a common mistake with HashSet vs TreeSet?
- Ignore the HashSet vs TreeSet issue and rely on team discipline instead of APIs or contracts.
- Silence the HashSet vs TreeSet problem by using raw types, broad catches, or shared mutable state.
- Do not choose TreeSet when hash-based uniqueness is enough and sorting adds no product value.
- Prefer the version of HashSet vs TreeSet that makes behavior less predictable as long as the code compiles.
Hint
Look for the option that protects correctness instead of hiding the problem.
Answer and rationale
Correct answer: C. Do not choose TreeSet when hash-based uniqueness is enough and sorting adds no product value.
Do not choose TreeSet when hash-based uniqueness is enough and sorting adds no product value. This is a common failure mode in real Java code and a frequent interview follow-up.
Track: Java