This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why is the result of scala.collection.immutable.TreeSet[A]#toString unsorted ?

No replies
Kota Mizushima
Joined: 2009-09-07,
User offline. Last seen 42 years 45 weeks ago.

Hello. In scala 2.8.0.RC2 REPL, I noticed the following behavior:

scala> val ts = TreeSet("a", "b", "c", "d")
ts: scala.collection.immutable.TreeSet[java.lang.String] = TreeSet(a, b, c, d)

scala> val ts2 = ts + "e"
ts2: scala.collection.immutable.TreeSet[java.lang.String] =
TreeSet(e, a, b, c, d) // unsorted

Of course, I know it is not bug because ts.toList returns sortred list
correctly:

scala> ts2.toList
res0: List[java.lang.String] = List(a, b, c, d, e)

However, I think that this behavior is unkind. Because TreeSet
extends SortedSet, users of TreeSet
(include me) may expect that the result of toString is sorted. Then,
I hope that the result of
TreeSet[A]#toString is sorted. Is this demand incorrect?

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland