palley
palley

Reputation: 11

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 (e.g. 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?

Upvotes: 1

Views: 510

Answers (2)

Prakhar Jain
Prakhar Jain

Reputation: 63

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();
}

Upvotes: 0

NilsH
NilsH

Reputation: 13821

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?

Upvotes: 2

Related Questions