标签:
在 RxJava- 操作符之过滤Observables了解到,RxJava过滤操作符的基本使用,主要是针对Observable列表。本篇主要针对Observable列表中对象操作符的使用。用于变换可观测序列来创建一个能够更好的满足我们需求的序列。
Observable.from(mLists) .map(new Func1<Student, Student>() { @Override public Student call(Student student) { StringBuilder sb = new StringBuilder(); String age = student.getAge(); sb.append("AGE - "); sb.append(age); student.setAge(sb.toString()); return student; } }) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
private void doFlatMap() { List<String> lists = new ArrayList<>(); lists.add("Flat - A"); lists.add("Flat - B"); lists.add("Flat - C"); lists.add("Flat - D"); Observable.from(lists) .flatMap(new Func1<String, Observable<Student>>() { @Override public Observable<Student> call(String s) { return getStudentData(s); } }) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } }); } // 针对不同的s发射不同的Observable序列 private Observable<Student> getStudentData(final String s) { List<Student> lists_0 = new ArrayList<>(); if (TextUtils.equals("Flat - A", s) || TextUtils.equals("Concat - A", s)) { lists_0.add(new Student(s + "11", "20", "1101")); lists_0.add(new Student(s + "12", "20", "1102")); lists_0.add(new Student(s + "13", "20", "1103")); } else if (TextUtils.equals("Flat - B", s) || TextUtils.equals("Concat - B", s)) { lists_0.add(new Student(s + "11", "20", "1101")); } else if (TextUtils.equals("Flat - C", s) || TextUtils.equals("Concat - C", s)) { lists_0.add(new Student(s + "11", "20", "1101")); lists_0.add(new Student(s + "12", "20", "1102")); } else if (TextUtils.equals("Flat - D", s) || TextUtils.equals("Concat - D", s)) { lists_0.add(new Student(s + "11", "20", "1101")); lists_0.add(new Student(s + "12", "20", "1102")); lists_0.add(new Student(s + "13", "20", "1103")); lists_0.add(new Student(s + "13", "20", "1104")); } return Observable.from(lists_0); }
List<String> lists = new ArrayList<>(); lists.add("Concat - A"); lists.add("Concat - B"); lists.add("Concat - C"); lists.add("Concat - D"); Observable.from(lists) .concatMap(new Func1<String, Observable<Student>>() { @Override public Observable<Student> call(String s) { return getStudentData(s); } }) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
private void doFlatMapIterable() { List<String> lists = new ArrayList<>(); lists.add("FlatMapIterable - A"); lists.add("FlatMapIterable - B"); lists.add("FlatMapIterable - C"); lists.add("FlatMapIterable - D"); Observable.from(lists) .flatMapIterable(new Func1<String, Iterable<Student>>() { @Override public Iterable<Student> call(String s) { return getStudentList(s); } }) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } }); } // 针对不同的s发射不同的Observable序列 private ArrayList<Student> getStudentList(final String s) { ArrayList<Student> lists_0 = new ArrayList<>(); if (TextUtils.equals("FlatMapIterable - A", s)) { lists_0.add(new Student(s + "11", "20", "1101")); lists_0.add(new Student(s + "12", "20", "1102")); lists_0.add(new Student(s + "13", "20", "1103")); } else if (TextUtils.equals("FlatMapIterable - B", s)) { lists_0.add(new Student(s + "11", "20", "1101")); } else if (TextUtils.equals("FlatMapIterable - C", s)) { lists_0.add(new Student(s + "11", "20", "1101")); lists_0.add(new Student(s + "11", "20", "1102")); } else if (TextUtils.equals("FlatMapIterable - D", s)) { lists_0.add(new Student(s + "11", "20", "1101")); lists_0.add(new Student(s + "12", "20", "1102")); lists_0.add(new Student(s + "13", "20", "1103")); lists_0.add(new Student(s + "13", "20", "1104")); } return lists_0; }
switchMap操作符的流程图如下:
Observable.just(1,2,3,4,5) .scan((sum,item) -> sum + item) .subscribe(new Subscriber<Integer>() { @Override public void onCompleted() { Log.d("RXJAVA", "Sequence completed."); } @Override public void onError(Throwable e) { Log.e("RXJAVA", "Something went south!"); } @Override public void onNext(Integer item) { Log.d("RXJAVA", "item is: " + item); } });
goupBy
goupBy操作符用于分组元素,将源Observable变换成一个发射Observables的新的Observable(分组后的)。它们中的每一个新的Observable都发射一组指定的数据。goupBy操作符的流程图如下:
Observable<GroupedObservable<String,Student>> groupedItems = Observable.from(mLists) .groupBy(new Func1<Student,String>(){ @Override public String call(Student student){ return student.getAge(); } }); Observable.concat(groupedItems) .subscribe(new Observer<Student>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Student student) { mAdaStu.addData(student); } });
Observable.from(mLists) .buffer(2) .subscribe(new Action1<List<Student>>() { @Override public void call(List<Student> list) { cout++; Log.i("123", "cout = " + cout); Log.i("123", list.toString()); mAdaStu.addDatdLists(list); } }); log打印 cout = 1 [Student(name=A11, age=26, no=1101), Student(name=A12, age=21, no=1102)] cout = 2 [Student(name=A13, age=20, no=1103), Student(name=B11, age=22, no=1201)] cout = 3 [Student(name=B12, age=30, no=1202), Student(name=B13, age=20, no=1203)] cout = 4 [Student(name=S10, age=28, no=1301), Student(name=E10, age=30, no=1401)] cout = 5 [Student(name=F10, age=26, no=1501)]
查看上方的Log,可以看出将源Observable量量分组,变换为一个新的Observable,新的Observable每次发射一组列表值。
Observable.from(mLists) .buffer(2,3) .subscribe(new Action1<List<Student>>() { @Override public void call(List<Student> list) { cout++; Log.i("123", "cout = " + cout); Log.i("123", list.toString()); mAdaStu.addDatdLists(list); } }); //Log打印 cout = 1 [Student(name=A11, age=26, no=1101), Student(name=A12, age=21, no=1102)] cout = 2 [Student(name=B11, age=22, no=1201), Student(name=B12, age=30, no=1202)] cout = 3 [Student(name=S10, age=28, no=1301), Student(name=E10, age=30, no=1401)]
Observable.from(mLists) .window(2) .subscribe(new Action1<Observable<Student>>() { @Override public void call(Observable<Student> studentObservable) { studentObservable.subscribe(new Action1<Student>() { @Override public void call(Student student) { mAdaStu.addData(student); } }); } }); //Log打印 I: cout = 1 I: Student(name=A11, age=26, no=1101) I: cout = 2 I: Student(name=A12, age=21, no=1102) I: cout = 3 I: Student(name=A13, age=20, no=1103) I: cout = 4 I: Student(name=B11, age=22, no=1201) I: cout = 5 I: Student(name=B12, age=30, no=1202) I: cout = 6 I: Student(name=B13, age=20, no=1203) I: cout = 7 I: Student(name=S10, age=28, no=1301) I: cout = 8 I: Student(name=E10, age=30, no=1401) I: cout = 9 I: Student(name=F10, age=26, no=1501)
Observable.from(mLists) .window(2,3) .subscribe(new Action1<Observable<Student>>() { @Override public void call(Observable<Student> studentObservable) { studentObservable.subscribe(new Action1<Student>() { @Override public void call(Student student) { mAdaStu.addData(student); } }); } }); I: cout = 1 I: Student(name=A11, age=26, no=1101) I: cout = 2 I: Student(name=A12, age=21, no=1102) I: cout = 3 I: Student(name=B11, age=22, no=1201) I: cout = 4 I: Student(name=B12, age=30, no=1202) I: cout = 5 I: Student(name=S10, age=28, no=1301) I: cout = 6 I: Student(name=E10, age=30, no=1401)
cast操作符是是map操作符的流程图如下:
标签:
原文地址:http://blog.csdn.net/io_field/article/details/51406910