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

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

Problem

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

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 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; and 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.

See Also

  • The Scala mutable Map trait

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote for O’Reilly:

You can find the Scala Cookbook at these locations:

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.