Remove null items from a list in Groovy

后端 未结 8 1422
清酒与你
清酒与你 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

    I think you'll find that this is the shortest, assuming that you don't mind other "false" values also dissappearing:

    println([null, 30, null].findAll())
    

    public Collection findAll() Finds the items matching the IDENTITY Closure (i.e. matching Groovy truth). Example:

    def items = [1, 2, 0, false, true, '', 'foo', [], [4, 5], null] assert items.findAll() == [1, 2, true, 'foo', [4, 5]]

提交回复
热议问题