2013-08-19 20 views
1

檢索自定義密鑰對象,這可能不是最好的數據結構,但我想知道是否有可能做到這一點: 我有一組工具,每一個唯一的ID和一堆或屬性。每個工具還有一個包含屬性的腔室集合。 我希望能夠使用該工具作爲HashMap和Chambers列表的鍵值。
當我從數據庫中取回所有腔室信息後,我想通過toolId獲取關鍵對象(工具),以便我可以將每個腔室添加到其合適的工具。我重寫了equals方法和hash方法以使用toolId。的Java從地圖

除了帶回所有的按鍵和迭代他們,看看他們是否等於toolId,是有一些方法來獲取密鑰對象

這是我到目前爲止的代碼:

Public class ToolBean { 

    Private String toolId; 
    Private String toolName; 
    Private String toolOwner; 

    Public ToolBean(String toolId){ 
     this.toolId = toolId; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     ToolBean other = (ToolBean) obj; 
     if (toolId == null) { 
      if (other.toolId != null) 
       return false; 
     } else if (!toolId.equals(other.toolId)) 
      return false; 
     return true; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((toolId == null) ? 0 : toolId.hashCode()); 
     return result; 
    } 
} 

我創建看起來像這樣的結構:

LinkedHashMap<ToolBean, LinkedHashMap<String, ChamberBean>> toolWithChamberMap = new LinkedHashMap<ToolBean, LinkedHashMap<String, ChamberBean>>(); 

我知道我可以創建一個ToolBean公頃結構通過Chambers的LinkedHashMap(LinkedHashMap),然後打開該工具,將新腔室添加到地圖中,將工具放回到原始地圖中。我想知道是否有辦法跳過這一步。

感謝, 布麗塔

+0

只需用Key = toolid和Value = tool創建第二個HashMap。 – DaoWen

+0

這看起來像足夠的代碼,允許你使用你的ToolBean類和散列圖。代碼對我來說看起來很不錯,假設你總是認爲兩個對象相同,如果他們有相同的工具ID。 –

+0

無論如何你有什麼問題?代碼是否工作?我認爲會的。你試過了嗎?發生了什麼?道文的回答很好,但我認爲你不需要這樣做。使用添加到ToolBean的代碼,ToolBean對象可以合理地作爲哈希映射中的鍵。 –

回答

0

假設

class ToolBean { 
    // as described in OP 
} 

class Chamber { 
    // some opaque class 
} 

你似乎在詢問這是什麼:

// Master map of ToolBean to map of Chamber objects 
Map<ToolBean, Map<String, Chamber>> toolBeanToChamberMap = 
    new LinkedHashMap<ToolBean,Map<String,Chamber>>(); 

// A tool bean and a chamber 
ToolBean tb1 = new ToolBean(...); 
Chamber ch1 = new Chamber(...); 

// Create a map that will contain Chambers and their String keys 
Map<String,Chamber> chMap = new LinkedHashMap<String,Chamber>(); 

// Put the Chamber into this map 
chMap.put("one",ch1); 

// Put the map of Chambers into the master map, keyed off the ToolBean 
toolBeanToChamberMap.put(tb1, chMap); 

// sometime later ... 

ToolBean tb2 = ... // may be the same as tb1 

// A new Chamber to be added to the data structure 
Chamber ch2 = new Chamber(...); 

// First find the Chamber map in the master map, matching the ToolBean of interest 
Map<String,Chamber> temp = toolBeanToChamberMap.get(tb2); 

// 'temp' is a reference to the submap - if it's null, this ToolBean wasn't in the master map yet 
if (temp == null) { 
    // So create a new empty submap 
    temp = new LinkedHashMap<String,Chamber>(); 
    // Add it to the master map 
    toolBeanToChamberMap.put(tb2,temp); 
} 
// At this point 'temp' is either the pre-existing submap or the one we just added 
temp.put("two",ch2); 

然而,除非你有一個很好的理由這樣做事情這樣,我會建議如下:

public class ToolBean { 
    some attributes... 
    Map<String, Chamber> chamberMap = new LinkedHashmap<String,Chamber>(); 
    ... 
    public void addChamber(String name, Chamber c) { 
     // similar logic as above 
    } 
    public Chamber getChamber(String name) { 
     return chamberMap.get(name); 
    } 
} 

Set<ToolBean> toolBeans = new HashSet<ToolBean>(); 

ToolBean tb1 = new ToolBean(); 
tb1.addChamber("one", new Chamber(...)); 
tb1.addChamber("two", new Chamber(...)); 
toolBeans.add(tb1); 

換言之,隱藏的ToolBean類的內部腔室中的地圖的所有的複雜性。

處理重複商會值和空值的,留作練習。

+0

一旦我創建第一個ToolBean並將其放入地圖中,我如何獲取並添加分庭的地圖。 如果我的第一個鍵(ToolBean)是一個toolId ='aaa'的工具,並且腔室圖具有腔室'一個'並且我想添加腔室'二'。 它看起來像 chamberMap = toolWithChamberMap('aaa') chamberMap.put('one',chamber)? 我知道我可以像Nikita建議的那樣做,但我想知道我是否可以將ToolBean作爲關鍵。 – itsNotABlanket

+0

啊。目前還不清楚您是否想使用腔室圖來查找「ToolBean」實例作爲關鍵。這完全是一個不同的問題,我的答案並不適用。 「Chamber」對象的定義是什麼? 'toolWithChamberMap(「aaa」)'返回什麼? ''aaa「'如何引用'Chamber'對象?你還沒有真正提供足夠的信息。 –

+0

我很抱歉在解釋事情時這麼糟糕。 第一個對象Key1 = ToolBean {toolId ='tool1'; toolName ='name1'; toolData ='someData「;} Value1 = chamberMap1 {'one',chamber1} 我需要修改chamberMap1以添加腔室'two'。我怎麼做? /n我可以使用tooId和equals函數,還是需要創建整個對象才能獲得腔體貼圖? – itsNotABlanket

1

個人而言,我會創建2個地圖:ToolId - > ToolBean和ToolId - >錢伯斯。它類似於你的方法,但我不喜歡ToolBean是地圖中的關鍵。我沒有看到ToolBean作爲鍵的很多好處,並且它編譯了代碼,因爲您需要覆蓋equals和忽略名稱和所有者的hashcode方法。

第二種方式是嵌入室以ToolBean本身。

使用你的代碼第三種方法是這樣的:有ToolId您可以使用該ID創建ToolBean的實例,並把它作爲關鍵檢索室地圖。就我個人而言,這感覺像是黑客。