Java 8 - 从列表中过滤空字符串不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40605998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java 8 - filter empty string from List not working
提问by Alexander Nikolov
I would like to remove an empty String from the List of Strings.
我想从字符串列表中删除一个空字符串。
Here is what I tried, using the stream API:
这是我尝试过的,使用流 API:
list.stream().filter(item-> item.isEmpty()).collect(Collectors.toList());
After that empty string is still present in the list. What am I missing?
之后,空字符串仍然存在于列表中。我错过了什么?
回答by JB Nizet
filter()
keepsthe elements that match the predicate. Soyou need the inverse predicate:
filter()
保留与谓词匹配的元素。所以你需要逆谓词:
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);
回答by Peter Lawrey
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());
回答by lczapski
Since java 11 there is a new method Predicate::not.
从 java 11 开始,有一个新方法Predicate::not。
So you can write it like that:
所以你可以这样写:
list.stream().filter(Predicate.not(String::isEmpty)).collect(Collectors.toList())
回答by Adam Siemion
Maybe the string contains whitespaces? Replace item -> item.isEmpty()
with item -> !item.trim().isEmpty()
也许字符串包含空格?替换item -> item.isEmpty()
为item -> !item.trim().isEmpty()
回答by ΦXoc? ? Пepeúpa ツ
you are collecting the empty elements :)
您正在收集空元素:)
you want the not empty so invert the predicate
你想要非空所以反转谓词
List<String> xxx = list.stream().filter(item -> !item.isEmpty()).collect(Collectors.toList());
additionally, the stream is not modifying the original list, so Collect(Collectrors.toList())
is returning the result of the predicate :)
此外,流没有修改原始列表,因此Collect(Collectrors.toList())
返回谓词的结果:)
回答by Sangram Badi
you can follow below example, it may help you
你可以按照下面的例子,它可能会帮助你
String[] firstArray = {"test1", "", "test2", "test4", "", null};
firstArray = Arrays.stream(firstArray).filter(s -> (s != null && s.length() > 0)).toArray(String[]::new);
回答by printfabcd
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5-SNAPSHOT</version>
</dependency>
import org.apache.commons.lang3.StringUtils;
//Java8 new api
strings.removeIf(StringUtils::isBlank);
//stream strings=strings.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
回答by Mohammed Imran
Might be your List contains null. So first filter with null check then again filter on empty
可能是您的列表包含空值。所以首先过滤空检查然后再次过滤空
list.stream().filter(item -> null != item).filter(item -> !item.isEmpty()).collect(Collectors.toList());
If you have apache commons lang lib use Apache StringUtils isNotBlank
如果你有 apache commons lang lib 使用 Apache StringUtils isNotBlank
list.stream().filter(item -> StrinUtils.isNotBlank(item)).collect(Collectors.toList());
回答by ernest_k
You are removing non-empty strings (check your filter condition). Try:
您正在删除非空字符串(检查您的过滤条件)。尝试:
list.stream().filter(item -> !item.isEmpty()).collect(Collectors.toList());
Otherwise, you may need to check that you're removing blanks as well:
否则,您可能需要检查是否也删除了空格:
list.stream().filter(item -> !item.trim().isEmpty()).collect(Collectors.toList());
回答by Andrei Olar
You are not removing empty Strings from the list, you were retrieving them. Your filter condition is wrong.
您不是从列表中删除空字符串,而是在检索它们。您的过滤条件错误。
Try like this:
像这样尝试:
List<String> collect = list.stream().filter(item -> !item.trim().isEmpty()).collect(Collectors.toList());