Java: What deeper point about HashSet vs TreeSet should a senior Java developer mention?

Difficulty:

Hard

Questions:

1

Time Limit:

2 minutes

Passing Score:

100%

Question

What deeper point about HashSet vs TreeSet should a senior Java developer mention?

  1. At senior level, the right answer is that HashSet vs TreeSet exists mostly for historical syntax reasons.
  2. At senior level, the JVM removes the tradeoffs around HashSet vs TreeSet, so design choices barely matter.
  3. At senior level, any approach to HashSet vs TreeSet is equally correct if it compiles and passes a small test.
  4. A comparator that is inconsistent with equals can make sorted-set behavior surprising during lookups and deduplication.

Hint

Look beyond syntax and explain the runtime, API, or design consequence.

Answer and rationale

Correct answer: D. A comparator that is inconsistent with equals can make sorted-set behavior surprising during lookups and deduplication.

A comparator that is inconsistent with equals can make sorted-set behavior surprising during lookups and deduplication. This is the kind of tradeoff-aware answer senior interviews usually expect.

Track: Java