distinct 去除重复数据
public static void test13() {
// 过滤重复的数据
List<Student> list = new ArrayList<>();
list.add(new Student("张三",18));
list.add(new Student("李四",18));
list.add(new Student("王五",18));
list.add(new Student("张三",28));
Observable
.from(list)
.distinct(new Func1<Student, String>() {
@Override
public String call(Student student) { // 这里表示已学生的名字来作为去重的判断依据
return student.name;
}
})
.subscribe(new Action1<Student>() {
@Override
public void call(Student student) {
Log.v(TAG, "...call = " +student.name + " " + student.age);
}
});
}
Filter 过滤:
public static void test14(){
Observable
.range(1,9)
.filter(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer integer) {
return integer > 5; // 大于5的数据才打印
}
})
.subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
Log.v(TAG, "...call = " + integer);
}
});
}
first/last/eleelementAt() 过滤数据
public static void test15() {
// 过滤重复的数据
List<Student> list = new ArrayList<>();
list.add(new Student("张三",18));
list.add(new Student("李四",18));
list.add(new Student("王五",18));
list.add(new Student("张三",28));
Observable
.from(list)
.distinct(new Func1<Student, String>() {
@Override
public String call(Student student) {
return student.name;
}
})
// .first()
// .last()
.elementAt(1) // 根据索引 下标不能越界
.subscribe(new Action1<Student>() {
@Override
public void call(Student student) {
Log.v(TAG, "...call = " +student.name + " " + student.age);
}
});
}
take/takelast
public static void test16() {
// 过滤重复的数据
List<Student> list = new ArrayList<>();
list.add(new Student("张三",18));
list.add(new Student("李四",18));
list.add(new Student("王五",18));
list.add(new Student("张三",28));
Observable
.from(list)
// .take(2)
.takeLast(2) // 获取数组最后两个数据
.subscribe(new Action1<Student>() {
@Override
public void call(Student student) {
Log.v(TAG, "...call = " +student.name + " " + student.age);
}
});
}
Sample&IgnoreElements
Observable
.interval(1000, TimeUnit.MILLISECONDS)//每秒发送1个数字
.sample(2000,TimeUnit.MILLISECONDS)//每2秒采样一次
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
Log.i(TAG, "call: "+aLong);
}
});输出的是0 2 4 6 8......
Observable.just(1, 2, 3, 4, 1)
.ignoreElements()//不发送任何信息 直接发送onCompleted()
.subscribe(new Subscriber<Integer>() {
@Override
public void onCompleted() {
Log.i(TAG, "onCompleted: ");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Integer integer) {
Log.i(TAG, "onNext: "+integer);
}
});