What does the symbol ->
mean in the context of a Map
in Scala?
Scala’s Predef
class offers an implicit conversion that lets
->
is used to couple keys and values for a map. So:
val m = Map(1 -> "one", 2 -> "two", 3 -> "three")
will map the first 3 positive integers into text equivalents (that is, m(1)
will be "one"
, etc.). You could also write it as
val m = Map((1,"one"), (2,"two"), (3,"three"))
but the first way looks nicer, which is why it's provided as an alternative.