Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider what TreeSet offers beyond the Set interface. Which statement is correct?

  • A.Rejection of duplicates, which HashSet does not provide.
  • B.Navigation methods such as first, last, higher and lower.
  • C.Constant-time membership tests, because the elements are hashed as well as sorted.
  • D.Synchronized access, since a sorted structure must be locked while rebalancing.

Answer: B

Correct answer: Navigation methods such as first, last, higher and lower.

TreeSet implements NavigableSet, so beyond the plain Set contract it offers first, last, higher, lower, ceiling, floor and the range views headSet, tailSet and subSet. What it gives up is speed: operations are logarithmic rather than constant time, because the elements are held in a balanced tree rather than hashed. It is not synchronized, and every set rejects duplicates.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.