1

I am learning RxJava for a few week, i have some java code like below

Code:

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);
            }
        }
    }
}

How can i translate it to RxJava code?

I trying to use flatMapIterable with flatMap but still can not reached it.

1 Answer 1

4

There you go

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

You can see more examples to lear Rx here https://github.com/politrons/reactive

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your replay.
happy to help. Welcome to the reactive world!
Remember to mark the question as complete if resolve your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.