2

The need is a little strange. I want to convert a list to a new list. For example, convert List<Android> to List<AndroidWrapper>.

class AndroidWrapper has two fields.

public class AndroidWrapper {
    private Android mAndroid;
    private String mAvatarUrl;

    public AndroidWrapper(Android android, String avatarUrl) {
        this.mAndroid = android;
        this.mAvatarUrl = avatarUrl;
    }
}

The field mAvatar is related to mAndroid field and it can be fetched from remote server.

Observable<String> fetchAvatarUrl(Android android)

So how can do this using RxJava2?

1 Answer 1

2

Turn your list into an Observable, flatMap the dependent call onto it, create the final object type and collect it into a list:

Observable.fromIterable(androids)
.flatMap(android ->
     fetchAvatarUrl(android)
     // go into a backgroud thread
     .subscribeOn(Schedulers.io())
     // combine the result with the original value
     .map(url -> new AndroidWrapper(android, url))
     // limit the maximum concurrent fetch calls
     , 1
)
.toList();
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks very much. Since android has not supported Java-8 yet, can you append a non-lambda version.
Your Android Studio should be able to convert this to that format, plus you can now have lambda support: developer.android.com/studio/write/java8-support.html
Thanks for your reply. But I am new to lambda, so I do need non-lambda version.
Paste in the code sample above, position on the ->, hit ALT+ENTER and pick "Replace lambda with anonymous class". Repeat for the other ->.
method toList return Single<List<T>>
|

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.