rx-java - 在 RxJava 中将 Observable 转换为 Collection

我想收集集合中发出的每个项目。我是这样做的:

List found = new ArrayList();
controller.search(query)
          .doOnCompleted(new Action0() {
            @Override
            public void call() {
                //Do something with "found" list
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String item) {
                found.add(item);
            }
        });

是否有 RxJava 内置方法来执行此操作?

最佳答案

使用 toList 收集发出的字符串,并在可观察对象完成后将它们作为单个列表发出,例如

controller.search(query)
          .toList()
          .subscribe(new Action1<List<String>>() {
            @Override
            public void call(List<String> found) {
                // Do something with the list
            }
        });

https://stackoverflow.com/questions/38739728/

相关文章:

python - 从 Tkinter Tcl 回调到 python 函数在 Windows 中崩溃

module - 如何导入本地模块?

c - 根据 AMD64 ABI,什么样的 C11 数据类型是数组

c - ArduinoJSON 未定义对 `__cxa_guard_acquire' 的引用

sql - 计算 pl/sql 中游标的行数

php - 如何检查字符串是否包含电子邮件?

amazon-web-services - 将记录写入 Aurora 数据库实例时触发 AWS La

elixir - 为什么 is_atom(nil) 在 elixir 中等于 true?

node.js - 应用程序使用(验证器()); ^ 类型错误 : validator is not

file - 在同一区域将大文件从 S3 下载到 EC2 的最快方法是什么?