저는 수업에서 재료의 양을 설정하는 중첩 된 hastable을 갖고 싶습니다. 다음 시나리오를 고려하십시오.enum이 포함 된 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은 Recipe 클래스의 enum입니다. 을 고려하여이 scenarion 촬영 :
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)
질문 말한다. 열거 형 키를 다른 열의 내부에있는 해시 테이블을 어떻게 설정할 수 있습니까?
왜 유닛을 저장하고 성분을 다량으로 저장해야합니까? 그것은 무 성분 클래스의 단순한 속성 일 수 있습니다. –
'HashTable'은 "오래된"클래스이므로 동기화가 필요하지 않다면 확실히'HashMap'을 사용하는 것이 좋습니다. –
@TimothyTruckle, 나는 하나의 레시피에서 레몬 1 개를 사용할 수 있고 다른 레시피에서 3 개의 레몬을 가질 수 있기 때문에 중복되는 재료를 얻을 수 있도록 레시피의 기존 성분 목록을 원합니다. 나는 재료가 kcal, lipids 등과 같은 영양을 가질 수 있다고 생각합니다. 왜 재료의 양은 레서피에 달려 있다고 생각합니다. 왜 그것이 제가 레시피의 속성 일 수 있다고 생각하는지. – ePascoal