How to split a sequence into two pieces by predicate?

后端 未结 6 948
孤城傲影
孤城傲影 2020-12-02 11:35

How do I split a sequence into two lists by a predicate?

Alternative: I can use filter and filterNot, or write my own method, but isn\'t th

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 12:13

    By using partition method:

    scala> List(1,2,3,4).partition(x => x % 2 == 0)
    res0: (List[Int], List[Int]) = (List(2, 4),List(1, 3))
    

提交回复
热议问题