Scala 的可变 Map 更新 [map(key) = newValue] 语法如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15604697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:05:32  来源:igfitidea点击:

How does Scala's mutable Map update [map(key) = newValue] syntax work?

scalascala-collections

提问by Dan Midwood

I'm working through Cay Horstmann's Scala for the Impatient book where I came across this way of updating a mutable map.

我正在通过 Cay Horstmann 的 Scala 为 Impatient 书工作,在那里我遇到了这种更新可变地图的方式。

scala> val scores = scala.collection.mutable.Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8)
scores: scala.collection.mutable.Map[String,Int] = Map(Bob -> 3, Alice -> 10, Cindy -> 8)

scala> scores("Alice") // retrieve the value of type Int
res2: Int = 10

scala> scores("Alice") = 5 // Update the Alice value to 5

scala> scores("Alice")
res4: Int = 5

It looks like scores("Alice")hits applyin MapLike.scala. But this only returns the value, not something that can be updated.

它看起来像scores("Alice")命中applyMapLike.scala。但这只会返回值,而不是可以更新的值。

Out of curiosity I tried the same syntax on an immutable map and was presented with the following error,

出于好奇,我在不可变映射上尝试了相同的语法,但出现以下错误,

scala> val immutableScores = Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8)
immutableScores: scala.collection.immutable.Map[String,Int] = Map(Alice -> 10, Bob -> 3, Cindy -> 8)

scala> immutableScores("Alice") = 5
<console>:9: error: value update is not a member of scala.collection.immutable.Map[String,Int]
              immutableScores("Alice") = 5
          ^

Based on that, I'm assuming that scores("Alice") = 5is transformed into scores update ("Alice", 5)but I have no idea how it works, or how it is even possible.

基于此,我假设它scores("Alice") = 5被转换为scores update ("Alice", 5)但我不知道它是如何工作的,或者它是如何可能的。

How does it work?

它是如何工作的?

回答by Boris the Spider

This is an example of the apply, updatesyntax.

这是apply,update语法的示例。

When you call map("Something")this calls map.apply("Something")which in turn calls get.

当您调用map("Something")this 时map.apply("Something"),它又会调用get.

When you call map("Something") = "SomethingElse"this calls map.update("Something", "SomethingElse")which in turn calls put.

当您调用map("Something") = "SomethingElse"this 时map.update("Something", "SomethingElse"),它又会调用put.

Take a look at thisfor a fuller explanation.

看看this以获得更完整的解释。

回答by AlexPes

Can you try this: => to update list of Map

你可以试试这个:=> 更新地图列表

import java.util.concurrent.ConcurrentHashMap
import scala.collection.JavaConverters._
import scala.collection.concurrent

val map: concurrent.Map[String, List[String]] = new ConcurrentHashMap[String, List[String]].asScala

def updateMap(key: String, map: concurrent.Map[String, List[String]], value: String): Unit = {
map.get(key) match {
case Some(list: List[String]) => {
val new_list = value :: list
map.put(key, new_list)
}
case None => map += (key -> List(value))
}
}

回答by maxim4d

The problem is you're trying to update immutable map. I had the same error message when my map was declared as

问题是您正在尝试更新不可变地图。当我的地图被声明为时,我有同样的错误消息

var m = new java.util.HashMap[String, Int]

But when i replaced the definition by

但是当我用

var m = new scala.collection.mutable.HashMap[String, Int]

the m.updateworked.

m.update工作。