Scala: How to create a Map[K,V] from a Set[K] and a function from K to V?

前端 未结 7 1215
栀梦
栀梦 2020-12-15 07:37

What is the best way to create a Map[K,V] from a Set[K] and function from K to V?

For example, suppose I have

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 08:13

    Without definition of func(i: Int) using "string repeating" operator *:

    scala> s map { x => x -> x.toString*2 } toMap
    res2: scala.collection.immutable.Map[Int,String] = Map(2 -> 22, 3 -> 33, 5 -> 55)
    

提交回复
热议问题