I am tasked with the following problem: given an array of IngredientPortions (an object i made), if two of the elements in the IngredientPortions are the same Ingredient (Ingredient is another object that is a component of IngredientPortion objects), I am supposed to combine the IngredientPortion elements.
For example, if the IngredientPortion array I am given has two elements, such as an avacodo portion of say 1.5 oz, and another avocado portion of 2.0 oz, I should produce a new IngredientPortion array of 1 element: an avocado of 3.5 oz.
I am not sure how to do this, but I was thinking of using a hashmap of Strings as keys, representing the ingredient name, and values of IngredientPortion objects. If the hashmap already has a key of the given ingredientPortion.getName(), i would put in that specific ingredientPortion for that key, but I'm not sure how to combine the ingredientPortion amounts. Would it automatically combine it or would it store it as two different ingredientPortions under that one key??? Thanks in advance!