2014-10-01 98 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)。