简体   繁体   中英

Using a hashmap to iterate over a list of hashmaps

I am using a hashmap to add similar values for each aclLine processed

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. How can I iterate over these collected hashmaps for each srcHostName entry in the hashmap? Should I use ArrayList/List to store each instance of the hashmap? 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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