Java: In Java, which comparison about HashSet vs TreeSet is accurate?

Difficulty:

Easy

Questions:

1

Time Limit:

2 minutes

Passing Score:

100%

Question

In Java, which comparison about HashSet vs TreeSet is accurate?

  1. TreeSet relies on natural ordering or a Comparator, while HashSet relies on equals and hashCode.
  2. There is no practical difference in behavior, performance, or API design when HashSet vs TreeSet is involved.
  3. The oldest option related to HashSet vs TreeSet is always the right production choice regardless of context.
  4. Differences around HashSet vs TreeSet are mostly cosmetic, so correctness and maintainability do not change.

Hint

Focus on the behavior or tradeoff, not just the keyword.

Answer and rationale

Correct answer: A. TreeSet relies on natural ordering or a Comparator, while HashSet relies on equals and hashCode.

TreeSet relies on natural ordering or a Comparator, while HashSet relies on equals and hashCode. This is the comparison that usually separates memorization from understanding.

Track: Java