-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mutable.ListMap should preserve insertion order #9893
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9893?orig=1 |
@SethTisue said: mutable.ListMap is implemented as a very simple wrapper around scala.collection.immutable.List. As a natural consequence, new items are added to the front. immutable.ListMap doesn't work that way — it's implemented on top of a private Node class. internally, it adds items on the front, but then it does |
@SethTisue said: in ascending order of ambitiousness, possibilities include:
Note that wrapping immutable.List is a really terrible way to implement mutable.ListMap — tons of unnecessary copying. |
@Ichoran said (edited on Aug 17, 2016 9:02:39 PM UTC): For immutable.ListSet/Map, I think either order is okay as long as it is incredibly clear what the expectation is, and it is followed for every operation. That is: head gives you the most recent item and iteration is always from recent to older; or head gives you the oldest item and iteration is always from recent to older. And this property should be maintained through map, flatMap, etc. (which will mean using builders to create the underlying links because otherwise the order will switch every time you map). |
Dmytro Kondratiuk (dk14) said (edited on Oct 19, 2016 1:29:07 AM UTC): scala> val m = collection.mutable.LinkedHashMap[Int, Int]()
m: scala.collection.mutable.LinkedHashMap[Int,Int] = Map()
scala> m += 1 -> 1
res42: m.type = Map(1 -> 1)
scala> m += 2 -> 2
res43: m.type = Map(1 -> 1, 2 -> 2)
scala> m += 3 -> 3
res44: m.type = Map(1 -> 1, 2 -> 2, 3 -> 3)
scala> m += 4 -> 4
res45: m.type = Map(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4)
scala> m += 5 -> 5
res46: m.type = Map(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4, 5 -> 5)
scala> m -= 99
res47: m.type = Map(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4, 5 -> 5)
scala> m -= 3
res48: m.type = Map(1 -> 1, 2 -> 2, 4 -> 4, 5 -> 5) |
@Ichoran said: |
@ruippeixotog said: |
Apoorv Agarwal (apoorv024) said: I was thinking of adding test cases for ordering here. Are those tests still needed? |
I’m happy to deprecate |
Scala 2.9:
but Scala 2.10.6 scrambles the order:
The intent that either ListMap should preserve insertion order wasn't explicit in the doc until scala/scala#4788 and scala/scala#4994, and there's some confusion at http://stackoverflow.com/questions/7539924/why-do-mutable-and-immutable-listmaps-have-different-orders-in-scala and https://twitter.com/travisbrown/status/735201036873400321 about what the behavior should properly be.
But I think not preserving the order is just absurd. There's no downside to preserving. Also, note that http://docs.scala-lang.org/overviews/collections/concrete-immutable-collection-classes.html#list_maps implies that the only reason to use a ListMap at all is if you care about the order.
The text was updated successfully, but these errors were encountered: