简体   繁体   English

使用哈希表迭代哈希表列表

[英]Using a hashmap to iterate over a list of hashmaps

I am using a hashmap to add similar values for each aclLine processed 我正在使用哈希aclLine每个处理的aclLine添加相似的值

for (String aclLine : refinedFileContents){
 if(Some condition)
 {
   staticVariablesMap.put("lineNumber", **lineNumber**); 
   staticVariablesMap.put("**srcHostName**", batchBean.getSourceIpAddress());
   staticVariablesMap.put("batchBean", batchBean);
}
} 

Later I want to iterate over these hashmaps for each line and perform some actions specific to a given key, value pair (eg get the srcHostName for that lineNumber ) and use it to process next steps. 稍后,我要遍历每行的这些哈希映射,并执行特定于给定键,值对的某些操作(例如,获取该lineNumbersrcHostName ),并使用它来处理后续步骤。 How can I iterate over these collected hashmaps for each srcHostName entry in the hashmap? 如何为哈希图中的每个srcHostName条目遍历这些收集的哈希图? Should I use ArrayList/List to store each instance of the hashmap? 我应该使用ArrayList / List存储哈希图的每个实例吗? Is this feasible? 这可行吗?

Sounds to me like you should combine the attributes in your hashmaps into an object instead. 在我看来,您应该将哈希图中的属性组合成一个对象。 Then you could just use one hash map. 然后,您可以只使用一个哈希图。

public class AclLine {
    private long lineNumber;
    private String srcHostName;
    private Object batchBean; 
}

Map<AclLine> lines = new HashMap<AclLine>();
// Or maybe a List?
List<AclLine> lines = new ArrayList<AclLine>();

Or is there a reason you need these "parallel" map entries? 还是有原因需要这些“并行”映射条目?

I didn't get your question completely like you are putting values in only one hash map & you want to iterate hashmaps You can iterate hash map like this. 我没有完全解决您的问题,就像您只将值放在一个哈希图中一样,并且您想要迭代哈希图。您可以像这样迭代哈希图。

Iterator<Entry<String, Object>> it = hashMap.entrySet().iterator();
while (it.hasNext())
{
    Map.Entry<String, Object> entry = (Map.Entry<String, Object>)it.next();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM