Java 8 - filter empty string from List not working
Since java 11 there is a new method Predicate::not.
So you can write it like that:
list.stream().filter(Predicate.not(String::isEmpty)).collect(Collectors.toList())
I suspect you are not keeping the result. The result is returned, the original list is not altered as this is functional programming style.
list = list.stream().filter(item-> !item.trim().isEmpty()).collect(Collectors.toList());
filter()
keeps the elements that match the predicate. Soyou need the inverse predicate:
list.stream().filter(item-> !item.isEmpty()).collect(Collectors.toList());
This will also not modify the original list. It will create a filtered copy of the original list. So you need
list = list.stream().filter(item-> !item.isEmpty()).collect(Collectors.toList());
If you want to modify the original list, you should use
list.removeIf(item -> item.isEmpty());
or simply
list.removeIf(String::isEmpty);