I want to create a map from a collection by providing it a mapping function. It's basically equivalent to what a normal map
method does, only I want it to return a Map
, not a flat collection.
I would expect it to have a signature like
def toMap[T, S](T => S): Map[T, S]
when invoked like this
val collection = List(1, 2, 3)
val map: Map[Int, String] = collection.toMap(_.toString + " seconds")
the expected value of map
would be
Map(1 -> "1 seconds", 2 -> "2 seconds", 3 -> "3 seconds")
The method would be equivalent to
val collection = List(1, 2, 3)
val map: Map[Int, String] = collection.map(x => (x, x.toString + " seconds")).toMap
is there such a method in Scala?