当对每个元素的操作产生不同的结果时,保持初始可迭代排序顺序稳定 counts/durations
Keep the initial iterable sorting order stable when operations on each element yield different result counts/durations
我无法理解在 1 次排放被解包为多个项目后如何对排放进行分类。我有以下代码,请允许我从功能上解释一下。
- 数据库中自上而下的内容结构是
A
1:nB
1:nC
- 在这个return逻辑中我们改变上面的;一个
A
可以在 正下方有多个 B
的 and/or C
(未打包 B
)
- 所有类型都从一个共享父类
Z
扩展而来,该父类实现 Comparable
,在重写的 compareTo 上使用自定义 Comparator
排序每种子类的唯一升序 int id(因此对于 B
和 C
,它从 0 重新开始)
- enrichB return 是单个
B
或 多个 C
的基础在某些类型的元数据上
- addChildrenToA 只是一个生成器 (.with)
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 列表中产生稳定的排序顺序:
B0
、B1
、B2
、B3
、B4
当我启用该行时,一个 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 将继续对 enrichB 和 toList()-additions 执行操作由于展开比其他操作花费更多(波动)时间,因此它在随机位置获得 "inserted"。
- 我的怀疑是正确的还是我遗漏了什么?
- 如何control/block这个流量保持稳定的秩序?最好不要修改模型
C
以在其中包含 B
的 id 并更改使用的 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);
}
我无法理解在 1 次排放被解包为多个项目后如何对排放进行分类。我有以下代码,请允许我从功能上解释一下。
- 数据库中自上而下的内容结构是
A
1:nB
1:nC
- 在这个return逻辑中我们改变上面的;一个
A
可以在 正下方有多个 - 所有类型都从一个共享父类
Z
扩展而来,该父类实现Comparable
,在重写的 compareTo 上使用自定义Comparator
排序每种子类的唯一升序 int id(因此对于B
和C
,它从 0 重新开始) - enrichB return 是单个
B
或 多个C
的基础在某些类型的元数据上 - addChildrenToA 只是一个生成器 (.with)
B
的 and/or C
(未打包 B
)
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 列表中产生稳定的排序顺序:
B0
、B1
、B2
、B3
、B4
当我启用该行时,一个 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 将继续对 enrichB 和 toList()-additions 执行操作由于展开比其他操作花费更多(波动)时间,因此它在随机位置获得 "inserted"。
- 我的怀疑是正确的还是我遗漏了什么?
- 如何control/block这个流量保持稳定的秩序?最好不要修改模型
C
以在其中包含B
的 id 并更改使用的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);
}