Overview

Maps are collections of key-value pais. Just like arrays, in Scala we can distinguish between mutable and immutable maps. Furthermore, the default map type is a hash map. However, tree maps are also provided [1].

As mentioned above, in Scala, a map is a collection of key-value pairs. A pair is a grouping of two values that do not have necessarily the same type [1]. We have two ways cosntructing a pair

  • Using the -> operator.
  • Using (key, value) constructs

Immutable map

We can construct an immutable Map as shown below

val map1 = Map("France" -> "Paris", "England" -> "London", "Greece" -> "Athens")
map1: Map[String, String] = Map(
  "France" -> "Paris",
  "England" -> "London",
  "Greece" -> "Athens"
)

The elements in Map cannot be changed

map1("Greece") = "New York"
cmd1.sc:1: value update is not a member of scala.collection.immutable.Map[String,String]
did you mean updated?
val res1 = map1("Greece") = "New York"
           ^Compilation Failed
Compilation Failed

Nevertheless, the following is a way to update an immmutable map

val map2 = map1 + ("Greece" -> "New York")
map2: Map[String, String] = Map(
  "France" -> "Paris",
  "England" -> "London",
  "Greece" -> "New York"
)

Similarly, we can remove or add a new element

val map3 = map2 + ("Italy" -> "Rome")
map3: Map[String, String] = Map(
  "France" -> "Paris",
  "England" -> "London",
  "Greece" -> "New York",
  "Italy" -> "Rome"
)
val map4 = map3 - "Greece"
map4: Map[String, String] = Map(
  "France" -> "Paris",
  "England" -> "London",
  "Italy" -> "Rome"
)

Mutable map

In order to get a mutable map we need to explicitly say so

import scala.collection.mutable.Map
import scala.collection.mutable.Map
val grades = Map("Alex" -> 10, "Alice" -> 15, "George" ->5)
grades: Map[String, Int] = HashMap("Alex" -> 10, "George" -> 5, "Alice" -> 15)
grades("Alex") = 12
grades
res7: Map[String, Int] = HashMap("Alex" -> 12, "George" -> 5, "Alice" -> 15)

Above we have initialized the map at construction time. However, we might not always be able to do so. In this case, we need to specify explicitly what type of map we want [1]

import scala.collection.mutable.HashMap
import scala.collection.mutable.HashMap
val gradesEmpty = new HashMap[String, Int]
gradesEmpty: HashMap[String, Int] = HashMap()

If the map is mutable, this means tha we can add, change or remove elements. We can add a new element in two ways

  • Use operator (key). If the key exists it will update the value corresponding to the key. Otherwise, it will create a new key-value pair
  • Use operator += followed by a tuple of pairs
gradesEmpty += ("Suzana" -> 15, "John" -> 3)
res10: HashMap[String, Int] = HashMap("Suzana" -> 15, "John" -> 3)

We can remove a key-value pait using the -= operator

gradesEmpty -= "Suzana"
res11: HashMap[String, Int] = HashMap("John" -> 3)

Querying a map

How can we find whether a key is contained in a map?

grades.contains("Alex")
res12: Boolean = true
grades.contains("")
res13: Boolean = false

One nice feature is that we can query a map with a key and specify a default value in case that the key does not exist

grades.getOrElse("Alex", "Invalid Name")
res14: Any = 12
grades.getOrElse("SomeOne", "Invalid Name")
res15: Any = "Invalid Name"

As shown above, we can access the value of a particular key using the () operator. This, however, will an exception if the key does not exit. Finally, we can use grades.get( someKey ). This returns an Option object that is either Some( value for key ) or None [1].

References

  1. Cay Horstmann, Scala for the Impatient 1st Edition