当对每个元素的操作产生不同的结果时,保持初始可迭代排序顺序稳定 counts/durations

Keep the initial iterable sorting order stable when operations on each element yield different result counts/durations

我无法理解在 1 次排放被解包为多个项目后如何对排放进行分类。我有以下代码,请允许我从功能上解释一下。

private Observable<A> getChildren(final A a) {
    final Observable<List<B>> bList = containerService.getBs(a)
            .toSortedList();

    final Observable<? extends List<? extends Z>> children = bList
            .flatMapIterable(items -> items)
            .flatMap(b -> enrichB(b))
            .toList();

    return Observable.zip(
            Observable.just(A),
            children,
            this::addChildrenToA);
}

如果我删除这一行:

.flatMap(b -> enrichB(b))

children 列表将只包含 B,因为没有展开(在 enrichB 步)地方。 总是 在 returned 列表中产生稳定的排序顺序:

B0B1B2B3B4

当我启用该行时,一个 B 会转换为多个 C 项目之一 )。我会 期望 (或者可能希望)这会作为输出发生:

B0,B1,B2,C1,C2,C3,B4(B3解包)

但是 .. 我得到如下输出:

B0,B1,C1,C2,C3,B2,B4

B0,C1,C2,C3,B1,B2,B4

B0,B1,B2,B4,C1,C2,C3

所以顺序"within"被保留了,但是为什么顺序"overall"变得不稳定了?我怀疑是因为 flatMap 将继续对 enrichBtoList()-additions 执行操作由于展开比其他操作花费更多(波动)时间,因此它在随机位置获得 "inserted"。

  1. 我的怀疑是正确的还是我遗漏了什么?
  2. 如何control/block这个流量保持稳定的秩序?最好不要修改模型 C 以在其中包含 Bid 并更改使用的 Comparator

所以答案是使用concatMap而不是flatMap

private Observable<A> getChildren(final A a) {
final Observable<List<B>> bList = containerService.getBs(a)
        .toSortedList();

final Observable<? extends List<? extends Z>> children = bList
        .flatMapIterable(items -> items)
        .concatMap(b -> enrichB(b))
        .toList();

return Observable.zip(
        Observable.just(A),
        children,
        this::addChildrenToA);
}