Remove null items from a list in Groovy

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

    here is an answer if you dont want to keep the original list

    void testRemove() {
        def list = [null, 30, null]
    
        list.removeAll([null])
    
        assertEquals 1, list.size()
        assertEquals 30, list.get(0)
    }
    

    in a handy dandy unit test

提交回复
热议问题