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

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

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

        List li = new ArrayList<>();
        Observable.just(li).map( l -> {
            List lr = new ArrayList();
            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())
    

提交回复
热议问题