How to convert a Seq[A] to a Map[Int, A] using a value of A as the key in the map?

前端 未结 4 1328
野趣味
野趣味 2021-01-31 07:59

I have a Seq containing objects of a class that looks like this:

class A (val key: Int, ...)

Now I want to convert this Seq<

4条回答
  •  爱一瞬间的悲伤
    2021-01-31 08:49

    Map over your Seq and produce a sequence of tuples. Then use those tuples to create a Map. Works in all versions of Scala.

    val map = Map(seq map { a => a.key -> a }: _*)
    

提交回复
热议问题