2011-10-14 67 views
0

我在地图内的地图内有地图。格式是HashMap<String, Map>采样数据:如何在地图内的地图内迭代地图

deviceName={commandName={dataKey1=[dataValue1], dataKey2=[dataValue2]}} 

它的设置是deviceNamekeycommandName是它的价值的方式。为了简单起见,现在只有一个,但可能有多个命令。在第二次迭代commandNamekey{dataKey1=[dataValue1], dataKey2=[dataValue2]}是值。在第三次迭代中,​​是关键字,dataValue1是值(它是一个arrayList)。

我可以遍历一个,但我不知道如何做到这一点。

public void analyseVersion(Map cmd) { 

    Iterator deviceIT = cmd.keySet().iterator(); 
    while (deviceIT.hasNext()) { 
     String deviceName = (String) deviceIT.next(); 
     //Map<String, Map> cmd = (Map<String, Map>) cmd.get(deviceName);//This will not work. 
     //At this point I want to iterate through each command and for each command iterate through each data set. 
    } 
} 
+2

你为什么不使用泛型的声明? –

回答

1

为了呼应jtahlborn,抽象是你的朋友

public class Data 
    private Map<String, Object> attributes 

public class Command 
    private Map<String, Data> data; 

public class Device 
    private Map<String, Command> commands; 

// in main, etc 
for (Entry<String, Command> commandEntry : device.getCommandEntries()) 
{ 
    for (Entry<String, Data> dataEntry : commandEntry.getValue().getDataEntries()) 
    { 
    for (Entry<String, Object> attributeEntry : dataEntry.getValue().getAttributeEntries()) 
     // do something 
    } 
} 
0

假设你要保持这种结构,你应该使用这样的事情你CMD地图:

Map<String, Map<String, Map<String,List<String>>>> 
然而

,我建议你在课堂上包装这些地图,让您的生活更轻松和你的代码更清洁。