1.过滤操作
filter( ) — 过滤数据
Observable.just(1,2,3,4,5,6) .filter(newFunc1() {@OverridepublicBooleancall(Integer integer){//从数组中,筛选偶数returninteger %2==0; } }).subscribe(newAction1() {@Overridepublicvoidcall(Integer i){ Log.d("RxJava", String.valueOf(i)); }});
结果:
11-06 03:42:04.747 4213-4213/? D/RxJava: 211-06 03:42:04.747 4213-4213/? D/RxJava: 411-06 03:42:04.747 4213-4213/? D/RxJava: 6
takeLast( ) — 只发射最后的N项数据
Observable.just(1,2,3,4,5,6) .takeLast(3)//取最后3项数据.subscribe(newAction1() {@Overridepublicvoidcall(Integer i){ Log.d("RxJava", String.valueOf(i)); } });
结果:
11-06 03:44:18.307 6379-6379/keye.com.rxjavaobserver D/RxJava: 411-06 03:44:18.307 6379-6379/keye.com.rxjavaobserver D/RxJava: 511-06 03:44:18.307 6379-6379/keye.com.rxjavaobserver D/RxJava: 6
last( ) — 只发射最后的一项数据
Observable.just(1,2,3,4,5,6) .last() .subscribe(newAction1() {@Overridepublicvoidcall(Integer i){ Log.d("RxJava", String.valueOf(i)); } });
结果:
11-06 03:49:46.710 6582-6582/? D/RxJava: 6
skip( ) — 跳过开始的N项数据
Observable.just(1,2,3,4,5,6) .skip(3) .subscribe(newAction1() {@Overridepublicvoidcall(Integer i){ Log.d("RxJava", String.valueOf(i)); } });
结果:
11-06 03:49:46.710 6582-6582/? D/RxJava: 411-06 03:49:46.710 6582-6582/? D/RxJava: 511-06 03:49:46.710 6582-6582/? D/RxJava: 6
take( ) — 只发射开始的N项数据
Observable.just(1,2,3,4,5,6) .take(3) .subscribe(i -> { Log.d("RxJava",String.valueOf(i)); });
结果:
11-06 03:49:46.710 6582-6582/? D/RxJava: 111-06 03:49:46.710 6582-6582/? D/RxJava: 211-06 03:49:46.710 6582-6582/? D/RxJava: 3
first( ) and takeFirst( ) — 只发射第一项数据,或者满足某种条件的第一项数据
Observable.just(1,2,3,4,5,6) .first() .subscribe(i -> { Log.d("RxJava",String.valueOf(i)); });
结果:
11-06 03:49:46.710 6582-6582/? D/RxJava: 1
elementAt( ) — 发射第N项数据
Observable.just(1,2,3,4,5,6) .elementAt(3) .subscribe(i -> { Log.d("RxJava",String.valueOf(i)); });
结果:
11-06 03:49:46.710 6582-6582/? D/RxJava: 4
sample( ) or throttleLast( ) — 定期发射Observable最近的数据
Observable.interval(1,TimeUnit.SECONDS) .sample(4, TimeUnit.SECONDS) .subscribe(i -> { Log.d("RxJava",String.valueOf(i)); });
结果:interval()每隔一秒发送整数序列,sample()每隔4秒,获取Observable的数据,结果如下:
-16618/keye.com.rxjavaobserver D/RxJava: 3-16618/keye.com.rxjavaobserver D/RxJava: 6-16618/keye.com.rxjavaobserver D/RxJava: 10-16618/keye.com.rxjavaobserver D/RxJava: 14-16618/keye.com.rxjavaobserver D/RxJava: 18-16618/keye.com.rxjavaobserver D/RxJava: 22
debounce( ) — 只有当Observable在指定的时间后还没有发射数据时,才发射一个数据
Observable.create(newObservable.OnSubscribe() {@Overridepublicvoidcall(Subscriber subscriber){try{for(inti =1; i <10; i++) { subscriber.onNext(i); Thread.sleep(i *1000);//每次发送,延迟i*1秒} subscriber.onCompleted(); }catch(Exception e) { subscriber.onError(e); } }}) .subscribeOn(Schedulers.newThread()) .debounce(3000, TimeUnit.MILLISECONDS)//3秒没有数据,则发送.subscribe(newAction1() {@Overridepublicvoidcall(Integer integer){ Log.d("RxJava", String.valueOf(integer)); } });
结果:前3个数延迟短,没有触发debounce()操作符,第4个数延迟3秒,debounce()生效
30534-30550/keye.com.rxjavaobserver D/RxJava: 430534-30550/keye.com.rxjavaobserver D/RxJava: 530534-30550/keye.com.rxjavaobserver D/RxJava: 630534-30550/keye.com.rxjavaobserver D/RxJava: 730534-30550/keye.com.rxjavaobserver D/RxJava: 8
distinct( ) — 过滤掉重复数据
Observable.just(1,2,1,4,1,6) .distinct() .subscribe(i -> { Log.d("RxJava",String.valueOf(i)); });
结果:
11-06 04:38:49.987 19504-19504/keye.com.rxjavaobserver D/RxJava: 111-06 04:38:49.987 19504-19504/keye.com.rxjavaobserver D/RxJava: 211-06 04:38:49.987 19504-19504/keye.com.rxjavaobserver D/RxJava: 411-06 04:38:49.988 19504-19504/keye.com.rxjavaobserver D/RxJava: 6
ofType( ) — 只发射指定类型的数据
Observable.just(1,"2",3,"4",5,6) .ofType(Integer.class) .subscribe(i -> { Log.d("RxJava", String.valueOf(i)); });
结果:
11-06 04:44:28.321 25785-25785/keye.com.rxjavaobserver D/RxJava: 1
11-06 04:44:28.321 25785-25785/keye.com.rxjavaobserver D/RxJava: 3
11-06 04:44:28.321 25785-25785/keye.com.rxjavaobserver D/RxJava: 5
11-06 04:44:28.321 25785-25785/keye.com.rxjavaobserver D/RxJava: 6