Java 8 - 从List中过滤空字符串不起作用

时间:2021-07-20 18:15:46

I would like to remove an empty String from List of Strings.

我想从字符串列表中删除一个空字符串。

Here is what I tried, using the stream API:

以下是我尝试使用流API的方法:

list.stream().filter(item-> !item.isEmpty()).collect(Collectors.toList());

and the empty string is still present in the list. What am I missing ?

并且列表中仍然存在空字符串。我错过了什么?

9 个解决方案

#1


14  

filter() keeps the elements that match the predicate. Soyou need the inverse predicate:

filter()保留与谓词匹配的元素。 Soyou需要逆谓词:

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);

#2


3  

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());

#3


2  

Maybe the string contains whitespaces? Replace item -> item.isEmpty() with item -> !item.trim().isEmpty()

也许字符串包含空格?用item - >!item.trim()替换item - > item.isEmpty()。isEmpty()

#4


1  

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())返回谓词的结果:)

#5


0  

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());

#6


0  

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());

#7


0  

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);

#8


0  

     <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());

#9


0  

Might be your List contains null. So first filter with null check then again filter on empty

你的List可能包含null。所以先用null检查过滤然后再过滤空

    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());

#1


14  

filter() keeps the elements that match the predicate. Soyou need the inverse predicate:

filter()保留与谓词匹配的元素。 Soyou需要逆谓词:

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);

#2


3  

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());

#3


2  

Maybe the string contains whitespaces? Replace item -> item.isEmpty() with item -> !item.trim().isEmpty()

也许字符串包含空格?用item - >!item.trim()替换item - > item.isEmpty()。isEmpty()

#4


1  

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())返回谓词的结果:)

#5


0  

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());

#6


0  

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());

#7


0  

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);

#8


0  

     <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());

#9


0  

Might be your List contains null. So first filter with null check then again filter on empty

你的List可能包含null。所以先用null检查过滤然后再过滤空

    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());