Idiomatic way to update value in a Map based on previous value

后端 未结 4 760
日久生厌
日久生厌 2020-12-08 00:35

Let\'s say I store bank accounts information in an immutable Map:

val m = Map(\"Mark\" -> 100, \"Jonathan\" -> 350, \"Bob\" -> 65)
         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 00:46

    An SO Answer proposes another alternative, using the |+| operator from scalaz

    val m2 = m |+| Map("Mark" -> -50)
    

    The |+| operator will sum the values of an existing key, or insert the value under a new key.

提交回复
热议问题