前言
前面已经讲了Rxjava相关基础以及基本使用,有不清楚的童鞋可以看Rxjava实践之路[入门篇]。本章主要讲过滤,如何从发射的Observable中选取想要的内容以及学会更多有用的处理技巧。
正题
1、filter
- 筛选符合条件的内容
/**
* 筛选符合条件的内容
* @author leibing
* @createTime 2016/09/18
* @lastModify 2016/09/18
* @param
* @return
*/
private void filterObservable(){
// 使用filter方法过滤内容中不需要的值
Observable.just("A1","B1","A2","C3").filter(new Func1() {
@Override
public Boolean call(String s) {
// 条件筛选以“A”开始的内容
return s.startsWith("A");
}
}).subscribe(new Action1() {
@Override
public void call(String s) {
// 打印结果
System.out.println("ddddddddddddddddddd filter s = " + s);
}
});
}
运行结果如下:
09-18 14:32:29.978 25356-25356/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddddd filter s = A1
09-18 14:32:29.978 25356-25356/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddddd filter s = A2
2、take
- 筛选出原始数据中前n个数据发射
/**
* 筛选出原始数据中前n个数据发射
* @author leibing
* @createTime 2016/09/18
* @lastModify 2016/09/18
* @param
* @return
*/
private void takeObservable(){
// take(n) 提取原数据前n个数据发射
Observable.just("1","2","3","4","5").take(4).subscribe(new Action1() {
@Override
public void call(String s) {
// 打印结果
System.out.println("ddddddddddddddd take s = " + s);
}
});
}
运行结果如下:
09-18 14:46:21.894 424-424/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd take s = 1
09-18 14:46:21.894 424-424/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd take s = 2
09-18 14:46:21.894 424-424/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd take s = 3
09-18 14:46:21.895 424-424/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd take s = 4
3、takelast
- 筛选出原始数据中从倒数前n个数据发射
/**
* 筛选出原始数据中从倒数前n个数据发射
* @author leibing
* @createTime 2016/09/18
* @lastModify 2016/09/18
* @param
* @return
*/
private void takeLastObservable(){
// takeLast(n) 提取原数据倒数前n个数据发射
Observable.just("1","2","3","4","5").takeLast(2).subscribe(new Action1() {
@Override
public void call(String s) {
// 打印结果
System.out.println("dddddddddddddddddd takeLast s = " + s);
}
});
}
4、distinct
- 去掉数据中重复值
/**
* 去掉数据中重复值
* @author leibing
* @createTime 2016/09/18
* @lastModify 2016/09/18
* @param
* @return
*/
private void distinctObservable(){
// 将发射数据重复五遍再去重复
Observable.just("A1","A1","B1","C1","D1","B1","A1").repeat(5).distinct().subscribe(new Action1() {
@Override
public void call(String s) {
// 打印结果
System.out.println("ddddddddddddddddd distinct s = " + s);
}
});
}
运行结果如下:
09-18 14:54:35.616 6187-6187/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddd distinct s = A1
09-18 14:54:35.616 6187-6187/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddd distinct s = B1
09-18 14:54:35.617 6187-6187/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddd distinct s = C1
09-18 14:54:35.617 6187-6187/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddd distinct s = D1
5、skip
- 筛选出原始数据中前n个数据跳过不发射
/**
* 筛选出原始数据中前n个数据跳过不发射
* @author leibing
* @createTime 2016/09/18
* @lastModify 2016/09/18
* @param
* @return
*/
private void skipObservable(){
// skip(n) 提取原数据前n个数据跳过不发射
Observable.just("1","2","3","4","5").skip(4).subscribe(new Action1() {
@Override
public void call(String s) {
// 打印结果
System.out.println("ddddddddddddddd skip s = " + s);
}
});
}
运行结果如下:
09-18 15:04:10.187 11687-11687/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd skip s = 5
6、skiplast
- 筛选出原始数据中从倒数前n个数据跳过不发射
/**
* 筛选出原始数据中从倒数前n个数据跳过不发射
* @author leibing
* @createTime 2016/09/18
* @lastModify 2016/09/18
* @param
* @return
*/
private void skipLastObservable(){
// takeLast(n) 提取原数据倒数前n个数据跳过不发射
Observable.just("1","2","3","4","5").skipLast(2).subscribe(new Action1() {
@Override
public void call(String s) {
// 打印结果
System.out.println("dddddddddddddddddd skipLast s = " + s);
}
});
}
运行结果如下:
09-18 15:05:31.576 11687-11687/cn.jianke.rxjavafilterdemo I/System.out: dddddddddddddddddd skipLast s = 1
09-18 15:05:31.577 11687-11687/cn.jianke.rxjavafilterdemo I/System.out: dddddddddddddddddd skipLast s = 2
09-18 15:05:31.578 11687-11687/cn.jianke.rxjavafilterdemo I/System.out: dddddddddddddddddd skipLast s = 3
7、timeout
- 在指定的时间间隔内Observable不发射值的话,监听的原始的Observable时就会触发onError()函数
/**
* 在指定的时间间隔内Observable不发射值的话,监听的原始的Observable时就会触发onError()函数
* @author leibing
* @createTime 2016/09/18
* @lastModify 2016/09/18
* @param
* @return
*/
private void timeoutObservable(){
// 指定4秒后再发射,再指定3秒后如果未发射就触发onError函数
Observable.just("1","2").timer(4, TimeUnit.SECONDS).timeout(3, TimeUnit.SECONDS).subscribe(
new Observer() {
@Override
public void onCompleted() {
System.out.println("dddddddddddddddd timeout onCompleted = ");
}
@Override
public void onError(Throwable e) {
System.out.println("dddddddddddddddd timeout e = " + e.getMessage());
}
@Override
public void onNext(Long aLong) {
System.out.println("dddddddddddddddd timeout along = " + aLong);
}
});
}
运行结果如下:
09-18 15:28:34.142 25661-25812/cn.jianke.rxjavafilterdemo I/System.out: dddddddddddddddd timeout e = null
demo地址:RxjavaFilterDemo
总结
目前笔者觉得以上七种过滤比较实用,在此抛砖引玉,欢迎阅读此文章的童鞋拍砖,多多指教!下章准备写Rxjava Observable变换的实践总结!敬请关注!
作者
- github:leibing@github
- email:[email protected]
- qq:872721111