Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove null elements from list

Tags:

java

List<String> list = new ArrayList<String>();   list.add("One");   list.add(null);   list.add("Two!");   list.add(null);   list.add("Three");   list.add(null);   list.add("Four");   list.add(null); 

I have a list containing null elements. Is there any way to remove the null elements from the current collection without using any iterations?

like image 738
Noufal Panolan Avatar asked Dec 19 '11 09:12

Noufal Panolan


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.

How do I remove null elements from a list in R?

If a list contains NULL then we might want to replace it with another value or remove it from the list if we do not have any replacement for it. To remove the NULL value from a list, we can use the negation of sapply with is. NULL.

How do you remove null from a list in Python?

The easiest way to remove none from list in Python is by using the list filter() method. The list filter() method takes two parameters as function and iterator. To remove none values from the list we provide none as the function to filter() method and the list which contains none values.

How do I get rid of null?

replace("null", ""); You can also replace all the instances of 'null' with empty String using replaceAll.


1 Answers

This should work:

list.removeAll(Collections.singleton(null));   
like image 62
dveth Avatar answered Sep 24 '22 07:09

dveth