gpt4 book ai didi

java - RxJava : Return List Containing Past And Current Result

转载 作者:搜寻专家 更新时间:2023-11-01 02:05:33 27 4
gpt4 key购买 nike

我想链接一个连续的数据流并创建一个包含过去结果的结果列表。我可以用下面的代码来做。有没有办法在 rx 链之外没有变量?谢谢!

- [0]
- [0, 1]
- [0, 1, 2]
- [0, 1, 2, 3]
- [0, 1, 2, 3, 4]
- [0, 1, 2, 3, 4, 5]
final List<Long> list = new ArrayList<>();

Observable
.interval(1, TimeUnit.SECONDS)
.subscribe(new Action1<Long>() {
@Override
public void call(Long number) {
list.add(number);
System.out.println("- " + list);
}
});

Thread.sleep(100000000L);

↓↓↓↓↓↓↓↓↓↓

Observable
.interval(1, TimeUnit.SECONDS)
.addToPastResultList() // <--- something like this?
.subscribe(new Action1<List<Long>>() {
@Override
public void call(List<Long> list) {
System.out.println("- " + list);
}
});

Thread.sleep(100000000L);

最佳答案

您正在寻找扫描运算符

Observable.interval(1, TimeUnit.SECONDS)
.scan(new ArrayList<>(), (list, integer) -> {
list.add(integer);
return list;
})
.subscribe(list -> System.out.println(list));

关于java - RxJava : Return List Containing Past And Current Result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35094232/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com