89

What is the syntax for adding an element to a scala.collection.mutable.Map ?

Here are some failed attempts:

val map = scala.collection.mutable.Map

map("mykey") = "myval"

map += "mykey" -> "myval"

map.put("mykey","myval")
Ricardo
  • 3,696
  • 5
  • 36
  • 50
Koala3
  • 2,203
  • 4
  • 20
  • 15

7 Answers7

103

The point is that the first line of your code is not what you expected.

You should use:

val map = scala.collection.mutable.Map[A,B]()

You then have multiple equivalent alternatives to add items:

scala> val map = scala.collection.mutable.Map[String,String]()
map: scala.collection.mutable.Map[String,String] = Map()


scala> map("k1") = "v1"

scala> map
res1: scala.collection.mutable.Map[String,String] = Map((k1,v1))


scala> map += "k2" -> "v2"
res2: map.type = Map((k1,v1), (k2,v2))


scala> map.put("k3", "v3")
res3: Option[String] = None

scala> map
res4: scala.collection.mutable.Map[String,String] = Map((k3,v3), (k1,v1), (k2,v2))

And starting Scala 2.13:

scala> map.addOne("k4" -> "v4")
res5: map.type = HashMap(k1 -> v1, k2 -> v2, k3 -> v3, k4 -> v4)
starball
  • 20,030
  • 7
  • 43
  • 238
Eastsun
  • 18,526
  • 6
  • 57
  • 81
47

As always, you should question whether you truly need a mutable map.

Immutable maps are trivial to build:

val map = Map(
  "mykey" -> "myval",
  "myotherkey" -> "otherval"
)

Mutable maps are no different when first being built:

val map = collection.mutable.Map(
  "mykey" -> "myval",
  "myotherkey" -> "otherval"
)

map += "nextkey" -> "nextval"

In both of these cases, inference will be used to determine the correct type parameters for the Map instance.

You can also hold an immutable map in a var, the variable will then be updated with a new immutable map instance every time you perform an "update"

var map = Map(
  "mykey" -> "myval",
  "myotherkey" -> "otherval"
)

map += "nextkey" -> "nextval"

If you don't have any initial values, you can use Map.empty:

val map : Map[String, String] = Map.empty //immutable
val map = Map.empty[String,String] //immutable
val map = collection.mutable.Map.empty[String,String] //mutable
Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
  • 2
    Let's say that I have a class with a Map field and that it can be updated at any time; is it really preferred in Scala to have a 'var' + immutable map rather than 'val' + mutable map? As an experienced Java programmer I'd go for the 2nd option, but from your answer I assume that the 1st one is more common in Scala world? – omnomnom May 30 '13 at 20:50
  • 2
    And if you want to add multiple entries at once: `map ++ Seq("another1" -> "anotherVal1", "another2" -> "anotherVal2")` – Brent Faust Jul 15 '15 at 01:38
18

When you say

val map = scala.collection.mutable.Map

you are not creating a map instance, but instead aliasing the Map type.

map: collection.mutable.Map.type = scala.collection.mutable.Map$@fae93e

Try instead the following:

scala> val map = scala.collection.mutable.Map[String, Int]()
map: scala.collection.mutable.Map[String,Int] = Map()

scala> map("asdf") = 9

scala> map
res6: scala.collection.mutable.Map[String,Int] = Map((asdf,9))
Synesso
  • 37,610
  • 35
  • 136
  • 207
3

Create a new immutable map:

scala> val m1 = Map("k0" -> "v0")   
m1: scala.collection.immutable.Map[String,String] = Map(k0 -> v0)

Add a new key/value pair to the above map (and create a new map, since they're both immutable):

scala> val m2 = m1 + ("k1" -> "v1")             
m2: scala.collection.immutable.Map[String,String] = Map(k0 -> v0, k1 -> v1)
0nIce
  • 31
  • 2
2
var test = scala.collection.mutable.Map.empty[String, String]
test("myKey") = "myValue"
kkress
  • 777
  • 5
  • 6
2

Create a mutable map without initial value:

scala> var d= collection.mutable.Map[Any, Any]()
d: scala.collection.mutable.Map[Any,Any] = Map()

Create a mutable map with initial values:

scala> var d= collection.mutable.Map[Any, Any]("a"->3,1->234,2->"test")
d: scala.collection.mutable.Map[Any,Any] = Map(2 -> test, a -> 3, 1 -> 234)

Update existing key-value:

scala> d("a")= "ABC"

Add new key-value:

scala> d(100)= "new element"

Check the updated map:

scala> d
res123: scala.collection.mutable.Map[Any,Any] = Map(2 -> test, 100 -> new element, a -> ABC, 1 -> 234)
Andy Dong
  • 405
  • 5
  • 10
0

var map:Map[String, String] = Map()

var map1 = map + ("red" -> "#FF0000")

println(map1)

  • 2
    That poor little piece of scared and lonely code is begging for company and protection by an explanation. – Yunnosch Nov 03 '20 at 06:18
  • 3
    Welcome to SO. With questions this old (it was asked over 10 years ago), and with so many answers already submitted, it is helpful to point out how your answer adds something new to the topic. As it is, your post displays very poor Scala style. – jwvh Nov 03 '20 at 06:53