Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an empty list from a list (Java)

I have searched for this but it's in other languages like Python or R (?). I have lists inside a list and I would like to remove the empty list. For example:

[ [abc,def], [ghi], [], [], [jkl, mno]]

I would like:

[ [abc,def], [ghi], [jkl, mno]]

How do I remove empty list from a list? Thanks!

like image 479
sw2 Avatar asked Jul 03 '15 00:07

sw2


People also ask

How do I remove a blank list from a list?

Short answer: You can remove all empty lists from a list of lists by using the list comprehension statement [x for x in list if x] to filter the list.


2 Answers

You could try this as well:

list.removeIf(p -> p.isEmpty());
like image 72
Micho Avatar answered Sep 22 '22 09:09

Micho


You could use:

list.removeAll(Collections.singleton(new ArrayList<>()));
like image 45
Alex Sifuentes Avatar answered Sep 18 '22 09:09

Alex Sifuentes