Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start with RxJava, loop

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.

like image 726
FinalDestination Avatar asked Mar 12 '23 02:03

FinalDestination


2 Answers

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);
like image 50
Tassos Bassoukos Avatar answered Mar 21 '23 03:03

Tassos Bassoukos


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
        }
    });
like image 41
Weava Avatar answered Mar 21 '23 03:03

Weava