[英]A difference between Mono.mapNotNull(....) and Mono.flatMap(Mono.justOrEmpty(....))
Being a novice to Project Reactor I'm trying to wrap my head around the documentation.作为 Project Reactor 的新手,我正在努力研究文档。 However I still can't figure out if there any practical difference between the following (pseudo-)code:但是我仍然无法弄清楚以下(伪)代码之间是否存在任何实际差异:
aMonoObject
.flatMap(value -> Mono.justOrEmpty(transform(value))
.someOtherProcessing(...)
and和
aMonoObject
.mapNotNull(value -> transform(value))
.someOtherProcessing(...)
given that transform
is a transformation method which is declared like this:鉴于transform
是一种转换方法,声明如下:
@Nullable <T, R> R transform(T value);
The activity diagrams look quite similar in the docs , I'm not sure that I understand everything correctly. 文档中的活动图看起来非常相似,我不确定我是否正确理解了所有内容。
Thank you in advance.先感谢您。
The result will be the same but there is a semantic difference between the two.结果将相同,但两者之间存在语义差异。 In your case, the mapNotNull
is a better choice because it does exactly what you need: transforms the emitted value within the existing stream. You could use Mono.justOrEmpty
if you wanted to start a new stream.在您的情况下, mapNotNull
是更好的选择,因为它完全满足您的需要:在现有 stream 中转换发出的值。如果您想启动一个新的 stream,您可以使用Mono.justOrEmpty
。
A similar question would be what is the difference between Mono.just
and map
operator.一个类似的问题是Mono.just
和map
运算符之间有什么区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.