//window操作符
private static void windowOperator() {
Observable.just(78,88,9,845,21,5,6,8)
.window(3)
.subscribe(new Consumer<Observable>() {
@Override
public void accept(Observable integerObservable) throws Exception {
System.out.println(“On next”);
integerObservable.subscribe(new Consumer() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(“accept”+integer);
}
});
}
});
//发送第N项数据
//elementAt 自发送第N项数据
//elementAtOperator();
private static void elementAtOperator() {
Observable.just(1,5,7,8,9,52,68)
.elementAt(8,78)
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(integer+"第N项数据");
}
});
//ignoreElements 不发送任何数据
//ignoreElemtents();
private static void ignoreElemtents() {
Observable.just(102,3,34,42,3)
.ignoreElements()
.subscribe(new Action() {
@Override
public void run() throws Exception {
System.out.println("空数据发送成功!");
}
});
}
//flatMapOpretor操作符
//flatMapOpretor();
private static void flatMapOpretor() {
User user = new User();
user.username = "张三";
user.addresses = new ArrayList<>();
User.Address address = new User.Address();
address.city = "北京";
address.street = "朝阳北路";
user.addresses.add(address);
//groupByOpretor操作符
//groupByOperator();
private 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()+integer);
}
});
}
}
});
}
//filter操作符
private static void filterOperator() {
Observable.just(1,3,5,6,3,64,3,2,53,5)
.filter(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
return integer!=-1;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(integer+"");
}
});
//distinct过滤掉重复的数据项
//distinctOperator();
private static void distinctOperator() {
Observable.just(2,3,4,2,3,4)
.distinct()
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(integer+"过滤");
}
});
//map操作符
mapOpretor();