我想在我的课上有一个嵌套的hastable来设置成分的数量。请考虑以下情况。使用枚举的JAVA嵌套哈希表
场景:
一个配方有几种成分:
public class Ingredient {
private static int id;
String name;
public Ingredient(String name) {
this.name = name;
}
}
public class Recipe {
public static enum uom{
ml,gr, unit, teaspoon, tablespoon,cup,coffeespoon
};
public String name;
public Hashtable<Ingredient,Hashtable<uom,Integer>> ingredients;
public String description;
public Recipe(String name, String description, Hashtable<Ingredient,Hashtable<uom,Integer>> ingredients) {
this.name = name;
this.description = description;
this.ingredients = ingredients;
}
public static void main (String [] args) {
Ingredient lemon = new Ingredient("lemon");
Hashtable<Ingredient,Hashtable<Recipe.uom,Integer>> ingredient = null;
ingredient.put(new Ingredient("Lemon"),new Hashtable(uom.unit,1));
Recipe LemonPie = new Recipe("Lemon pie","blabla",ingredients);
}
}
在这里,在这种情况下,我想包括里面的配方各成分为量,我相信哈希表最好的方法。但我怎样才能设置哈希表内另一个(像这样的东西):
{new Ingredient("Lemon") : {"unit":1}}
其中unit是一个食谱类的枚举。
Hashtable<Ingredient,Hashtable<Recipe.uom,Integer>> ingredient = null;
ingredient.put(new Ingredient("Lemon"),new Hashtable(uom.unit,1));
它说,Hashtable (int,float) in Hashtable cannot be applied to (Recipe.uom,int)
问: 采取这种scenarion考虑。我怎样才能设置一个散列表内另一个作为关键枚举?
为什么你需要一个散列表来存储单位和ammout的成分?这可能是无用的类中的简单属性...... –
'HashTable'是一个“旧”类,现在推荐使用'HashMap'来代替,当然如果你不需要同步的话。 –
@TimothyTruckle,我想我的食谱现有成分列表,我会得到重复的成分,因为在一个食谱中,我可以使用1柠檬,在另一个有3个柠檬。我认为一种成分可以有像千卡,脂质等营养。这就是为什么我认为配料的数量取决于配方,这就是为什么我认为这可能是食谱的一个属性。 – ePascoal