最简单的例子是像这样的字符串流:["3", "a", "b", "c", "1", "a", "2", "a", "b"]数字表示它的组应该包含多少个元素。非常重要的一点是,流是连续的,所以我们不能只是等待下一个数字来分割流。据我所知,RXJava2中没有内置功能var flowable = Flowable.concat(Flowable.fromArray("3", "a", "b", "c", "1", "a", "2", "a", "b"), Flowable.never());flowable/*Something here*/.blockingSubscribe(System.out::println);预期的输出将是:[3, a, b, c][1, a][2, a, b]
1 回答
catspeake
TA贡献1111条经验 获得超0个赞
我后来发现了阿卡诺克的RxJava2扩展包。使用它,我能够构建这个,它可以做我想要的:
var flowable = Flowable.concat(Flowable.fromArray("3", "a", "b", "c", "1", "a", "2", "a", "b"), Flowable.never());
flowable.compose(FlowableTransformers.bufferUntil(new Predicate<>() {
private int remaining = 0;
@Override
public boolean test(String next) {
if(next.chars().allMatch(Character::isDigit)) {
remaining = Integer.parseInt(next);
}
return --remaining < 0;
}
})).blockingSubscribe(System.out::println);
添加回答
举报
0/150
提交
取消