蘇哲聖
蘇哲聖

Reputation: 795

Scala mutable ListMap seems to have wrong order

I am confused by the following phenomena: (in scala version 2.12.4)

val muListMap = collection.mutable.ListMap.empty[String, Int]
muListMap += "b" -> 1
muListMap += "a" -> 2
muListMap += "c" -> 3
muListMap += "d" -> 4
println(muListMap) //"Map(d -> 4, a -> 2, b -> 1, c -> 3)"

var immuListMap = collection.immutable.ListMap.empty[String, Int]
immuListMap += "b" -> 1
immuListMap += "a" -> 2
immuListMap += "c" -> 3
immuListMap += "d" -> 4
println(immuListMap) //"ListMap(b -> 1, a -> 2, c -> 3, d -> 4)"

The order of muListMap is wrong! Why?

Upvotes: 2

Views: 171

Answers (1)

Alan Effrig
Alan Effrig

Reputation: 773

It's a bug, so just use the immutable ListMap if possible. See https://github.com/scala/bug/issues/9893

Upvotes: 3

Related Questions