标签:
涉及到列表的数据时,总是会想到一个过滤这个词语。比如,在1-100的整数中,筛选出偶数或者奇数相加,或者将前49个数相加,又或者后36个数相加,等等。在这样的场景中,不由想到将需要的数据筛选出来。在发射的Observable中,可不可以做筛选呢?
filter操作符是对源Observable产生的结果按照指定条件进行过滤,只有满足条件的结果才会提交给订阅者。
其流程图如下:
Observable.from(mLists) .filter(new Func1<Student, Boolean>() { @Override public Boolean call(Student student) { return student.getName().startsWith("A"); } }) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
Observable.from(mLists) .filter(student -> student.getName().startsWith("A")) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
.filter(new Func1<AppInfo,Boolean>(){ @Override public Boolean call(AppInfo appInfo){ return appInfo != null; } })
take操作符是用整数N来作为一个参数,把源Observable产生的结果,提取前面的N个提交给订阅者,而忽略后面的结果。
其流程图如下
Observable.from(mLists) .take(5) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
takeLast操作符是把源Observable产生的结果的后n项提交给订阅者,提交时机是Observable发布onCompleted通知之时。
其流程图如下:
Observable.from(mLists) .takeLast(5) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
Observable.from(mLists) .takeFirst(new Func1<Student, Boolean>() { @Override public Boolean call(Student student) { return student.getName().startsWith("B"); } }) .subscribe(new Observer<Student>() { @Override public void onCompleted() { Log.i("123", "doTakeFirst - onCompleted"); } @Override public void onError(Throwable e) { Log.i("123", "doTakeFirst - onError"); } @Override public void onNext(Student student) { Log.i("123", "doTakeFirst - onNext"); mAdaStu.addData(student); } });
Observable<AppInfo> fullOfDuplicates = Observable.from(apps) .take(3) .repeat(3);从上述代码可知,fullOfDuplicates变量里把List的前三个重复了3次,有9个并且许多重复的,然后使用distinct进行过滤。
fullOfDuplicates.distinct() .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
distinctUntilChanged操作符的流程图如下:
Observable.from(mLists) .distinctUntilChanged(new Func1<Student, String>() { @Override public String call(Student student) { return student.getAge(); } }) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
Observable.from(mLists) .first() .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
.first(new Func1<Student, Boolean>() { @Override public Boolean call(Student student) { return student.getName().startsWith("B"); } }) //Lamada写法 .first(student -> student.getName().startsWith("B"))
效果图
Observable.from(mLists) .last() .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
Observable.from(mLists) .skip(2) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
Observable.from(mLists) .skipLast(6) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
Observable.from(mLists) .elementAt(2) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
Observable<Integer> sensor = [...] sensor.sample(30,TimeUnit.SECONDS) .subscribe(new Observer<Integer>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Integer currentTemperature) { updateDisplay(currentTemperature) } });
Subscription subscription = getCurrentTemperature() .timeout(2,TimeUnit.SECONDS) .subscribe(new Observer<Integer>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Log.d("RXJAVA","You should go check the sensor, dude"); } @Override public void onNext(Integer currentTemperature) { updateDisplay(currentTemperature) } });
标签:
原文地址:http://blog.csdn.net/io_field/article/details/51378909