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

后端 未结 3 906
温柔的废话
温柔的废话 2020-12-15 17:46

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

scala> val scores = scala.coll         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 18:21

    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))
    }
    }
    

提交回复
热议问题