How to transform a nested list of double values into a Java class using RxJava?

眉间皱痕 提交于 2020-01-11 12:23:14

问题


In my Android client I receive this JSON data from a backend:

[
    [
        1427378400000,
        553
    ],
    [
        1427382000000,
        553
    ]
]

Here is the routine which actually loads the data. I am using RxAndroid and Retrofit here.

private void getProductLevels() {
    Observable<List<List<Double>>> responseObservable =
        mProductService.readProductLevels();
    AppObservable.bindFragment(this, responseObservable)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        // TODO: Transform List<List<Double>> into List<ProductLevel>
        .subscribe(new Subscriber<List<List<Double>>>() {
            @Override
            public void onCompleted() {}

            @Override
            public void onError(Throwable e) {}

            @Override
            public void onNext(List<List<Double>> response) {}
        });

}

How can I map the inner List<Double> to a specific Java class such as ProductLevel using RxJava operators?

public class ProductLevel {

    public Double mTimeStamp;
    public Double mLevel;

    public ProductLevel(Double timeStamp, Double level) {
        mTimeStamp = timeStamp;
        mLevel = level;
    }

}

Finally, I would like to receive this: List<ProductLevel>.


回答1:


According to your Data, you receive a list of pair (timestamp, level). This pair is represented by a list which contains only 2 values.

So you want to emit each pair, and transform each pair into a ProductLevel.

To do this, you'll have to flatMap your list of pair to emit each pair. Then to map each pair into a ProductLevel. Finally, just build a list with all emited items.

(java8 style)

AppObservable.bindFragment(this, responseObservable)
             .subscribeOn(Schedulers.io())
             .observeOn(AndroidSchedulers.mainThread())
             .flatMapIterable(listOfList -> listOfList) // or flatMap(l -> Observable.from(l))
             .map(pair -> new ProductLevel(pair.get(0),pair.get(1))) // build ProductLevel for each pair
             .toList() // build a list with all ProductLevel
             .subscribe(listOfProductLevel -> /** ... **/);



回答2:


List<ProductLevel> productLevels = new ArrayList<>();
for(List<Double> p: list) {
    productLevels.add(new ProductLevel(p.get(0),p.get(1)));
}

I think it should work.




回答3:


If you change readProductLevels to emit each list item one at a time, you can then react to them one at a time:

In mProductService:

private Observable<List<Double>> readProductLevels() {
        return Observable.create(
                new Observable.OnSubscribe<List<Double>>() {
                    @Override
                    public void call(Subscriber<? super List<Double>> subscriber) {
                        List<List<Double>> items = new ArrayList<List<Double>>();
                        List<Double> list1 = new ArrayList<Double>() {{
                            add(1D);
                            add(2D);
                        }};
                        items.add(list1);
                        List<Double> list2 = new ArrayList<Double>() {{
                            add(10D);
                            add(12D);
                        }};
                        items.add(list2);
                        List<Double> list3 = new ArrayList<Double>() {{
                            add(21D);
                            add(22D);
                        }};
                        items.add(list3);

                       // Imagine the list creation above is your Json parsing


                        for (List<Double> list : items) {
                            subscriber.onNext(list);
                        }

                        subscriber.onCompleted();
                    }
                });
    }

Allows you to subscribe to each item in your super list:

private void getProductLevels() {
        Observable<List<Double>> responseObservable = readProductLevels();
        AppObservable.bindFragment(this, responseObservable)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                        // TODO: Transform List<List<Double>> into List<ProductLevel>
                .subscribe(
                        new Subscriber<List<Double>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onNext(List<Double> response) {
                                new ProductLevel(response.get(0), response.get(1));
                            }
                        });

    }


来源:https://stackoverflow.com/questions/29948117/how-to-transform-a-nested-list-of-double-values-into-a-java-class-using-rxjava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!