4

Can someone tell me how can I change default code of java like that:

   private ArrayList<RateItem> generateRates(List<Rates> ratesList){
    rateItemArrayList=new ArrayList<RateItem>();

    for(Rates rates : ratesList)
        rateItemArrayList.add(new RateItem(rates.getName(),rates.getCode(),rates.getValue()));

    return rateItemArrayList;
}

to code in RxJava? Im just starting and i cant handle the basics yet : (

EDIT

I have no idea why this loop iterating more times than my List from response has. Then my new Arraylist has 1300 items instead of 30. What is wrong?

 private Observable<ArrayList<RateItem>> generateRates(List<Rates> rates){
    rateItemArrayList = new ArrayList<RateItem>();

    Observable<ArrayList<RateItem>> observable = Observable.from(rates)
            .map(new Func1<Rates, ArrayList<RateItem>>() {
                @Override
                public ArrayList<RateItem> call(Rates rat) {
                    for(Rates ratt : rates)
                        rateItemArrayList.add(new RateItem(ratt.getName(),ratt.getCode(),ratt.getValue()));
                    return rateItemArrayList;
                }
            });

    return observable;
}

@Weava I dont know but I cant make your code, AS automatically generates code like above.

1
  • something like Observable.from(ratesList).map(rates -> new RateItem(rates.getName(),rates.getCode(),rates.getValue())).subscribe(<your callback here>) Commented Sep 20, 2016 at 15:16

5 Answers 5

6
private Observable<RateItem> generateRates(List<Rates> ratesList){
   return Observable
     .from(ratesList)
     .map(rates -> new RateItem(rates.getName(),rates.getCode(),rates.getValue());
}

Actually, I would even skip having List<anything> for actual usage (not inter-module APIs, but intra-module coordination), in which case the whole method becomes a simple call:

.map(rates -> new RateItem(rates.getName(),rates.getCode(),rates.getValue());

In fact, I'd say just refactor out the constructor into a separate static method in RateItem, preferably a constructor, then it's just

.map(RateItem::new);
Sign up to request clarification or add additional context in comments.

Comments

1

In RxJava you might do something like this, using the from and map functions.

private Observable<List<RateItem>> generateRates(List<Rates> ratesList) {
    Observable<List<RateItem>> myObservable = Observable.from(ratesList)
            .map(new Func1<List<Rates>, List<RateItem>>() {

                @Override
                public List<RateItem> call(List<Rates> rateList) {
                    List<RateItem> items = new ArrayList<>();

                    for(Rates rates : ratesList) {
                        items.add(new RateItem(rates.getName(), rates.getCode(), rates.getValue()));
                    }
                    return items;
                }
            });

      return myObservable;
}

In this function, I am adding the ratesList object to be emitted by the observable. Then, as the list is being emitted, I map it to the List<RateItem> objects. Then I just return the Observable for subscription wherever I call this method.

If you are using RetroLamda, you could definitely make this look a little nicer (see @njzk2's comment) but I wanted to show what was actually going on.

As an example of how to subscribe to your new Observalbe, it would looks something like this:

    generateRates(new ArrayList<Rates>()).subscribe(new Func1<List<RateItem>>(){
        @Override
        public void call(List<RateItem> rateItems) {
            // Do whatever it is you need to do with your list
        }
    });

2 Comments

You can directly iterate through the given list, no need to map the entire List again... See @Tassos answer
@Khuzy Check out Retrolamda if you haven't already. It's great, and allows you to do things like Tassos answer.
0

I wrote this code, I think this can work, but I didn't test it. Hope this can help you to start with RxJava:

List<Rates> ratesList;
List rateItemArrayList = new ArrayList<RateItem>();
Observable.just(ratesList)
        .flatMap(new Func1<List<Rates>, Observable<RateItem>>() {
            @Override
            public Observable<RateItem> call(List<Rates> rate) {
                return Observable.just(new RateItem(rate.getName(), rate.getCode(), rate.getValue()));
            }
        }).subscribe(new Action1<RateItem>() {
    @Override
    public void call(RateItem rateItem) {
        rateItemArrayList.add(rateItem);
    }
});

1 Comment

I would advise against using flat maps as you are not trying to map an item into multiple observables, then emit as one He is just trying to map the objects from a List<Rate> to List<RateItem> and emit as one item through the observable. Just a regular map function should do in this situation.
0

Another way, I´ve create this unit test

class RateItem {
    String name;
    String code;

    public RateItem(String name, String code) {
        this.name = name;
        this.code = code;
    }
}

class Rate {
    String name;
    String code;

    public Rate(String name, String code) {
        this.name = name;
        this.code = code;
    }
}

@Test
public void migrateToObservable() {
    generateRates(Arrays.asList(new Rate("a", "1"), new Rate("b", "2")));
}

private List<RateItem> generateRates(List<Rate> ratesList) {
    return  Observable.from(ratesList)
            .map(rate-> new RateItem(rate.name, rate.code))
            .toList().toBlocking().first();
}

If you want to see some RXJava examples take a look here https://github.com/politrons/reactive

Comments

0

Another way without a lot of changes is using Observable.defer(generateRates()); this will transform your old code into Observable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.