I am sitting in front of this simple problem and cannot get my head around...maybe its just too late :)
There is a simple "Food" class where a Food has id and amount (imagine shopping list):
public class Food {
private String id;
private Integer amount;
}
There is a list of foods that contains one "apple" and another list of foods that contains an "apple" and an "orange". The method addFood should process both lists using stream api and return a list that contains two "apples" and one orange:
List<Food> existingFood = new ArrayList<Food>();
existingFood.add(new Food("apple", 1));
List<Food> foodToAdd = new ArrayList<Food>();
foodToAdd.add(new Food("apple", 1));
foodToAdd.add(new Food("orange", 1));
List<Food> result = addFood(existingFood, foodToAdd);
// result should contain:
// 1. {"apple", 2}
// 2. {"orange", 1}
Whats the most elegant way to do that using stream api? Thanks for your help!