How to create all possible combinations from the elements of a list?

前端 未结 4 1483
醉话见心
醉话见心 2021-02-01 02:07

I have the following list:

List(a, b, c, d, e)

How to create all possible combinations from the above list?

I expect something like:

4条回答
  •  没有蜡笔的小新
    2021-02-01 02:21

    Or you could use the subsets method. You'll have to convert your list to a set first though.

    scala> List(1,2,3).toSet[Int].subsets.map(_.toList).toList
    res9: List[List[Int]] = List(List(), List(1), List(2), List(3), List(1, 2), List(1, 3), List(2, 3), List(1, 2, 3))
    

提交回复
热议问题