【发布时间】:2014-12-19 22:14:33
【问题描述】:
我正在使用 Reactor 2.0.0.M1,我正在尝试过滤 Stream。根据我的布尔运算的结果,我想继续使用一个或另一个流。 otherwise() 函数似乎可以做到这一点,但不清楚如何使用它。
我的信息流看起来像这样:
stream.filter(o -> o.isValid());
为了处理o.isValid() 为真的情况,我的理解是我可以直接调用.map() 继续顺流。
为了处理o.isValid() 为假的情况,我可以访问备用.otherwise() 流。
但似乎没有or() 或类似方法,因此似乎无法以完全流畅的方式配置两个流。
我能想到的最好的方法是这样的:
FilterAction<Object> filterAction = stream.filter(o -> o.isValid());
// Returns a 'true' Stream, which might additional operations
filterAction
.map(o -> trueOperation1(o))
.map(o -> trueOperation2(o));
// Returns a 'false' Stream, which might different additional operations
filterAction.otherwise()
.map(o -> falseOperation1(o))
.map(o -> falseOperation2(o));
这真的是最好的方法吗?
【问题讨论】:
标签: java spring reactor project-reactor