我正在学习RxJava几周,我有一些类似下面的Java代码
代码:
String[] strings1 = new String[]{"a", "b", "c", "d", "e"};
Integer[] integers = {1, 2, 3, 4, 5};
String[] strings2 = new String[]{"f", "g", "h", "i"};
for (String str : strings1) {
for (Integer integer : integers) {
System.out.println(str + ":" + integer);
if(integer == 4){
for (String str2 : strings2) {
System.out.println(str2 + ":" + integer);
}
}
}
}
如何将其翻译为RxJava代码?
我尝试将flatMapIterable
与flatMap
一起使用,但仍然无法达到它。
答案 0 :(得分:4)
你去了
@Test
public void thirdDeepLevel() {
Observable.from(Arrays.asList("a", "b", "c", "d", "e"))
.flatMap(letter -> Observable.from(Arrays.asList(1, 2, 3, 4, 5))
.map(number -> {
System.out.println(letter + ":" + number);
return number;
})
.filter(number -> number == 4)
.flatMap(number -> Observable.from(Arrays.asList("f", "g", "h", "i"))
.map(leter2 -> {
System.out.println(letter + ":" + number);
return leter2;
})))
.subscribe();
}
了解更多有关学习Rx的示例