2014-10-01 38 views
0

我有以下2個hashmaps,其中Message是我創建的對象。從嵌套散列映射覆制對象

HashMap<String, Message> hmA = new HashMap<>(); //A 
HashMap<String, HashMap<String, Message>> hmFinal = new HashMap<>(); 

如果我填充hmA具有以下

hmA.put("Albert", new Message("Albert", "[email protected]", "122-4645)); 
hmA.put("Anthony", new Message("Anthony", "[email protected]", "570-5214")); 
hmA.put("Alphonso", new Message("Alphonso", "[email protected]", "888-5314")); 

然後,添加hmAhmFinal

hmFinal.put("A", hmA); 

現在,如果我創建一個臨時的HashMap

HashMap<String, Message> tempHM = new HashMap<>(); 

如果我只有字母A進行搜索,如何使用hmFinal將整個散列圖hmA複製到tempHM

基本上,如果用戶想看到與字母A相關的散列表,我希望能夠抓取所有hmA並搜索其中的信息。

+0

啊哈希映射。很長時間OOP – ne1410s 2014-10-01 18:10:52

回答

3

你正在尋找的方法是putAll:哈希映射的

HashMap<String, Message> tempHM = new HashMap<>(); 
tempHM.putAll(hmFinal.get("A")); 
3

用字母A搜索檢索地圖後,使用putAll的所有條目複製到地圖:

HashMap<String, Message> mapA = hmFinal.get("A"); 
tempHM.putAll(mapA); 

附::嘗試使用接口編程變量(Map而不是HashMap)。