我在地图中的地图中有一张地图。格式为HashMap<String, Map>
样本数据:
deviceName={commandName={dataKey1=[dataValue1], dataKey2=[dataValue2]}}
设置deviceName
的方式是key
,commandName
是它的值。为了简单起见,现在只有一个,但可能有多个命令。在第二次迭代commandName
是key
,{dataKey1=[dataValue1], dataKey2=[dataValue2]}
是值。在第三次迭代中,dataKey1
是键,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.
}
}
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
假设你想要维护这个结构,你应该为你的cmd map使用这样的东西:
Map<String, Map<String, Map<String,List<String>>>>
但是,我建议在课堂上包装这些地图,以使您的生活更轻松,而且您的代码更多更清洁。