Rx java 如何基于列表项中以前的发射删除重复项
我有一个Rx java 如何基于列表项中以前的发射删除重复项,rx-java,reactive-programming,rx-java2,Rx Java,Reactive Programming,Rx Java2,我有一个可观察的列表: Observable<List<String>> source = Observable.just( List.of("a", "c", "e"), List.of("a", "b", "c", "d"), List.of("d", "e", "f") ); 我可以累积以前的排放量,只需要像上面那样进行转换 我使用scan操作符和助手类实现它,该类存储当前值和以前的值: static class D
可观察的列表
:
Observable<List<String>> source = Observable.just(
List.of("a", "c", "e"),
List.of("a", "b", "c", "d"),
List.of("d", "e", "f")
);
我可以累积以前的排放量,只需要像上面那样进行转换 我使用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);
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]