-2

I know that a TreeSet is ordered. But does that guarantee that every time I create an iterator, it is going to go through the elements of the collection in the same order (provided that I am not adding/removing any elements)?

Eran
  • 387,369
  • 54
  • 702
  • 768
Mackiavelli
  • 432
  • 2
  • 12

1 Answers1

9

Yes, as the Javadoc states :

Iterator java.util.TreeSet.iterator()

Returns an iterator over the elements in this set in ascending order.

Specified by: iterator() in NavigableSet, iterator() in Set, Overrides: iterator() in AbstractCollection Returns: an iterator over the elements in this set in ascending order

Eran
  • 387,369
  • 54
  • 702
  • 768