我有Observable
的{{1}}:
Lists
如何删除重复项:
Observable<List<String>> source = Observable.just(
List.of("a", "c", "e"),
List.of("a", "b", "c", "d"),
List.of("d", "e", "f")
);
我可以累积以前的排放量,只需要像上面那样转换即可。
答案 0 :(得分:1)
我使用scan
运算符和辅助程序类来实现它,该类存储当前值和先前值:
static class Distinct {
final HashSet<String> previous;
final List<String> current;
public Distinct(HashSet<String> previous, List<String> current) {
this.previous = previous;
this.current = current;
}
}
Observable<List<String>> source = Observable.just(
List.of("a", "c", "e"),
List.of("a", "b", "c", "d"),
List.of("d", "e", "f")
);
source.scan(new Distinct(new HashSet<>(), new ArrayList<>()), (acc, item) -> {
var newItem = new ArrayList<String>();
item.forEach(i -> {
if (acc.previous.add(i))
newItem.add(i);
});
return new Distinct(acc.previous, newItem);
})
.skip(1)
.map(md -> md.current)
.subscribe(System.out::println);
输出:
[a, c, e]
[b, d]
[f]