RxJava2 / RxAndroid2操作符distinct去重
distinct去除重复的数据,例如:
package zhangphil.app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
private Integer[] test_data = {
1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9
}; //故意设置若干重复的数据,等待去重复数据
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
test();
}
private void test() {
DisposableObserver disposableObserver = new DisposableObserver<Integer>() {
@Override
public void onNext(Integer i) {
Log.d(TAG, "#####开始#####");
Log.d("数据", String.valueOf(i));
Log.d(TAG, "#####结束#####");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
Observable<Integer> observable = Observable.fromArray(test_data);
mCompositeDisposable.add(observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.distinct()
.subscribeWith(disposableObserver));
}
@Override
protected void onDestroy() {
super.onDestroy();
// 如果退出程序,就清除后台任务
mCompositeDisposable.clear();
}
}
代码输出:
05-18 10:19:32.361 2346-2346/zhangphil.app D/MainActivity: #####开始#####
05-18 10:19:32.361 2346-2346/zhangphil.app D/数据: 1
05-18 10:19:32.361 2346-2346/zhangphil.app D/MainActivity: #####结束#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####开始#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/数据: 2
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####结束#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####开始#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/数据: 3
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####结束#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####开始#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/数据: 4
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####结束#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####开始#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/数据: 5
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####结束#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####开始#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/数据: 6
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####结束#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####开始#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/数据: 7
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####结束#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####开始#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/数据: 8
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####结束#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####开始#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/数据: 9
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: #####结束#####
05-18 10:19:32.362 2346-2346/zhangphil.app D/MainActivity: onComplete