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

Discussion

Which one of the following statements about what defines the iteration order of a HashSet is true?

  • A.Nothing that the class promises; the order is unspecified and may change.
  • B.The order in which the elements were inserted.
  • C.The natural order of the elements.
  • D.The reverse of the insertion order.

Answer: A

Like the HashMap it is built on, it makes no ordering guarantee and the order can change as the table resizes. Code that needs a predictable order should use LinkedHashSet for insertion order or TreeSet for sorted order.

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

Post your comment

Please log in or register to post a comment.