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

后端 未结 9 469
悲哀的现实
悲哀的现实 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<SourceObjet> source = ...
      source.flatMapIterable(list -> list)
            .map(item -> new ResultsObject().convertFromSource(item))
            .toList()
            .subscribe(transformedList -> ...);
      
    0 讨论(0)
  • 2021-01-31 07:43

    Don't break the chain, just like this.

    Observable.from(Arrays.asList(new String[] {"1", "2", "3", }))
    .map(s -> Integer.valueOf(s))
    .reduce(new ArrayList<Integer>, (list, s) -> {
        list.add(s);
        return list;
    })
    .subscribe(i -> {
        // Do some thing with 'i', it's a list of Integer.
    });
    
    0 讨论(0)
  • 2021-01-31 07:44

    If you want to maintain the Lists emitted by the source Observable but convert the contents, i.e. Observable<List<SourceObject>> to Observable<List<ResultsObject>>, you can do something like this:

    Observable<List<SourceObject>> source = ...
    source.flatMap(list ->
            Observable.fromIterable(list)
                .map(item -> new ResultsObject().convertFromSource(item))
                .toList()
                .toObservable() // Required for RxJava 2.x
        )
        .subscribe(resultsList -> ...);
    

    This ensures a couple of things:

    • The number of Lists emitted by the Observable is maintained. i.e. if the source emits 3 lists, there will be 3 transformed lists on the other end
    • Using Observable.fromIterable() will ensure the inner Observable terminates so that toList() can be used
    0 讨论(0)
  • 2021-01-31 07:47

    Using toList() waits for the source observable to complete, so if you do that on an infinite observable (like one bound to UI events and Database calls), you will never get anything in your subscribe().

    The solution to that is to Use FlatMapSingle or SwitchMapSingle.

    Observable<List<Note>> observable =   noteDao.getAllNotes().flatMapSingle(list -> Observable.fromIterable(list).toList());
    

    Now,everytime your list is updated, you can make that event behave as an observable.

    0 讨论(0)
  • 2021-01-31 07:48

    Non blocking conversion using nested map function

    val ints: Observable<List<Int>> = Observable.fromArray(listOf(1, 2, 3))
    
    val strings: Observable<List<String>> = ints.map { list -> list.map { it.toString() } }
    
    0 讨论(0)
  • 2021-01-31 07:48

    If what you need is simply List<A> to List<B> without manipulating result of List<B>.

    The cleanest version is:

    List<A> a = ... // ["1", "2", ..]
    List<B> b = Observable.from(a)
                          .map(a -> new B(a))
                          .toList()
                          .toBlocking()
                          .single();
    
    0 讨论(0)
提交回复
热议问题