【问题标题】:prepare map using Observable Rx-java使用 Observable Rx-java 准备地图
【发布时间】:2017-03-23 05:42:50
【问题描述】:

这是我的示例代码,它从 couchbase 服务器返回 JsonDocument 列表。

Cluster cluster = CouchbaseCluster.create();
Bucket bucket = cluster.openBucket();

List<JsonDocument> foundDocs = Observable
    .just("key1", "key2", "key3", "key4", "key5")
    .flatMap(new Func1<String, Observable<JsonDocument>>() {
        @Override
        public Observable<JsonDocument> call(String id) {
            return bucket.async().get(id);
        }
    })
    .toList()
    .toBlocking()
    .single();

我想返回Map 而不是List。我的返回类型是Map&lt;String, JsonDocument&gt;。 我尝试了toMap 方法,但它对我不起作用。

【问题讨论】:

  • 请提供您尝试的代码以及您得到的结果。
  • 方法toMap需要函数作为参数,我不知道怎么写
  • 你的问题是结合 key 和 flatMap 结果吗?然后检查这个 - stackoverflow.com/a/41820372/7045114

标签: java rx-java couchbase couchbase-java-api


【解决方案1】:

您在评论中正确提到toMap 需要Function 作为其参数。这个Function 将用于“提取”(或者可能是“构造”)键,每个值将在该键下输入到映射中。

因此,在您的情况下,您将需要一个Function&lt;JsonDocument, String&gt;,它将JsonDocument 作为其输入并以某种方式返回一个String,您稍后将使用它在Map 中找到JsonDocumentString 将是什么只有您可以回答 - 也许JsonDocument 中有一些 ID?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    相关资源
    最近更新 更多