Scala: How to add, update, and remove elements with a mutable Map

This is an excerpt from the 1st Edition of the Scala Cookbook (partially modified for the internet). This is Recipe 11.15, “How to Add, Update, and Remove Elements with a Mutable Scala Map”

Problem

You want to add, remove, or update elements in a mutable map in Scala.

Solution

Add elements to a mutable map by simply assigning them, or with the += method. Remove elements with -= or --=. Update elements by reassigning them.

Given a new, mutable Scala Map:

scala> var states = scala.collection.mutable.Map[String, String]()
states: scala.collection.mutable.Map[String,String] = Map()

You can add an element to a map by assigning a key to a value:

scala> states("AK") = "Alaska"
You can also add elements with the += method:

scala> states += ("AL" -> "Alabama")
res0: scala.collection.mutable.Map[String,String] = Map(AL -> Alabama, AK -> Alaska)

Add multiple elements at one time with +=:

scala> states += ("AR" -> "Arkansas", "AZ" -> "Arizona")
res1: scala.collection.mutable.Map[String,String] = Map(AL -> Alabama, AR -> Arkansas, AK -> Alaska, AZ -> Arizona)

Add multiple elements from another collection using ++=:

scala> states ++= List("CA" -> "California", "CO" -> "Colorado")
res2: scala.collection.mutable.Map[String,String] = Map(CO -> Colorado,
  AZ -> Arizona, AL -> Alabama, CA -> California, AR -> Arkansas,
  AK -> Alaska)

Remove a single element from a map by specifying its key with the -= method:

scala> states -= "AR"
res3: scala.collection.mutable.Map[String,String] = Map(AL -> Alabama, AK -> Alaska, AZ -> Arizona)

Remove multiple elements by key with the -= or --= methods:

scala> states -= ("AL", "AZ")
res4: scala.collection.mutable.Map[String,String] = Map(AK -> Alaska)

// remove multiple with a List of keys
scala> states --= List("AL", "AZ")
res5: scala.collection.mutable.Map[String,String] = Map(AK -> Alaska)

Update elements by reassigning their key to a new value:

scala> states("AK") = "Alaska, A Really Big State"

scala> states
res6: scala.collection.mutable.Map[String,String] = Map(AK -> Alaska, A Really Big State)

There are other ways to add elements to maps, but these examples show the most common uses.

Discussion

The methods shown in the Solution demonstrate the most common approaches. You can also use:

  • put to add an element (or replace an existing element)
  • retain to keep only the elements in the map that match the predicate you supply
  • remove to remove an element by its key value
  • clear to delete all elements in the map.

These methods are shown in the following examples:

scala> val states = collection.mutable.Map(
     |   "AK" -> "Alaska",
     |   "IL" -> "Illinois",
     |   "KY" -> "Kentucky"
     | )
states: collection.mutable.Map[String,String] = Map(KY -> Kentucky, IL -> Illinois, AK -> Alaska)

scala> states.put("CO", "Colorado")
res0: Option[String] = None

scala> states.retain((k,v) => k == "AK")
res1: states.type = Map(AK -> Alaska)

scala> states.remove("AK")
res2: Option[String] = Some(Alaska)

scala> states
res3: scala.collection.mutable.Map[String,String] = Map()

scala> states.clear

scala> states
res4: scala.collection.mutable.Map[String,String] = Map()

As shown, the remove method returns an Option that contains the value that was removed. It’s not shown in the example, but if the element put into the collection by put replaced another element, that value would be returned. Because this example didn’t replace anything, it returned None.