组合就是多输入,单输出,大家看具体的例子
package com.example.acer.rxjavatest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import rx.Observable;
import rx.Observer;
//需求,打印一组用户名
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] args1=new String[]{"张欣1","张欣2","张欣3","张欣4"};
String[] args2=new String[]{"春晓1","春晓2","春晓3","春晓4"};
//相同的数组可以进行合并
Observable<String> mergeObservable=Observable.merge(Observable.from(args1),Observable.from(args2));
mergeObservable.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
Log.i("zhang_xin",s);
}
});
}
}
看下运行效果
注意:在上面的程序中我们还可以把merge换成mergeDelayError();
Observable<String> mergeObservable=Observable.mergeDelayError(Observable.from(args1),Observable.from(args2));
mergeDelayError()方法:在Observable中,一旦某一个时间抛出异常,后面的序列将会终止,如果我们希望在序列出错的时候不影响后面的序列,那么可以使用mergeDelayErroe()方法!
运行结果同merge同,这里就不展示了!
merge是把两个数组 合并为一个数组,新的数组长度是原来两个数组长度之和;
zip合并后的数组长度是两个数组长度最小的那一个数组的长度;而且我们可以自己定义合并的方式,即合并的算法自己决定:看代码
package com.example.acer.rxjavatest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import rx.Observable;
import rx.Observer;
import rx.functions.Func2;
//需求,打印一组用户名
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//数组长度5
String[] args1=new String[]{"张欣1","张欣2","张欣3","张欣4","zhangxin5"};
//数组长度4
String[] args2=new String[]{"春晓1","春晓2","春晓3","春晓4"};
//相同的数组可以进行合并
Observable<String> mergeObservable=Observable.zip(Observable.from(args1), Observable.from(args2), new Func2<String, String, String>() {
@Override
public String call(String s, String s2) {
//这里使我们合并数组的算法
return s+"------"+s2;
}
});
mergeObservable.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
Log.i("zhang_xin",s);
}
});
}
}
看下join方法,数组长度是两个数组的乘积;
package com.example.acer.rxjavatest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Observer;
import rx.functions.Func1;
import rx.functions.Func2;
//需求,打印一组用户名
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] args1 = new String[]{"张欣1", "张欣2", "张欣3", "张欣4", "zhangxin5"};
String[] args2 = new String[]{"春晓1", "春晓2", "春晓3", "春晓4"};
Integer[] args3 = new Integer[]{1, 2, 3, 4, 5, 6};
Observable<String> o1 = Observable.from(args1);
Observable<String> o2 = Observable.from(args2);
//相同的数组可以进行合并
Observable<String> joinObservable = o2.join(o1, new Func1<String, Observable<Long>>() {
@Override
public Observable<Long> call(String s) {
return Observable.timer(2, TimeUnit.SECONDS);
}
}, new Func1<String, Observable<Long>>() {
@Override
public Observable<Long> call(String s) {
return Observable.timer(2, TimeUnit.SECONDS);
}
}, new Func2<String, String, String>() {
@Override
public String call(String s, String s2) {
return s + "-&--" + s2;
}
});
joinObservable.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
Log.i("zhang_xin", s);
}
});
}
}