Remove null items from a list in Groovy

后端 未结 8 1443
清酒与你
清酒与你 2021-02-06 20:34

What is the best way to remove null items from a list in Groovy?

ex: [null, 30, null]

want to return: [30]

8条回答
  •  青春惊慌失措
    2021-02-06 20:57

    This can also be achieved by grep:

    assert [null, 30, null].grep()​ == [30]​
    

    or

    assert [null, 30, null].grep {it}​ == [30]​
    

    or

    assert [null, 30, null].grep { it != null } == [30]​
    

提交回复
热议问题