Given an Observable of a sequence of source objects, how can I map multiple output Objects out of each input object using rxjava? (one to many mapping)
I have a list of dishes representing the items that compose an order of a restaurant. I need to transform each Dish into one or more OrderLine. Each Dish map creates one OrderLine for its name+price, one OrderLine for each Topping and one OrderLine if it has a note.
INPUT
List dishes = {...}
OUTPUT
List orderLines = {...}
class Dish {
public String name;
public List toppings;
public String note;
public BigDecimal price;
}
class Topping {
public String name;
public BigDecimal price;
}
class OrderLine {
public String name;
public BigDecimal price;
}
Is there a way to do so using Functional Programming and/or Reactive Programming?
I do not want to use something imperative such as:
List orderLines = new ArrayList();
for (Dish dish : dishes) {
orderLines.add(new OrderLine(dish.name, dish.price);
for (Topping topping : dish.toppings) {
orderLines.add(new OrderLine(topping.name, topping.price);
}
if(note != null) {
orderLines.add(new OrderLine(dish.note, "");
}
}
instead I would like to do something like this:
Observable.from(dishes).map( /* map each dish to many OrderLine */ ).