3.过滤操作符 Filtering Observables
将上级的数据按照制定规则选择后再发射出去
Operators that selectively emit items from a source Observable.
● Debounce
● Distinct
● ElementAt
● Filter
● First
● IgnoreElements
● Last
● Sample
● throttleFirst
● throttleLast
● Skip
● skipLast
● SkipUntil
● SkipWhile
● Take
● TakeLast
● TakeUntil
● TakeWhile
● Debounce
在特定的时间间隔里,只发射一个数据,当接收到的相邻两个数据的时间间隔,超过该函数指定的timestep时,丢弃先接收到的,再以新接收到数据的时间计时,判断下一个数据是否发射。
● throttleWithTimeout
● throttleWithTimeout
throttleWithTimeout方法内部是直接调用Debounce,所以和 Debounce方法作用相同。
Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
// send events with simulated time wait
emitter.onNext(1); // skip
Thread.sleep(400);
emitter.onNext(2); // deliver
Thread.sleep(505);
emitter.onNext(3); // skip
Thread.sleep(100);
emitter.onNext(4); // skip
Thread.sleep(300);
emitter.onNext(5); // skip
Thread.sleep(410);
emitter.onNext(6); // skip
Thread.sleep(205);
emitter.onNext(7);//deliver
emitter.onComplete();
}
}).debounce(500,TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(Integer integer) throws Exception {
print("accept:"+integer);//2,7
}
});
● Distinct
去除Observable发射一组数据当中重复的数据
要区别DistinctUntilChanged
该方法表示去除Observable发射一组数据当中连续重复的数据
Observable.from(new Integer[]{1,2,2,3,3,3,2,2,1}).distinct().subscribe(new Action1() {
@Override
public void call(Integer integer) {
Log.d(TAG, "integer=" + integer); //1,2,3
}
});
Observable.from(new Integer[]{1,2,2,3,3,3,2,2,1}).distinctUntilChanged().subscribe(new Action1() {
@Override
public void call(Integer integer) {
Log.d(TAG, "integer=" + integer); //1,2,3,2,1
}
});
● ElementAt
Observable发射一组数据当中,发射指定下标数据
Observable.from(new Integer[]{1,2,4,3,6,7,12,22,13}).elementAt(4).subscribe(new Action1() {
@Override
public void call(Integer integer) {
Log.d(TAG, "integer=" + integer); //6
}
});
● Filter
Observable调用该操作函数,进行数据筛选
● First
Observable发射一组数据当中,只发射第一个数据
Observable.just(1,2,3)
.first(0)//默认值
.subscribe(new Consumer() {
@Override
public void accept(Integer aInteger) throws Exception {
print("first Accept:"+aInteger);//1
}
});
● IgnoreElements
Observable发射一组数据当中, 数据数据全部丢弃
● Last
Observable发射一组数据当中,只发射最后一个数据
Observable.fromArray(arr)
.last("--")//默认值
.subscribe(new Consumer() {
@Override
public void accept(String aLong) throws Exception {
print("last Accept:"+aLong);//gg
}
});
● Sample
在一个特定时间间隔里采样,发射这段时间里最新收到的数据,其他丢弃
● ThrottleFirst
在一个特定时间间隔里,发射最先收到的数据,丢弃到其他数据,并且是发射完成一个数据以后再重新开始计时
● ThrottleLast
他是smaple的另一个版本,在一个特定时间间隔里,发射最新收到的数据, 区别在于Throttle会在上级发射一个数据完成以后计时。而smaple是上级发射数据的同时计时
Observable.intervalRange(1,20,0,300, TimeUnit.MILLISECONDS)
.sample(1, TimeUnit.SECONDS, Schedulers.newThread())
.subscribe(new Consumer() {
@Override
public void accept(Long aLong) throws Exception {
print("sample Accept:"+aLong);
}
});
Observable.intervalRange(1,20,5000,300, TimeUnit.MILLISECONDS)
.throttleFirst(1, TimeUnit.SECONDS, Schedulers.newThread())
.subscribe(new Consumer() {
@Override
public void accept(Long aLong) throws Exception {
print("throttleFirst Accept:"+aLong);
}
});
Observable.intervalRange(1,20,10000,300, TimeUnit.MILLISECONDS)
.throttleLast(1, TimeUnit.SECONDS, Schedulers.newThread())
.subscribe(new Consumer() {
@Override
public void accept(Long aLong) throws Exception {
print("throttleLast Accept:"+aLong);
}
});
// 11-07 15:53:57.386 7972-8005/com.callanna.demo D/duanyl: sample Accept:4
// 11-07 15:53:58.386 7972-8005/com.callanna.demo D/duanyl: sample Accept:7
// 11-07 15:53:59.386 7972-8005/com.callanna.demo D/duanyl: sample Accept:10
// 11-07 15:54:00.386 7972-8005/com.callanna.demo D/duanyl: sample Accept:14
// 11-07 15:54:01.386 7972-8005/com.callanna.demo D/duanyl: sample Accept:17
// 11-07 15:54:01.396 7972-8007/com.callanna.demo D/duanyl: throttleFirst Accept:1
// 11-07 15:54:02.596 7972-8007/com.callanna.demo D/duanyl: throttleFirst Accept:5
// 11-07 15:54:03.796 7972-8007/com.callanna.demo D/duanyl: throttleFirst Accept:9
// 11-07 15:54:04.996 7972-8007/com.callanna.demo D/duanyl: throttleFirst Accept:13
// 11-07 15:54:06.196 7972-8007/com.callanna.demo D/duanyl: throttleFirst Accept:17
// 11-07 15:54:07.396 7972-8008/com.callanna.demo D/duanyl: throttleLast Accept:4
// 11-07 15:54:08.396 7972-8008/com.callanna.demo D/duanyl: throttleLast Accept:7
// 11-07 15:54:09.396 7972-8008/com.callanna.demo D/duanyl: throttleLast Accept:10
// 11-07 15:54:10.396 7972-8008/com.callanna.demo D/duanyl: throttleLast Accept:14
// 11-07 15:54:11.396 7972-8008/com.callanna.demo D/duanyl: throttleLast Accept:17
● Skip
一组数据,从第一个开始,丢弃指定个数的数据
Observable.intervalRange(100,10,3000,300, TimeUnit.MILLISECONDS)
.skip(5)
.subscribe(new Consumer() {
@Override
public void accept(Long aLong) throws Exception {
print("skip Accept:"+aLong);//105,106,107,108,109
}
});
● SkipLast ‘
一组数据,丢弃从最后一个开始,倒着指定个数的数据
Observable.fromArray(new String[]{"aaa", "bb", "ccc", "dd", "eee","fff","gg"})
.skipLast(3)
.subscribe(new Consumer() {
@Override
public void accept(String aLong) throws Exception {
print("skipLast Accept:"+aLong);
//"aaa", "bb", "ccc", "dd"
}
});
● SkipUntil
当Until指定Observable的数据开始发射的时候,再发射数据
Observable.intervalRange(10,10,0,300, TimeUnit.MILLISECONDS)
.doOnSubscribe(new Consumer() {
@Override
public void accept(Disposable disposable) throws Exception {
print("skipUntil doOnSubscribe");
}
})
.skipUntil(Observable.timer(2000,TimeUnit.MILLISECONDS))
.subscribe(new Consumer() {
@Override
public void accept(Long aLong) throws Exception {
print("skipUntil Accept:"+aLong);
}
});
// 11-07 14:26:35.776 6302-6302/com.callanna.demo D/duanyl: skipUntil doOnSubscribe
// 11-07 14:26:37.876 6302-6324/com.callanna.demo D/duanyl: skipUntil Accept:17
// 11-07 14:26:38.176 6302-6324/com.callanna.demo D/duanyl: skipUntil Accept:18
// 11-07 14:26:38.476 6302-6324/com.callanna.demo D/duanyl: skipUntil Accept:19
● SkipWhile
从一个数据不满足一个条件的数据,并且包括该数据发射
Observable.fromArray(new String[]{"aaa", "bb", "ccc", "dd", "eee","fff","gg"})
.doOnSubscribe(new Consumer() {
@Override
public void accept(Disposable disposable) throws Exception {
print("skipWhile doOnSubscribe");
}
})
.skipWhile(new Predicate() {
@Override
public boolean test(String s) throws Exception {
return !s.equals("ccc");
}
})
.subscribe(new Consumer() {
@Override
public void accept(String aLong) throws Exception {
print("skipWhile Accept:"+aLong);
}
});
// 11-07 14:29:07.816 6302-6302/com.callanna.demo D/duanyl: skipWhile doOnSubscribe
// 11-07 14:29:07.816 6302-6302/com.callanna.demo D/duanyl: skipWhile Accept:ccc
// 11-07 14:29:07.816 6302-6302/com.callanna.demo D/duanyl: skipWhile Accept:dd
// 11-07 14:29:07.816 6302-6302/com.callanna.demo D/duanyl: skipWhile Accept:eee
// 11-07 14:29:07.816 6302-6302/com.callanna.demo D/duanyl: skipWhile Accept:fff
// 11-07 14:29:07.816 6302-6302/com.callanna.demo D/duanyl: skipWhile Accept:gg
● Take
Observable.intervalRange(10,10,0,300, TimeUnit.MILLISECONDS)
.take(5)
.subscribe(new Consumer() {
@Override
public void accept(Long aLong) throws Exception {
print("take Accept:"+aLong);//10,11,12,13,14
}
});
● TakeLast
Observable.fromArray(arr)
.takeLast(3)
.subscribe(new Consumer() {
@Override
public void accept(String aLong) throws Exception {
print("takeLast Accept:"+aLong);
//"eee","fff","gg"
}
});
● TakeUntil
当Until指定Observable的数据开始发射的时候,再发射一组数据的前n个数据
Observable.intervalRange(10,10,5,300, TimeUnit.MILLISECONDS)
.doOnSubscribe(new Consumer() {
@Override
public void accept(Disposable disposable) throws Exception {
print("takeUntil doOnSubscribe");
}
})
.takeUntil(Observable.timer(2000,TimeUnit.MILLISECONDS))
.subscribe(new Consumer() {
@Override
public void accept(Long aLong) throws Exception {
print("takeUntil Accept:"+aLong);
}
});
// 11-07 14:26:35.776 6302-6302/com.callanna.demo D/duanyl: takeUntil doOnSubscribe
// 11-07 14:26:35.786 6302-6326/com.callanna.demo D/duanyl: takeUntil Accept:10
// 11-07 14:26:36.086 6302-6326/com.callanna.demo D/duanyl: takeUntil Accept:11
// 11-07 14:26:36.386 6302-6326/com.callanna.demo D/duanyl: takeUntil Accept:12
// 11-07 14:26:36.676 6302-6326/com.callanna.demo D/duanyl: takeUntil Accept:13
// 11-07 14:26:36.986 6302-6326/com.callanna.demo D/duanyl: takeUntil Accept:14
// 11-07 14:26:37.276 6302-6326/com.callanna.demo D/duanyl: takeUntil Accept:15
// 11-07 14:26:37.576 6302-6326/com.callanna.demo D/duanyl: takeUntil Accept:16
● TakeWhile
发射数据直到数据不满足一个条件的数据,并且不包括该数据
Observable.fromArray(arr)
.doOnSubscribe(new Consumer() {
@Override
public void accept(Disposable disposable) throws Exception {
print("takeWhile doOnSubscribe");
}
})
.takeWhile(new Predicate() {
@Override
public boolean test(String s) throws Exception {
return !s.equals("ccc");
}
})
.subscribe(new Consumer() {
@Override
public void accept(String aLong) throws Exception {
print("takeWhile Accept:"+aLong);
}
});
// 11-07 14:29:07.816 6302-6302/com.callanna.demo D/duanyl: takeWhile doOnSubscribe
// 11-07 14:29:07.816 6302-6302/com.callanna.demo D/duanyl: takeWhile Accept:aaa
// 11-07 14:29:07.816 6302-6302/com.callanna.demo D/duanyl: takeWhile Accept:bb