Java 8 流字符串空或空过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31380784/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:00:08  来源:igfitidea点击:

Java 8 Stream String Null Or Empty Filter

javajava-8guavajava-stream

提问by ServerSideCat

I've got Google Guava inside Stream:

我在 Stream 中有 Google Guava:

this.map.entrySet().stream()
.filter(entity -> !Strings.isNullOrEmpty(entity.getValue()))
.map(obj -> String.format("%s=%s", obj.getKey(), obj.getValue()))
.collect(Collectors.joining(","))

As you see there is a statement !String.isNullOrEmpty(entity)inside the filter function.

如您所见!String.isNullOrEmpty(entity),过滤器函数中有一条语句。

I don't want to use Guava anymore in the project, so I just want to replace it simply by:

我不想在项目中再使用 Guava,所以我只想简单地替换它:

string == null || string.length() == 0;

How can I do it more elegant?

我怎样才能做得更优雅?

采纳答案by fge

You can write your own predicate:

您可以编写自己的谓词:

final Predicate<Map.Entry<?, String>> valueNotNullOrEmpty
    = e -> e.getValue() != null && !e.getValue().isEmpty();

Then just use valueNotNullOrEmptyas your filter argument.

然后只需valueNotNullOrEmpty用作您的过滤器参数。

回答by Olivier Grégtheitroade

You can create your own Stringsclass with your own predicate:

您可以Strings使用自己的谓词创建自己的类:

public class Strings {
  public static boolean isNotNullOrEmpty (String str) {
    return str != null && !str.isEmpty();
  }
}

Then in your code:

然后在你的代码中:

.filter(Strings::isNotNullOrEmpty)

But as @fge mentionned, you can't use that on a Map.Entry<?,?>...

但正如@fge 所提到的,你不能在Map.Entry<?,?>......

回答by rvertigo

If you prefer to use commons-lang3, StringUtils has

如果你更喜欢使用 commons-lang3,StringUtils 有

  • isEmpty()
  • isNotEmpty()
  • isBlank()
  • isNotBlank()
  • isEmpty()
  • isNotEmpty()
  • isBlank()
  • isNotBlank()

These methods can be used in filters as method references:

这些方法可以在过滤器中用作方法引用:

this.stringList.stream().filter(StringUtils::isNotBlank);

or as lambdas:

或作为 lambdas:

this.stringList.stream().filter(s -> StringUtils.isNotBlank(s));

回答by shmosel

You can break down the filter into two steps:

您可以将过滤器分解为两个步骤:

this.map.entrySet().stream()
    .filter(entity -> entity.getValue() != null)
    .filter(entity -> !entity.getValue().isEmpty())
    .map(obj -> String.format("%s=%s", obj.getKey(), obj.getValue()))
    .collect(Collectors.joining(","))

On a side note, most Map.Entry.toString()implementations do exactly what you're doing in map(), so you could theoretically just do map(Map.Entry::toString). But I wouldn't rely on that unless you're producing a toString()or something that doesn't require documented or deterministic behavior.

在一个侧面说明,大多数Map.Entry.toString()的实现做你在做什么map(),所以你可以在理论上只是做map(Map.Entry::toString)。但我不会依赖它,除非你正在生产toString()不需要记录或确定性行为的东西。

Also, I know you want to abandon Guava, but here's a solution that might make you reconsider:

另外,我知道您想放弃 Guava,但这里有一个可能会让您重新考虑的解决方案:

Joiner.on(',').withKeyValueSeparator("=")
      .join(Maps.filterValues(map, Predicates.not(Strings::isNullOrEmpty)));

回答by Taras Melnyk

It works for me: list.stream().filter(el-> el != null && !el.toString().trim().isEmpty()).collect(Collectors.toList());

这个对我有用: list.stream().filter(el-> el != null && !el.toString().trim().isEmpty()).collect(Collectors.toList());

回答by lczapski

In java 11 there is a new method Predicate::not.

在 java 11 中有一个新方法Predicate::not

So you can filter out empty string :

所以你可以过滤掉空字符串:

list.stream()
  .filter(Objects::nonNull)
  .filter(Predicate.not(String::isEmpty))