JAVA 带有枚举的嵌套哈希表
JAVA nested hashtables with enum
我想在我的 class 中嵌套 hastable 来设置成分的数量。请考虑以下场景。
场景:
一个食谱有几种成分:
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);
}
}
在这种情况下,我想在食谱中包含每种成分的数量,我认为散列 table 是最好的方法。但是我怎样才能在另一个里面设置一个散列table(像这样):
{new Ingredient("Lemon") : {"unit":1}}
其中单位是 class 食谱的枚举。
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)
问题:
考虑到这种情况。我如何在另一个以枚举为键的内部设置散列table?
您需要在另一个哈希表上使用 put()
方法来:
Map<Recipe.uom,Integer> mass new HashMap<>();
mass.put(uom.unit,1);
ingredient.put(new Ingredient("Lemon"),mass);
我打算在这个答案中使用 HashMap
而不是 HashTable
,因为前者现在是标准方法。
您必须使用 put 方法单独构建 "nested" HashMap
:
Map<Recipe.uom, Integer> amount = new HashMap<>();
amount.put(Recipe.uom.unit, 1);
Ingredient lemon = new Ingredient("Lemon", amount);
我同意 Timothy 的观点,这并不是一个好的设计。我会亲自创建另一个 class/interface Amount
来处理这些东西。
好吧,我想这是一个公平的答案,它对我有用..
事实上,在这种情况下,我们可以考虑第三个 class 命名部分,这是一种可扩展的方法,因为如果明天我们想要 "complex",它具有添加方法或更多属性的优势部分,但就我而言,这本词典似乎符合我的需要。所以嵌套的 HashTable/HashMap 应该这样定义:
Ingredient lemon = new Ingredient("lemon");
Ingredient powder = new Ingredient("powder");
HashMap<Ingredient,HashMap<uom,Integer>> portion = new HashMap<Ingredient,HashMap<uom,Integer>>();
portion.put(lemon,new HashMap<uom, Integer>(){{put(uom.unit,1);}});
portion.put(powder,new HashMap<uom, Integer>(){{put(uom.cup,2);}});
Recipe lemon_cake = new Recipe("lemon cake","bla bla",portion,"Mr Martin");
我想在我的 class 中嵌套 hastable 来设置成分的数量。请考虑以下场景。
场景:
一个食谱有几种成分:
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);
}
}
在这种情况下,我想在食谱中包含每种成分的数量,我认为散列 table 是最好的方法。但是我怎样才能在另一个里面设置一个散列table(像这样):
{new Ingredient("Lemon") : {"unit":1}}
其中单位是 class 食谱的枚举。
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)
问题: 考虑到这种情况。我如何在另一个以枚举为键的内部设置散列table?
您需要在另一个哈希表上使用 put()
方法来:
Map<Recipe.uom,Integer> mass new HashMap<>();
mass.put(uom.unit,1);
ingredient.put(new Ingredient("Lemon"),mass);
我打算在这个答案中使用 HashMap
而不是 HashTable
,因为前者现在是标准方法。
您必须使用 put 方法单独构建 "nested" HashMap
:
Map<Recipe.uom, Integer> amount = new HashMap<>();
amount.put(Recipe.uom.unit, 1);
Ingredient lemon = new Ingredient("Lemon", amount);
我同意 Timothy 的观点,这并不是一个好的设计。我会亲自创建另一个 class/interface Amount
来处理这些东西。
好吧,我想这是一个公平的答案,它对我有用..
事实上,在这种情况下,我们可以考虑第三个 class 命名部分,这是一种可扩展的方法,因为如果明天我们想要 "complex",它具有添加方法或更多属性的优势部分,但就我而言,这本词典似乎符合我的需要。所以嵌套的 HashTable/HashMap 应该这样定义:
Ingredient lemon = new Ingredient("lemon");
Ingredient powder = new Ingredient("powder");
HashMap<Ingredient,HashMap<uom,Integer>> portion = new HashMap<Ingredient,HashMap<uom,Integer>>();
portion.put(lemon,new HashMap<uom, Integer>(){{put(uom.unit,1);}});
portion.put(powder,new HashMap<uom, Integer>(){{put(uom.cup,2);}});
Recipe lemon_cake = new Recipe("lemon cake","bla bla",portion,"Mr Martin");