Glains has already answered your question. Still I'd like to add some: there are interfaces in Java library which provides contracts to the developers, making promises that they can be used in which ways.
Just like your question, basic interface Set
A collection that contains no duplicate elements.
And your implementor
HashSet, which already clearly specifies the fact that order is not guaranteed, in which case you should NOT expect any order-related promises.
It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
And specifically, Java provides another interface SortedSet instead to ensure order you are asking for:
A Set that further provides a total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time.
So in your case, you should find an implementor
that implements the interface SortedSet
, like TreeSet.
Normally we care not much about the implementation details since they can be adjusted/changed without our knowing, while on the other hand the promises the library developers made in the official documentation will be kept always.