The correct statement about HashSet and TreeSet in Java is:

C. HashSet is unordered, while TreeSet is ordered.

Explanation:

  • HashSet: A HashSet in Java does not guarantee any specific order for its elements. The elements are stored in a way that allows for efficient lookups (using hashing), but the order of retrieval may be unpredictable.

  • TreeSet: In contrast, a TreeSet maintains its elements in a sorted order. This order is based on the natural ordering of the elements or a custom comparator provided during the set's creation. The sorted nature of TreeSet makes it suitable for situations where you need to access elements in a specific order.

HashSet and TreeSet: Understanding Ordered vs. Unordered Sets in Java