2016-11-20 1 views
0

저는 수업에서 재료의 양을 설정하는 중첩 된 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)

질문 말한다. 열거 형 키를 다른 열의 내부에있는 해시 테이블을 어떻게 설정할 수 있습니까?

+1

왜 유닛을 저장하고 성분을 다량으로 저장해야합니까? 그것은 무 성분 클래스의 단순한 속성 일 수 있습니다. –

+0

'HashTable'은 "오래된"클래스이므로 동기화가 필요하지 않다면 확실히'HashMap'을 사용하는 것이 좋습니다. –

+0

@TimothyTruckle, 나는 하나의 레시피에서 레몬 1 개를 사용할 수 있고 다른 레시피에서 3 개의 레몬을 가질 수 있기 때문에 중복되는 재료를 얻을 수 있도록 레시피의 기존 성분 목록을 원합니다. 나는 재료가 kcal, lipids 등과 같은 영양을 가질 수 있다고 생각합니다. 왜 재료의 양은 레서피에 달려 있다고 생각합니다. 왜 그것이 제가 레시피의 속성 일 수 있다고 생각하는지. – ePascoal

답변

0

음이 추가 할 수있는 장점이 있기 때문에

는 사실,이 경우 우리는 확장 가능한 접근 방법이다 부분이라는 세 번째 클래스를 고려할 수 .. 나는이 공정한 대답 가정 그리고 그것은 나를 위해 작동 메서드 또는 더 많은 특성을 가지고 있다면 내일 "복잡한"부분을 원하지만 제 경우에는이 사전이 제 필요에 맞게 보입니다. 따라서 중첩 된 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"); 
1

당신은 너무 문신 다른 해시 테이블에 put() 방법을 사용합니다 :이 대답 HashMap 대신 HashTable을 사용하는거야

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

, 전자는 이제 표준 접근 방식 때문이다.

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

나는이 정말 좋은 디자인 아니라고 디모데와 동의 :

당신은 put 메소드를 사용하여, 별도로 "중첩"HashMap를 구성해야합니다. 나는 개인적으로이 물건을 다루는 또 다른 클래스/인터페이스 Amount을 만들 것이다.