Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove null items from a list in Groovy

Tags:

groovy

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

ex: [null, 30, null]

want to return: [30]

like image 778
RyanLynch Avatar asked Jul 19 '10 21:07

RyanLynch


People also ask

Can we remove null from List?

Using List. To remove all null occurrences from the list, we can continuously call remove(null) until all null values are removed. Please note that the list will remain unchanged if it does not contain any null value.

Is null or empty groovy?

In Groovy, there is a subtle difference between a variable whose value is null and a variable whose value is the empty string. The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.


1 Answers

Just use minus:

[null, 30, null] - null 
like image 122
Sergei Avatar answered Sep 16 '22 15:09

Sergei