Java 8流字符串空或空过滤器 [英] Java 8 Stream String Null Or Empty Filter

查看:1176
本文介绍了Java 8流字符串空或空过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Stream内部有Google Guava:

I've got Google Guava inside Stream:

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

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

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

我不喜欢我不想再在项目中使用Guava了,所以我只想简单地用它来代替:

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

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

我怎样才能更优雅?

推荐答案

你可以编写自己的谓词:

You can write your own predicate:

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

然后只需使用 valueNotNullOrEmpty 作为过滤器参数。

Then just use valueNotNullOrEmpty as your filter argument.

这篇关于Java 8流字符串空或空过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆