RxJava 是一个 基于事件流、实现异步操作的库
1、作用
类似于 Android中的 AsyncTask 、Handler作用
2、特点
由于 RxJava的使用方式是:基于事件流的链式调用,所以使得 RxJava:
逻辑简洁
实现优雅
使用简单
更重要的是,随着程序逻辑的复杂性提高,它依然能够保持简洁 & 优雅
3、原理介绍
Rxjava原理 基于 一种扩展的观察者模式
即RxJava原理可总结为:被观察者 (Observable) 通过 订阅(Subscribe) 按顺序发送事件 给观察者 (Observer), 观察者(Observer) 按顺序接收事件 & 作出对应的响应动作。具体如下图:
4、基本使用
1、分步骤实现:该方法主要为了深入说明Rxjava的原理 & 使用,主要用于演示说明
2、基于事件流的链式调用:主要用于实际使用
代码实现
package com.example.rxjava;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.observables.GroupedObservable;
import java.util.ArrayList;
public class Rxjava {
public static void main(String[] args) {
/* new Observable<String>(){
@Override
protected void subscribeActual(Observer<? super String> observer) {
observer.onNext("玻璃瓶");
observer.onNext("啤酒瓶");
observer.onComplete();
}
}.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
System.out.println("接收到了:" + s);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
System.out.println("接收数据完毕");
}
});*/
// mapOpretor();
// flatMapOpretor();
// groupByOperator();
// filterOperator();
// distinctOperator();
// ignoreElements();
// elementAtOperator();
windowOperator();
}
//map操作符
public static void mapOpretor(){
Observable.just("HELLO")
.map(new Function<String, String>() {
@Override
public String apply(String s) throws Exception {
return s.toLowerCase();
}
})
.map(new Function<String, String>() {
@Override
public String apply(String s) throws Exception {
return s + ",world";
}
})
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
System.out.println("接收到的数据是:" + s);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
//flatMap操作符
public static void flatMapOpretor() {
User user = new User();
user.userName = "zhangsan";
user.addresss = new ArrayList<>();
User.Address address1 = new User.Address();
// User.Address address1 = new User.Address();
address1.street = "上地九街";
address1.city = "北京";
user.addresss.add(address1);
User.Address address2 = new User.Address();
address2.street = "上地十街";
address2.city = "上海";
user.addresss.add(address2);
/*Observable.just(user)
.map(new Function<User, List<User.Address>>() {
@Override
public List<User.Address> apply(User user) throws Exception {
return user.addresss;
}
}).subscribe(new Consumer<List<User.Address>>() {
@Override
public void accept(List<User.Address> addresses) throws Exception {
for (int i = 0; i < addresses.size(); i++) {
System.out.println("地址是:" + addresses.get(i).street);
}
}
});*/
Observable.just(user)
.flatMap(new Function<User, ObservableSource<User.Address>>() {
@Override
public ObservableSource<User.Address> apply(User user) throws Exception {
return Observable.fromIterable(user.addresss);
}
})
.subscribe(new Consumer<User.Address>() {
@Override
public void accept(User.Address address) throws Exception {
System.out.println("地址是:" + address.street);
}
});
}
//groupBy操作符
public static void groupByOperator(){
Observable
.range(1,8)
.groupBy(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
return (integer % 2 == 0)? "偶数组":"奇数组";
}
}).subscribe(new Consumer<GroupedObservable<String, Integer>>() {
@Override
public void accept(final GroupedObservable<String, Integer> stringIntegerGroupedObservable) throws Exception {
if(stringIntegerGroupedObservable.getKey().equals("奇数组")){
stringIntegerGroupedObservable.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(stringIntegerGroupedObservable.getKey() + "member:" + integer);
}
});
}/*else if(stringIntegerGroupedObservable.getKey().equals("偶偶组")){
stringIntegerGroupedObservable.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(stringIntegerGroupedObservable.getKey() + "member:" + integer);
}
});
}*/
System.out.println();
}
});
}
//filter操作符
public static void filterOperator(){
Observable.just(1,8,20,3,50,4,6,100)
.filter(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
return integer > 8;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println("next :" + integer);
}
});
}
//distinct过滤掉重复的数据项
public static void distinctOperator() {
Observable.just("A","A","B")
.distinct()
.subscribe(new Consumer<String>() {
@Override
public void accept(String integer) throws Exception {
System.out.println("next :" + integer);
}
});
}
//ignoreElements 不发射任何数据
public static void ignoreElements() {
Observable.just(1,2,3,4,5)
.ignoreElements()
.subscribe(new Action() {
@Override
public void run() throws Exception {
System.out.println("完成了任务");
}
});
}
//elementAt 只发射第n项数据
public static void elementAtOperator() {
Observable.just(1,2,3,4,5,6,7)
.elementAt(2)
// .elementAt(12,34)
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println("next:" + integer);
}
});
}
//window操作符
public static void windowOperator() {
Observable.range(1,10)
.window(2)
.subscribe(new Consumer<Observable<Integer>>() {
@Override
public void accept(Observable<Integer> integerObservable) throws Exception {
System.out.println("On next:" );
integerObservable.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println("accept:" + integer );
}
});
}
});
}
}