1 zip() 是合并多个Observable 发射出来的数据,并且根据FUNC2 函数变换产生一个新的值
这里会把 1A 2B ,如果一个序列介绍了,就不会产生新的值了;
Listlist = new ArrayList(); for (int i =0;i<=20;i++){ list.add("arg="+i+"====>>"); } Observable observable = Observable.interval(1, TimeUnit.SECONDS); Observable observable1 = Observable.from(list); Observable.zip(observable, observable1, new Func2 () { @Override public Object call(Long aLong, String s) { return s+aLong; } }).subscribe(new Action1
结果为
01-18 15:25:33.042 1498-1694/sean.com.test I/===>>: arg=16====>>1601-18 15:25:34.042 1498-1694/sean.com.test I/===>>: arg=17====>>1701-18 15:25:35.042 1498-1694/sean.com.test I/===>>: arg=18====>>1801-18 15:25:36.041 1498-1694/sean.com.test I/===>>: arg=19====>>1901-18 15:25:37.042 1498-1694/sean.com.test I/===>>: arg=20====>>20
2 Marge() 是把多个Abservable里面的值合并成一个
最后的序列如上图所示
Listlist = new ArrayList(); for (int i =0;i<20;i++){ list.add(Long.valueOf(i)); } List list1 = new ArrayList(); for (int i =20;i<40;i++){ list1.add(Long.valueOf(i)); } Observable observable = Observable.from(list); Observable observable1 = Observable.from(list1); Observable.merge(observable, observable1).subscribe(new Action1
结果为
01-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 001-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 101-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 201-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 301-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 401-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 501-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 601-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 701-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 801-18 15:54:55.132 16954-16954/sean.com.test I/===>>: 901-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1001-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1101-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1201-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1301-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1401-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1501-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1601-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1701-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1801-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 1901-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 2001-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 2101-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 2201-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 2301-18 15:54:55.133 16954-16954/sean.com.test I/===>>: 2401-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 2501-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 2601-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 2701-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 2801-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 2901-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 3001-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 3101-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 3201-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 3301-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 3401-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 3501-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 3601-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 3701-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 3801-18 15:54:55.134 16954-16954/sean.com.test I/===>>: 39
3 Flatmap 和 map, Flatmap 返回一个新的Observable, 而map返回原来的Observerable
Listlist = new ArrayList(); for (int i =0;i<20;i++){ list.add(Long.valueOf(i)); } Observable observable = Observable.from(list); observable.flatMap(new Func1 >() { @Override public Observable call(Long aLong) { return Observable.just("hello"+aLong); } }).subscribe(new Action1 () { @Override public void call(String s) { Log.i("===>>",s); } }); }
4 Concat 链接多个Observable(不能用不定参数??)