Observable.just("Hello", "RxJava", "Nice to meet you")
String[] strings = {"Hello", "RxJava", "Nice to meet you"};
Observable.from(strings)
.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
private String[] strings1 = {"Hello", "World"};
private String[] strings2 = {"Hello", "RxJava"};
private void test() {
Observable observable = Observable.defer(new Func0>() {
@Override
public Observable call() {
return Observable.from(strings1);
}
});
strings1 = strings2; //订阅前把strings给改了
observable.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
}
我们发现数据结果变成这样了
onNext--> Hello
onNext--> RxJava
onComplete
由此可以证明defer操作符起到的不过是一个“预创建”的作用,真正创建是发生在订阅的时候
Observable.empty()
.subscribe(new Action1
onComplete
Observable.never()
.subscribe(new Action1
······
Observable.error(new RuntimeException("fuck!"))
.subscribe(new Action1
onError--> fuck!
Observable.range(3, 8)
.subscribe(new Action1
onNext--> 3
onNext--> 4
onNext--> 5
onNext--> 6
onNext--> 7
onNext--> 8
onNext--> 9
onNext--> 10
onComplete
Observable.interval(1, TimeUnit.SECONDS)
.subscribe(new Action1
onNext--> 0
onNext--> 1
onNext--> 2
onNext--> 3
onNext--> 4
onNext--> 5
onNext--> 6
...
Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.buffer(3)
.subscribe(new Action1
onNext--> [1, 2, 3]
onNext--> [4, 5, 6]
onNext--> [7, 8, 9]
onNext--> [10]
onComplete
Observable.just("Hello", "RxJava", "Nice to meet you")
.map(new Func1() { //泛型第一个类型是原数据类型,第二个类型是想要变换的数据类型
@Override
public Integer call(String s) {
// 这是转换成了Student类型
// Student student = new Student();
// student.setName(s);
// return student;
return s.hashCode(); //将数据转换为了int(取得其hashCode值)
}
})
.subscribe(new Action1() {
@Override
public void call(Integer o) {
System.out.println("onNext--> " + o);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
onNext--> 69609650
onNext--> -1834252888
onNext--> -1230747480
onComplete
Observable.just("Hello", "RxJava", "Nice to meet you")
.flatMap(new Func1>() {
@Override
public Observable call(String s) {
return Observable.just(s.hashCode());
}
})
.subscribe(new Action1() {
@Override
public void call(Integer o) {
System.out.println("onNext--> " + o);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
}
onNext--> 69609650
onNext--> -1834252888
onNext--> -1230747480
onComplete
这里虽然结果和map是相同的,但是过程却是不同的。flatMap是先将原来的三个字符串("Hello","RxJava","Nice to meet you")依次取其hashCode,在利用Observable.just将转换之后的int类型的值在发射出来。map只是单穿的转换了数据类型,而flapMap是转换成了新的Observable了,这在开发过程中遇到嵌套网络请求的时候十分方便。
window
看到网上其他人说他的总用类似于buffer,不过我倒是认为他更像flatMap,区别的是flapMap在转换之后形成新的Observable会再将新的数据发射出来,不过window就仅仅只转换成了发射新的数据类型的Observable,有点像是flatMap在干活时半途而废的意思。
Observable.just("Hello", "RxJava", "Nice to meet you")
.filter(new Func1() {
@Override
public Boolean call(String s) {
//这里的显示条件是s的长度大于5,而Hello的长度刚好是5
//所以不能满足条件
return s.length() > 5;
}
})
.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
onNext--> RxJava
onNext--> Nice to meet you
onComplete
Observable.just("Hello", "RxJava", "Nice to meet you")
.take(2)
//.taktLast(2)
.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
}
onNext--> Hello
onNext--> RxJava
onComplete
Observable.just("Hello", "RxJava", "Nice to meet you")
.skip(2)
//.skipLast(2)
.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
}
onNext--> Nice to meet you
onComplete
Observable.just("Hello", "RxJava", "Nice to meet you")
.elementAtOrDefault(1, "Great")
.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
onNext--> RxJava
onComplete
Observable.just("Hello", "Hello", "Hello", "RxJava", "Nice to meet you")
.distinct()
.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
onNext--> Hello
onNext--> RxJava
onNext--> Nice to meet you
onComplete
Observable.just("Hello", "RxJava", "Nice to meet you")
.startWith("One", "Two", "Three")
.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
onNext--> One
onNext--> Two
onNext--> Three
onNext--> Hello
onNext--> RxJava
onNext--> Nice to meet you
onComplete
Observable.merge(Observable.just(1, 2, 3), Observable.just(4, 5),
Observable.just(6, 7), Observable.just(8, 9, 10))
.subscribe(new Action1() {
@Override
public void call(Integer s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
onNext--> 1
onNext--> 2
onNext--> 3
onNext--> 4
onNext--> 5
onNext--> 6
onNext--> 7
onNext--> 8
onNext--> 9
onNext--> 10
onComplete
Observable.zip(Observable.just(1, 8, 7), Observable.just(2, 5),
Observable.just(3, 6), Observable.just(4, 9, 0), new Func4() {
@Override
public Integer call(Integer integer, Integer integer2, Integer integer3, Integer integer4) {
return integer < integer2 ? integer3 : integer4;
}
})
.subscribe(new Action1() {
@Override
public void call(Integer s) {
System.out.println("onNext--> " + s);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
System.out.println("onError--> " + throwable.getMessage());
}
}, new Action0() {
@Override
public void call() {
System.out.println("onComplete");
}
});
onNext--> 3
onNext--> 9
onComplete
通过观察以上例子可以发现我们的发射规则是如果发射的第一个数据小于第二个数则发射第三个数据,否则发射第四个数据(我们来验证一下,1确实是小于2的,随意发射的是3;8并不小于5,所以发射的是9,又因为四个发射箱,最少的之后两项,所以最后只发射了两项数据)
转自:chuwe1
|--------------------------------------------------------------------
* 这里有精选的技术分享,也有程序猿的日常
* 面经,转型,成长,致富,欢迎一起参与讨论
* https://blog.csdn.net/itchosen
---------------------------------------------------------------------|
如果你对老罗的话题或者IT技术感兴趣,请添加微信入讨论群:chaintips