# raw Data #
====================
private List<DataBean> list=new ArrayList<>();
list.add(new DataBean(1))
list.add(new DataBean(8))
list.add(new DataBean(3))
list.add(new DataBean(8))
list.add(new DataBean(9))
Observable.from(list).filter(new Func<DataBean, Integer>() {
@Override
public Integer call(DataBean Data) {
Data.getId()==8;
return <-- position should be Array index
}
}).subscribeOn(Schedulers.io()).subscribe(new Action1<Integer>() {
@Override
public void call(Integer pos) {
}
});
答案 0 :(得分:0)
你可以通过一些有状态(和有点复杂)的运算符来实现这一点:
@Test
public void test() {
Observable.range(1, 10)
.compose(indexOf(v -> v == 5))
.subscribe(System.out::println);
Observable.range(1, 10)
.compose(indexOf(v -> v == 12))
.subscribe(System.out::println);
}
static <T> ObservableTransformer<T, Integer> indexOf(Predicate<? super T> predicate) {
return o -> Observable.defer(() -> {
int[] index = { -1, -1 };
return o.filter(v -> {
index[0]++;
if (predicate.test(v)) {
index[1] = index[0];
return true;
}
return false;
})
.takeWhile(v -> index[1] != -1)
.ignoreElements()
.andThen(Observable.fromCallable(() -> index[1]));
});
}