scala Remove (in place) all elements of a ListBuffer that meet a condition

前端 未结 2 1048
粉色の甜心
粉色の甜心 2021-02-13 04:36

I have a ListBuffer. I want to remove all elements that meet a certain condition.

I could iterate over it and remove each element. But what doe Scala say about mutating

2条回答
  •  渐次进展
    2021-02-13 04:51

    You could combine the two and do the following:

    val lb = ListBuffer(1,2,3,4,5,6)
    lb --= lb.filter(_ % 2 == 0)
    
    println(lb)
    // outputs: ListBuffer(1, 3, 5)
    

提交回复
热议问题