0

I have a UserBean like :

class UserBean {
    public List<String> userNumber;
    public int userId;
    public String userName;
}
class UserBeanData {
    public String userNumber;
    public int userId;
    public String userName;
}

Now , i want to circular this bean list and get a piece of data list,like this : [{userId , userName , userNumber},{userId , userName , userNumber},...]. The way I use it now is:

    List<UserBean> userBeen = new ArrayList<>();
    final List<UserBeanData> userBeanDatas = new ArrayList<>();
    Observable.from(userBeen)
            .flatMap(new Func1<UserBean, Observable<UserBeanData>>() {
                @Override
                public Observable<UserBeanData> call(UserBean userBean) {
                    return Observable.combineLatest(Observable.just(userBean), Observable.from(userBean.userNumber)
                            , new Func2<UserBean, String, UserBeanData>() {
                                @Override
                                public UserBeanData call(UserBean userBean, String s) {
                                    UserBeanData tData = new UserBeanData();
                                    tData.userId = userBean.userId;
                                    tData.userName = userBean.userName;
                                    tData.userNumber = s;
                                    return tData;
                                }
                            });
                }
            })
            .subscribe(new Action1<UserBeanData>() {
                @Override
                public void call(UserBeanData userBeanData) {
                    userBeanDatas.add(userBeanData);
                }
            });

Are there any other better ways to achieve this?

1 Answer 1

1

flatMap can take a second argument as result selector (which help to combine results)

flatMapIterable transform a list to an Observable with items from the list. As flatMap, flatMapIterable can take a result selector

final List<UserBeanData> userBeanDatas = Observable.fromIterable(userBeen)
        // emit all numbers and create a UserBeanData for each, using the bean as source 
        .flatMapIterable(bean -> bean.userNumber, (source, number) -> new UserBeanData(number, source.userId, source.userName))
        // transform the observable as a list
        .toList()
        // get the list
        .blockingGet()

Please note that this code targets RxJava 2 but should work with RxJava 1 (change Observable.fromIterable to Observable.from and blockingGet to toBlocking().first()

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

3 Comments

Thank you very much . Through your method I completed this demand
I want to filter the number of 15 userbean,so tried that: Observable.from(userBeen) .flatMapIterable(userBean -> userBean.userNumber,(source, number) -> { // if number is "15" return source else return null;}) .filter(source -> {// if source != null return true,else return false}) .toList() .toBolcking() .first(); This is the worst way is it?
don't change flatMapIterable: just filter if source.number != 15. You'll alocate an object that may be filtered just behind, but I think that the overhead is minimal.

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.