How to create Maps in Scala (Scala Map class examples)

This is an excerpt from the 1st Edition of the Scala Cookbook (partially modified for the internet). This is Recipe 11.13, “How to Create Maps in Scala.”

Problem

You want to create and use a mutable or immutable Map in a Scala application.

Solution: Creating Scala Maps

To use an immutable map, you don’t need an import statement, just create a Map:

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

This expression creates an immutable Map with type Map[String, String]. For the first element, the string AL is the key, and Alabama is the value.

As noted, you don’t need an import statement to use a basic, immutable Map. The Scala Predef object brings the immutable Map trait into scope by defining a type alias:

type Map[A, +B] = immutable.Map[A, B]
val Map         = immutable.Map

To create a mutable map, either use an import statement to bring it into scope, or specify the full path to the scala.collection.mutable.Map class when you create an instance.

You can define a mutable Map that has initial elements:

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

You can also create an empty, mutable Map initially, and add elements to it later:

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

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

Discussion: Scala Maps

Like maps in other programming languages, maps in Scala are a collection of key/value pairs. If you’ve used maps in Java, dictionaries in Python, or a hash in Ruby, Scala maps are straightforward. You only need to know a couple of new things, including the methods available on map classes, and the specialty maps that can be useful in certain situations, such as having a sorted map.

Note that the syntax that’s used inside parentheses in a map creates a Tuple2:

"AL" -> "Alabama"

Because you can also declare a Tuple2 as ("AL", "Alabama"), you may also see maps created like this:

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

Use whichever style you prefer.

When I want to be clear that I’m using a mutable map, I normally specify the full path to the mutable Map class when I create the instance, as shown in the Solution. Another technique you can use it to give the mutable Map an alias when you import it, and then refer to it using that alias, as shown here:

import scala.collection.mutable.{Map => MMap}

object Test extends App {
    // MMap is really scala.collection.mutable.Map
    val m = MMap(1 -> 'a')
    for((k,v) <- m) println(s"$k, $v")
}

This technique is described more in Recipe 7.3, “Renaming Members on Import”.

See Also

  • The Map trait
  • The Predef object