RxJava: How to convert List of objects to List of another objects

后端 未结 9 470
悲哀的现实
悲哀的现实 2021-01-31 07:30

I have the List of SourceObjects and I need to convert it to the List of ResultObjects.

I can fetch one object to another using method of ResultObject:

c         


        
相关标签:
9条回答
  • 2021-01-31 07:51

    The Observable.from() factory method allows you to convert a collection of objects into an Observable stream. Once you have a stream you can use the map operator to transform each emitted item. Finally, you will have to subscribe to the resulting Observable in order to use the transformed items:

    // Assuming List<SourceObject> srcObjects
    Observable<ResultsObject> resultsObjectObservable = Observable.from(srcObjects).map(new Func1<SourceObject, ResultsObject>() {
        @Override
        public ResultsObject call(SourceObject srcObj) {
            return new ResultsObject().convertFromSource(srcObj);
        }
    });
    
    resultsObjectObservable.subscribe(new Action1<ResultsObject>() { // at this point is where the transformation will start
        @Override
        public void call(ResultsObject resultsObject) { // this method will be called after each item has been transformed
            // use each transformed item
        }
    });
    

    The abbreviated version if you use lambdas would look like this:

    Observable.from(srcObjects)
      .map(srcObj -> new ResultsObject().convertFromSource(srcObj))
      .subscribe(resultsObject -> ...);
    
    0 讨论(0)
  • 2021-01-31 07:51

    As an extension to Noel great answer. Let's say transformation also depends on some server data that might change during subscription. In that case use flatMap + scan.

    As a result when id list changes, transformations will restart anew. And when server data changes related to a specific id, single item will be retransformed as well.

    fun getFarmsWithGroves(): Observable<List<FarmWithGroves>> {
    
            return subscribeForIds() //may change during subscription
                    .switchMap { idList: Set<String> ->
    
                        Observable.fromIterable(idList)
                                .flatMap { id: String -> transformId(id) } //may change during subscription
                                .scan(emptyList<FarmWithGroves>()) { collector: List<FarmWithGroves>, candidate: FarmWithGroves ->
                                    updateList(collector, candidate)
                                }
                    }
        }
    
    0 讨论(0)
  • 2021-01-31 07:54

    You can use map operator. For example if you have lists of integers and you want to convert to lists of doubles:

        List<Integer> li = new ArrayList<>();
        Observable.just(li).map( l -> {
            List<Double> lr = new ArrayList<Double>();
            for(Integer e:l) {
                lr.add(e.doubleValue());
            }
            return lr;
        });
    

    But all it's a lot more natural if you can control the observable and change it to observe single elements instead of collections. Same code that converts single integer elements into double ones:

    Observable.just(1,2,3).map( elem -> elem.doubleValue())
    
    0 讨论(0)
提交回复
热议问题