Scala maps -> operator

后端 未结 4 1124
离开以前
离开以前 2020-12-23 16:54

What does the symbol -> mean in the context of a Map in Scala?

Scala’s Predef class offers an implicit conversion that lets

4条回答
  •  轮回少年
    2020-12-23 17:19

    -> 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.

提交回复
热议问题