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

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

Here are some unsuccessful attempts:

val map = scala.collection.mutable.Map map("mykey") = "myval" map += "mykey" -> "myval" map.put("mykey","myval") 
+63
put scala mutable map add
Oct 22 2018-10-10T00:
source share
6 answers

The fact is that the first line of your codes is not what you expected. You have to use

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

or

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

instead.

 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)) 
+70
Oct 22 '10 at 3:27
source share

As always, you should ask if you really need a changeable map.

Immutable cards are trivial to build:

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

Volatile cards are no different at first build:

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

In both cases, the output will be used to determine the correct type parameters for the map instance.

You can also hold an immutable map in var , then the variable will 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 do not have 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 
+40
Oct 22 '10 at 9:34
source share

When you speak

 val map = scala.collection.mutable.Map 

you are not creating a map instance, but instead imposing a map type alias.

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

Try the following instead:

 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)) 
+12
Oct 22 '10 at 3:11
source share
 var test = scala.collection.mutable.Map.empty[String, String] test("myKey") = "myValue" 
+2
Oct 22 '10 at 3:26
source share

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 specified map (and create a new map, as they are both immutable):

 scala> val m2 = m1 + ("k1" -> "v1") m2: scala.collection.immutable.Map[String,String] = Map(k0 -> v0, k1 -> v1) 
+2
Jun 12 '14 at 23:42 on
source share

Create a modified map with no initial value:

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

Create a modified 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 a new key value:

 scala> d(100)= "new element" 

Check updated map:

 scala> d res123: scala.collection.mutable.Map[Any,Any] = Map(2 -> test, 100 -> new element, a -> ABC, 1 -> 234) 
0
Jan 02 '17 at 9:28 on
source share



All Articles