在应用收集之前,如何从流中删除零元素?
我的流函数有时会返回null
,当我收集它们时如何删除该null
返回?
versions.stream().map(vs->{
if(vs.matches("^matched string$")) {
...
return new VersionNumber(tmp[0], tmp[1], tmp[2]));
}
return null;
}).flatMap(Optional::stream).collect(Collectors.toList());
对于此流函数,如果所有匹配均为false
,则是指map()
方法中的所有功能,它将上升nullpointException
。
如何使此流不引起异常,并且当所有元素均为null
使其返回空列表或null
?
My stream function sometime returns null
, when I do collect them How to delete that null
return?
versions.stream().map(vs->{
if(vs.matches("^matched stringquot;)) {
...
return new VersionNumber(tmp[0], tmp[1], tmp[2]));
}
return null;
}).flatMap(Optional::stream).collect(Collectors.toList());
For this stream functions, if all matched is false
, I mean if all the function inside the map()
method, it will rise NullPointException
.
How to make this stream not rise an exception and when all elements are null
make it to return an empty list or null
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以后来过滤
,但这是更明智的过滤
You can filter afterward
But that is wiser to filter the sooner
因为您正在获得
nullpointerexception
,这意味着您的代码编译和编译代码跑步。因此,我假设map()
操作中的“代码”不是您的实际代码选项。flatmap(可选:: stream)
仅当先前的map()
产生类型可选< versionNumber>
时,才不会导致汇编错误。因此,我的建议很简单:永远不会返回
null
代替可选的。当可选对象可能是null
时,它应该包含一个值或为空是完全错误的,但绝不应该是null
。正如 @andy thomas 在评论中指出的那样,如果
no 如果
可选
如果代码map()中的代码()
真正在现场创建对象,例如new versionNumber(TMP [0],TMP [1],TMP [2]));
。在这种情况下,将map()
的结果与可选
一起包装,将是对可选的滥用。返回versionNumber
实例或null
。然后应用一个过滤器:您必须求助于可选的 ,如果您实际上是不是而不是创建
version> versionnumber
当您've显示了,但是,例如,如果某种条件是肉,请返回可选>
的API调用。在这种情况下,无需应用过滤。
optional.stream()
在空的可选时调用时会产生一个空流。Since you're getting a
NullPointerException
that means that your code compiles and running. Therefore, I assume that the "code" inside themap()
operation isn't your actual code because we can't treat elements in the ofStream<VersionNumber>
as optionals.flatMap(Optional::stream)
will not cause a compilation error only if precedingmap()
produces a result of typeOptional<VersionNumber>
.Hence, my suggestion is simple: never return
null
in place of optional. It's completely wrong when an optional object might benull
, it should either contain a value or be empty, but it should never benull
.And as @Andy Thomas has pointed out in the comment, there's no reason to utilize
Optional
if the code inside themap()
really creates the object on the spot likenew VersionNumber(tmp[0], tmp[1], tmp[2]));
. In this case, wrapping the result of themap()
with anOptional
will be a misuse of optional. Return eitherVersionNumber
instance ornull
. And then apply a filter:You have to resort to the usage of optional only if inside the map you're actually not creating
VersionNumber
as you've shown, but, for instance, making an API call which return anOptional<VersionNumber>
if some condition is meat.In such a case, there's no need to apply filtering.
Optional.stream()
when invoked on an empty optional produces an empty stream.从Java 16开始,还有
stream.mapmulti
:(强调矿山)
使用此矿场,您的示例变为:
请注意,零值根本不是下游发射的,因此无需过滤或中间流以进行平面映射。
另一个例子:
印刷:
3个橙子,但没有香蕉
As of java 16, there is also
Stream.mapMulti
:(emphasis mine)
Using this, your example becomes:
Note that the null values are not emitted downstream at all, so there is no need for filtering or intermediate streams for flat mapping.
Another example:
Prints:
3 oranges, but no banana