Search code examples
scalaseqsortedset

SortedSet to a seq preserving the (sorted) order


I have a SortedSet and I need to convert it to a Seq while preserving the order (because I need to call map with a seq output).

A couple of tests in the REPL shows that toSeq keep the order as well as toIndexedSeq but I need to be sure: is it guaranteed to keep the order?

Otherwise I can use .toSeq.sorted but that is seriously overkill...


Solution

  • Functions such as toSeq are implemented by building a new collection (for toSeq it is a mutable Buffer) and added the elements in the iteration order of the original collection (the SortedSet). That means that the resulting sequence will always be ordered.