July 2017
Intermediate to advanced
796 pages
18h 55m
English
A map is an Iterable consisting of pairs of keys and values (also named mappings or associations). A map is also one of the most widely used connections as it can be used to hold basic datatypes. For example:
scala> Map(1 -> 2)res7: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> Map("X" -> "Y")res8: scala.collection.immutable.Map[String,String] = Map(X -> Y)
Scala's Predef object offers an implicit conversion that lets you write key -> value as an alternate syntax for the pair (key, value). For instance, Map("a" -> 10, "b" -> 15, "c" -> 16) means exactly the same as Map(("a", 10), ("b", 15), ("c", 16)), but reads better.
Moreover, a Map can be simply considered a collection of Tuple2s:
Map(2 -> "two", 4 -> "four")
The preceding ...
Read now
Unlock full access