GroupBy in scala

后端 未结 5 1123
醉酒成梦
醉酒成梦 2021-01-02 23:52

I have

val a = List((1,2), (1,3), (3,4), (3,5), (4,5))

I am using A.groupBy(_._1) which is groupBy with the first element. Bu

5条回答
  •  無奈伤痛
    2021-01-03 00:30

    As from Scala 2.13 it would be possible to use groupMap so you'd be able to write just:

    // val list = List((1, 2), (1, 3), (3, 4), (3, 5), (4, 5))
    list.groupMap(_._1)(_._2)
    // Map(1 -> List(2, 3), 3 -> List(4, 5), 4 -> List(5))
    

提交回复
热议问题