I'm dipping my toes in to RXJava for the first time. The function below works just fine but I can't help thinking it can be improved. For instance I'm sure that I'm unnecessarily creating a new List<Dish> sortedDishes when I could be performing operations on the exiting List<Dish> dishes that is passed in, I just don't know how yet.
public void sortDishes(int categoryId, List<Dish> dishes) {
final List<Dish> sortedDishes = new ArrayList<>();
final Integer[] subCategories = dbInteractor.getSubCategories(categoryId);
final ArrayList<Integer> cats = new ArrayList<>();
cats.add(categoryId);
if (subCategories != null) {
cats.addAll(Arrays.asList(subCategories));
}
Observable.from(dishes)
.filter(new Func1<Dish, Boolean>() {
@Override
public Boolean call(Dish dish) {
Integer[] categories = categoryHelper.getCategorys(dish.getCategories());
for (Integer category : categories) {
if (cats.contains(category)) {
return true;
}
}
return false;
}
})
.subscribe(new Subscriber<Dish>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Dish dish) {
sortedDishes.add(dish);
}
});
}