【问题标题】:JAVA nested hashtables with enum带有枚举的 JAVA 嵌套哈希表
【发布时间】:2016-11-20 16:57:43
【问题描述】:

我想在我的班级中有一个嵌套的 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);

}

}

在这种情况下,我想在配方中包含每种成分的数量,我认为哈希表是最好的方法。但是我怎么能在另一个里面设置一个哈希表(像这样):

{new Ingredient("Lemon") :  {"unit":1}}

其中unit是类Recipe的枚举。

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)

问题: 考虑到这种情况。如何在另一个以枚举为键的哈希表中设置哈希表?

【问题讨论】:

  • 为什么需要一个哈希表来存储成分的单位和数量?这可能是 ungredient 类中的简单属性...
  • HashTable 是一个“旧”类,现在建议使用HashMap,当然如果您不需要同步。
  • @TimothyTruckle,我想要一份我食谱中现有成分的列表,因为我会得到重复的成分,因为在一个食谱中我可以使用 1 个柠檬,而在另一个食谱中可以使用 3 个柠檬。我认为一种成分可能具有千卡、脂质等营养成分。这就是为什么我认为成分的数量取决于配方,这就是为什么我认为这可能是配方的属性。
  • @MrMartin 然后我觉得你错过了一个抽象。您可能需要一个 Quantity 类,其中包含成分、单位和数量作为成员。并且您的收据应该有这个数量的列表(而不是地图)。

标签: java hashtable enumeration


【解决方案1】:

我将在此答案中使用HashMap 而不是HashTable,因为前者现在是标准方法。

您必须使用 put 方法单独构造“嵌套”HashMap

Map<Recipe.uom, Integer> amount = new HashMap<>();
amount.put(Recipe.uom.unit, 1);
Ingredient lemon = new Ingredient("Lemon", amount);

我同意蒂莫西的观点,即这并不是一个好的设计。我会亲自创建另一个类/接口Amount 来处理这些东西。

【讨论】:

    【解决方案2】:

    你也需要在其他 Hashtable 上使用put() 方法:

    Map<Recipe.uom,Integer> mass new HashMap<>();
    mass.put(uom.unit,1);
    ingredient.put(new Ingredient("Lemon"),mass);
    

    【讨论】:

      【解决方案3】:

      我想这是一个公平的答案,它对我有用..

      事实上,在这种情况下,我们可以考虑使用名为 Portion 的第三个类,这是一种可扩展的方法,因为如果明天我们想要一个“复杂”的部分,它具有添加方法或更多属性的优势,但在我的情况下,这本字典似乎以满足我的需要。所以嵌套的 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");
      

      【讨论】:

        猜你喜欢
        • 2013-04-10
        • 1970-01-01
        • 2017-07-12
        • 1970-01-01
        • 1970-01-01
        • 2011-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多