Small steps to Scala (4/20) - maps and tuples

Scala
4/20

Maps and tuples

  • constructing a map
  • accessing map values
  • updating map values
  • iterating over maps
  • sorted maps
  • tuples

Constructing a map

Maps, or hash tables are basically immutable collections which you can construct as follows. The two statements below are equivalent.

val measures1 = Map("height" -> 10, "length" -> 20, "width" -> 5)
val measures2 = Map(("height", 10), ("length", 20), ("width", 5))

The examples above are immutable constructions of a map. However, it is possible to create a mutable map also, as shown below:

val new_measures = scala.collection.mutable.Map(("height", 10), ("length", 20), ("width", 5))

Accessing map values

Since the map is a key value pair construct, in order to access the value of a map, you need to call it using the key (or index).

> measures1("height")
res0: Int = 10

If the key does not exist, Scala will return key not found exception. However, this can be avoided with the following construct.

measures1.getOrElse("test", 0)

This translates into, if the key test exists, return its value, else return 0.

Updating map values

You cannot update an immutable map. When trying to update an immutable map, an error will be thrown value += is not a member of scala.collection.immutable.Map.

It is however possible to create a new immutable map, by using the existing immutable map(s).

val some_measures = measures1 + (("test", 23))

The sample below is showing how to update a mutable map.

new_measures += (("high", 50), ("low", -5))

giving a return of new_measures.type = Map(high -> 50, length -> 20, height -> 10, width -> 5, low -> -5)

To remove an association, the construct is:

new_measures -= ("low", "length")

Iterating over maps

There are multiple ways to iterate over maps:

  • Good old fashion for loop.
scala> for ((k, v) <- measures1) println(k + " = " + v)
height = 10
length = 20
width = 5
  • Using foreach
measures1.foreach(x => println(x._1 + " = " + x._2))

This example uses a tuple construct (e.g. x._2) which we will see a bit later down the page. The result is the same as above

  • Using case
measures1.foreach {
     case(k,v) => println(k + " = " + v)
     case _ => "" // this is not necessary, just habit
}

Sorted maps

scala> val sorting = scala.collection.immutable.SortedMap(("height", 10), ("length", 20), ("width",5), ("hello", 4))
sorting: scala.collection.immutable.SortedMap[String,Int] = Map(height -> 10, hello -> 4, length -> 20, width -> 5)

Tuples

Tuples are aggregations of different types, enclosed in round parenthesis ()

val tpl = (3, "3", 3.0, "Three")

This particular example is of type Tuple4[Int, String, Double, String] and the way you can access its elements is by using the following notation:

scala> tpl._3
res38: Double = 3.0

Each element of the tuple can be accessed by calling its indexed position. In the tuple case, the index starts at 1 not at 0 as the array.


[+] Scala series
  • Small steps to Scala 1/20 - the basics
  • Small steps to Scala 2/20 - functions
  • Small steps to Scala 3/20 - arrays

  • Disclaimer: This is by no means an original work it is merely meant to serve as a compilation of thoughts, code snippets and teachings from different sources.