Remove null items from a list in Groovy

后端 未结 8 1421
清酒与你
清酒与你 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 21:01

    Just use minus:

    [null, 30, null] - null
    
    0 讨论(0)
  • 2021-02-06 21:05

    This does an in place removal of all null items.

    myList.removeAll { !it }
    

    If the number 0 is in your domain you can check against null

    myList.removeAll { it == null }
    
    0 讨论(0)
提交回复
热议问题