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

后端 未结 9 616
悲哀的现实
悲哀的现实 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:42

    If your Observable emits a List, you can use these operators:

    • flatMapIterable (transform your list to an Observable of items)
    • map (transform your item to another item)
    • toList operators (transform a completed Observable to a Observable which emit a list of items from the completed Observable)

      Observable source = ...
      source.flatMapIterable(list -> list)
            .map(item -> new ResultsObject().convertFromSource(item))
            .toList()
            .subscribe(transformedList -> ...);
      

提交回复
热议问题