Convert List of tuple to map (and deal with duplicate key ?)

前端 未结 8 2154
[愿得一人]
[愿得一人] 2020-12-12 14:32

I was thinking about a nice way to convert a List of tuple with duplicate key [(\"a\",\"b\"),(\"c\",\"d\"),(\"a\",\"f\")] into map (\"a\" -> [\"b\", \"

8条回答
  •  爱一瞬间的悲伤
    2020-12-12 15:11

    You can try this

    scala> val b = new Array[Int](3)
    // b: Array[Int] = Array(0, 0, 0)
    scala> val c = b.map(x => (x -> x * 2))
    // c: Array[(Int, Int)] = Array((1,2), (2,4), (3,6))
    scala> val d = Map(c : _*)
    // d: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 2 -> 4, 3 -> 6)
    

提交回复
热议问题