Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I iterate over an Arraylist of Hashmaps

807601Mar 3 2008 — edited Mar 3 2008
Hello, I'm new to java, but have been programming for quite some time. My question is "How do I iterate over an Arraylist of Hashmaps?". I have the following code fragment

List displayParamList = new ArrayList();

pubic void setList() {
// I have a global String array that contains my label and label values
for (int i=0; i<2; i++) {
Map row = new LinkedHashMap();
row.put(this.label,this.labelValue[i]);
this.displayParamList.add(row);
}
}

// Now that function works fine because I can print the "this.displayParamList" object and
// see the key value pairs in the order that I placed them onto the arraylist
// The following function is the problem

public String setDisplayString() {
String displayString = "";
for (int i=0; i<this.displayParamList.size(); i++ ) {
Map.Entry entry = (Map.Entry) this.displayParamList.get(i);
String key = (String) entry.getKey();
String value = (String) entry.getValue();
displayString = displayString + key ": " + value;
}
return displayString;
}

// The program basically stops when it hits the Map.Entry line, What I'm doing wrong here?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 31 2008
Added on Mar 3 2008
11 comments
5,612 views