RxJava带有条件的多循环

时间:2017-06-07 04:43:00

标签: java rx-java

我正在学习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代码?

我尝试将flatMapIterableflatMap一起使用,但仍然无法达到它。

1 个答案:

答案 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();
}

您可以在https://github.com/politrons/reactive

了解更多有关学习Rx的示例