我可以使用以下命令更新 mutable.Map 值+=
:
scala> val map = mutable.Map[Int,Int]()
map: scala.collection.mutable.Map[Int,Int] = Map()
scala> map(5) = 5
scala> map
res55: scala.collection.mutable.Map[Int,Int] = Map(5 -> 5)
scala> map(5) += 1
scala> map
res57: scala.collection.mutable.Map[Int,Int] = Map(5 -> 6)
但我不能使用+=
withgetOrElseUpdate
也不明白为什么:
scala> map.getOrElseUpdate(5, 0) += 1
<console>:16: error: value += is not a member of Int
Expression does not convert to assignment because receiver is not assignable.
map.getOrElseUpdate(5, 0) += 1
^
我知道我以前使用过mutable.SortedMap
with值,它让我可以毫无问题mutable.ArrayBuffer
地使用该类型的+=
操作。我应该使用getOrElseUpdate
类似的东西吗?mutable.Int