Why is there no mutable TreeMap in Scala?

后端 未结 5 1003
-上瘾入骨i
-上瘾入骨i 2020-12-29 19:17

Is it lack of time, some technical problem or is there a reason why it should not exist?

5条回答
  •  旧巷少年郎
    2020-12-29 19:34

    There may be performance reasons for a mutable TreeMap, but usually you can use an immutable map in the same way as you would a mutable one. You just have to assign it to a var rather than a val. It would be the same as for HashMap, which has mutable and immutable variants:

    val mh = collection.mutable.HashMap[Int, Int]()
    var ih = collection.immutable.HashMap[Int, Int]()
    mh += (1 -> 2)
    ih += (1 -> 2)
    mh // scala.collection.mutable.HashMap[Int,Int] = Map(1 -> 2)
    ih // scala.collection.immutable.HashMap[Int,Int] = Map(1 -> 2)
    

提交回复
热议问题