Scala可变ListMap似乎有错误的顺序

时间:2018-04-30 01:19:19

标签: scala

我对以下现象感到困惑:(在scala版本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)"

muListMap的顺序错了!为什么呢?

1 个答案:

答案 0 :(得分:3)

这是一个错误,所以如果可能的话,只使用不可变的ListMap。见https://github.com/scala/bug/issues/9893