2012-11-09 40 views
0

我收到這些數據,它通過網絡,因此需要在本地緩存。關於如何構造這些數據的建議?

的數據格式:

Action (String) 
    Direction (String) 
     Frame (int) 
      X,Y (Point or int,int) 

的使用基本上是:

Point myPoint = data.get(action).get(direction).get(frame); 
myPoint.x; // do something with x and y 

我嘗試了這個巨大的HashMap類型的結構:

HashMaP<String, HashMap<String, HashMap<int, Point>>> 

它的工作原理,但醜陋,容易出錯。

我也嘗試將它分成類,它工作;但需要大量的內務代碼。

任何人都知道這個數據結構叫什麼,也許我可以谷歌它。

有什麼建議嗎?

+4

類有什麼問題?什麼「管家」代碼在那裏? – Bohemian

+0

每課都沒什麼問題,我猜「養家」是錯誤的詞。你將如何構建/組織它,Action包含一個方向和方向包含Hashmap ? – user1516346

+0

在我們建議一個類層次結構之前,您需要描述每個項目與其父項之間的關係。一個'Action'可以有很多'Direction's等嗎? –

回答

4

在「龐大的散列映射類型的結構的」隱式是實體之間的關係:

  • 動作是一個字符串,它索引「多」方向
  • 方向是一個字符串,它索引「多」幀
  • 幀是一個數,其索引「多」點
  • 點是一個結構

一個簡單approac h可能是定義一個包含'action''direction'和'frame'的'key'對象,並在Map結構中使用它,例如

class PointKey { 
    String action, direction; 
    int frame; 
    PointKey(String action, String direction, int frame { .. init etc etc } 

... 

根據使用特點,你會想要麼重寫hashCode提供基於這三部分關鍵的一些「合理」的獨特價值,否則實現Comparable如果你希望那裏是大量的這些價值觀,而且你期望更多地閱讀它們,而不是寫它們。

然後定義您的Map這樣的:

​​

Map<PointKey,Point> data = new TreeMap<PointKey,Point>(); 

取決於哪種方法您選擇。

另一個問題是,如果您要創建大量這些密鑰,則需要創建新密鑰來隨機訪問這些密鑰,在這種情況下,您可能想要使用flyweight

... 
// PointKey instance that is retained and used again and again, purely for 'access' purposes 
dataKey.setIdentifiers(myAction, myDirection, myFrame); 
Point myPoint = data.get(dataKey)