学习新的东西还是要写下博客,不然太容易忘记了。。。
RxJava的操作符
操作符分为以下几类
interval | 创建一个按固定时间间隔发送整数序列的Observable |
range | 创建发送指定范围的整型序列的Observable |
repeat | 创建一个N次重复发射特定数据的Observable |
示例代码如下:
package com.magic.rxjava;
import android.util.Log;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
/***
*@author Create By Magic
*@date 2019/5/6
**/
public class RxJavaCreateOperateSymbol extends RxJava {
private static final String TAG = "RxJavaCreateOperateSymb";
/**
* 创建一个按固定时间间隔发送整数序列的Observable
* 结果会从0开始打印,每隔1s加1
*/
public void interval() {
Disposable disposable = Observable.interval(1, TimeUnit.SECONDS)
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
Log.i(TAG, "accept: " + aLong);
}
});
}
/**
* 创建发送指定范围的整型序列的Observable
* 第一个参数为起始值,第二个参数值为数量
*/
public void range(){
Disposable disposable = Observable.range(0, 88)
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.i(TAG, "accept: " + integer);
}
});
}
/**
* repeat 一般和range连用
* 表示重复某个range范围内的次数
*/
public void repeat(){
Disposable disposable = Observable.range(0,5).repeat(10)
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.i(TAG, "accept: " + integer);
}
});
}
}
map | 指定一个Func对象将Observable转换成一个新的Observable发射出去 |
flapMap | 将Observable发送的数据集合转换成Observable集合发射出去 注意:flapMap不保证发射的顺序 |
cast | 强制将Observable发送的所有数据类型转换为指定类型 |
concatMap | 将Observable发送的数据集合转换成Observable集合发射出去 注意:concatMap保证发射的顺序 |
flatMapIterable | 可以将数据包装成Iterable |
buffer | 將源Observable变换成一个新的Observable,每次发射一组列表值而不是一个一个发 |
groupBy | 用于分组元素 |
package com.magic.rxjava;
import android.util.Log;
import com.bean.School;
import com.bean.Student;
import com.bean.Teacher;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.observables.GroupedObservable;
import io.reactivex.schedulers.Schedulers;
/***
*@author Create By Magic
*@date 2019/5/4
**/
public class RxJavaOperateSymbol {
private static final String TAG = "RxJavaOperateSymbol";
private Student student1 = new Student(1, "1");
private Student student2 = new Student(2, "2");
private Student student3 = new Student(2, "3");
private Student student4 = new Student(2, "4");
private Student student5 = new Student(3, "5");
private Student student6 = new Student(1, "6");
private Teacher teacher1 = new Teacher("小学1", "王老师");
private Teacher teacher2 = new Teacher("小学2", "李老师");
private Teacher teacher3 = new Teacher("小学3", "刘老师");
Observable<Student> mStudentObservable = Observable.create(new ObservableOnSubscribe<Student>() {
@Override
public void subscribe(ObservableEmitter<Student> emitter) throws Exception {
emitter.onNext(student1);
emitter.onNext(student2);
emitter.onNext(student3);
emitter.onNext(student4);
emitter.onNext(student5);
emitter.onNext(student6);
}
});
Observable<Teacher> mTeacherObservable = Observable.create(new ObservableOnSubscribe<Teacher>() {
@Override
public void subscribe(ObservableEmitter<Teacher> emitter) throws Exception {
emitter.onNext(teacher1);
Log.i(TAG, "subscribe: emitter teacher 1");
emitter.onNext(teacher2);
Log.i(TAG, "subscribe: emitter teacher 2");
emitter.onNext(teacher3);
Log.i(TAG, "subscribe: emitter teacher 3");
}
});
Observer<School> mSchoolObservable = new Observer<School>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(School school) {
Log.i(TAG, "[School] onNext: " + school);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
};
Observer<Student> mStudentObserver = new Observer<Student>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Student student) {
Log.i(TAG, "onNext: " + student);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
};
/**
* map操作符用户转换类型、或者做一些操作
*/
public void map() {
Function<Student, Student> mapFunction = new Function<Student, Student>() {
@Override
public Student apply(Student student) throws Exception {
student.setAge(student.getAge() + 1);
student.setName(student.getName() + "_change");
return student;
}
};
mStudentObservable.map(mapFunction).subscribe(mStudentObserver);
}
/**
* flapMap 将数据集合变成Observerble集合
* flapMap是无序的
*/
public void flapMap() {
Function flatFunction = new Function<Student, ObservableSource<Student>>() {
@Override
public ObservableSource<Student> apply(Student student) throws Exception {
List<Student> studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
//后面增加100ms延迟是为了显示flapMap是无序的
return Observable.fromIterable(studentList).delay(100, TimeUnit.MILLISECONDS);
}
};
mStudentObservable.flatMap(flatFunction).subscribe(mStudentObserver);
}
public void cast() {
Observable.just(1, 2, 3, 4, 5).cast(Integer.class)
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer s) throws Exception {
Log.i(TAG, "accept: " + s);
}
});
}
/**
* concatMap 将数据集合变成Observerble集合
* concatMap 是有序的
*/
public void concatMap() {
Function fconcatMapFunction = new Function<Student, ObservableSource<Student>>() {
@Override
public ObservableSource<Student> apply(Student student) throws Exception {
List<Student> studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
//后面增加100ms延迟是为了显示flapMap是无序的
return Observable.fromIterable(studentList).delay(100, TimeUnit.MILLISECONDS);
}
};
mStudentObservable.concatMap(fconcatMapFunction).subscribe(mStudentObserver);
}
/**
* zip 用于在发送的时候合并多个Observable
* 注意由于两个Observable都是在主线程中进行所以需要分别指定其运行的线程
*/
public void zip() {
BiFunction biFunction = new BiFunction<Student, Teacher, School>() {
@Override
public School apply(Student student, Teacher teacher) throws Exception {
return new School(student, teacher);
}
};
Observable<Student> studentObservable = mStudentObservable
.subscribeOn(Schedulers.newThread());
Observable<Teacher> teacherObservable = mTeacherObservable
.subscribeOn(Schedulers.newThread());
Observable.zip(studentObservable, teacherObservable, biFunction)
.subscribe(mSchoolObservable);
}
/**
* 过滤器,当符合条件下游才进行接收
*/
public void filter() {
mStudentObservable.delay(1, TimeUnit.SECONDS)
.filter(new Predicate<Student>() {
@Override
public boolean test(Student student) throws Exception {
//过滤学生年龄大于12的学生
return student.getAge() > 12;
}
})
.subscribe(mStudentObserver);
}
/**
* sample 关键字用于定时去取离源最近的数据
*/
public void sample() {
mStudentObservable.sample(1, TimeUnit.SECONDS)
.subscribe(mStudentObserver);
}
public void flapMapIterable() {
mStudentObservable.flatMapIterable(new Function<Student, Iterable<Student>>() {
@Override
public Iterable<Student> apply(Student student) throws Exception {
List<Student> studentList = new ArrayList<>();
student1.setAge(1);
studentList.add(student1);
return studentList;
}
}).subscribe(new Consumer<Student>() {
@Override
public void accept(Student student) throws Exception {
Log.i(TAG, "accept: " + student);
}
});
}
public void groupBy() {
mStudentObservable.groupBy(new Function<Student, Integer>() {
@Override
public Integer apply(Student student) throws Exception {
return student.getAge() == 2?0:1;
}
}).subscribe(new Consumer<GroupedObservable<Integer, Student>>() {
@Override
public void accept(final GroupedObservable<Integer, Student> integerStudentGroupedObservable) throws Exception {
integerStudentGroupedObservable.subscribe(new Consumer<Student>() {
@Override
public void accept(Student student) throws Exception {
Log.i(TAG, "accept: "+student+" [age]"+integerStudentGroupedObservable.getKey());
}
});
}
});
}
public void buffer() {
mStudentObservable.buffer(2).subscribe(new Consumer<List<Student>>() {
@Override
public void accept(List<Student> students) throws Exception {
for (int i = 0; i < students.size(); i++) {
Log.i(TAG, "accept: " + students.get(i));
}
Log.i(TAG, "accept: ==========");
}
});
}
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}