【发布时间】:2021-11-11 17:22:36
【问题描述】:
我可以用 += 更新 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)
但我不能将+= 与getOrElseUpdate 一起使用,我不明白为什么:
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 和 mutable.ArrayBuffer 值,它让我可以毫无问题地使用该类型的 += 操作和 getOrElseUpdate。我应该使用像mutable.Int 这样的东西吗?
【问题讨论】:
-
你到底想做什么?
标签: scala dictionary integer assign mutablemap