Iterating through an ArrayList of HashMaps?

Hi All, I'm wondering if someone could provide me with an example of how to iterate through an ArrayList of HashMaps using JSTL? Thanks
[157 byte] By [joebassila] at [2007-11-27 9:45:44]
# 1
c:forEach is all you need.
BalusCa at 2007-7-12 23:54:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Right, I'm wondering if you could help me out with the syntax in particular. For example, would i be able to do the following:

- Assume that myBean extends ArrayList

<c:forEach var="myArrayList" items="${mybean}">

<c:forEach var="myHashMap" items="${mybean.myMap}">

//How do i access a key, value pair in this map?

</c:forEach>

</c:forEach>

Thanks for the help.

joebassila at 2007-7-12 23:54:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

[nobr]Based on this example:

MyBeanpublic List< Map<String, String> > getArrayList() {

List< Map<String, String> > arrayList = new ArrayList< Map<String, String> >();

Map<String, String> map1 = new HashMap<String, String>();

map1.put("key1", "value1a");

map1.put("key2", "value2a");

arrayList.add(map1);

Map<String, String> map2 = new HashMap<String, String>();

map2.put("key1", "value1b");

map2.put("key2", "value2b");

arrayList.add(map2);

return arrayList;

}

If you know the key names before:<c:forEach items="${myBean.arrayList}" var="hashMap">

key1 value: <c:out value="${hashMap.key1}" />

key2 value: <c:out value="${hashMap.key2}" />

<br />

</c:forEach>

If you don't know the key names before and just want all entries:<c:forEach items="${myBean.arrayList}" var="hashMap">

<c:forEach items="${hashMap}" var="entry">

key: <c:out value="${entry.key}" />

value: <c:out value="${entry.value}" />

<br />

</c:forEach>

<br />

</c:forEach>

Note: remove spaces from generic type parameter if you're about to copypasting this. This forum doesn't parse nested < and > tags correctly.[/nobr]

BalusCa at 2007-7-12 23:54:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

[nobr]Ok. I do know my key names...they are defined as constants inside of a java file. Can I access constants defined in a java file using EL? Also, with your example below:

<c:forEach items="${myBean.arrayList}" var="hashMap">

key1 value: <c:out value="${hashMap.key1}" />

key2 value: <c:out value="${hashMap.key2}" />

<br />

</c:forEach>

- Sorry for the dumb question, but how come ${myBean.arrayList} isn't referring to the ArrayList and NOT the hashMap?[/nobr]

joebassila at 2007-7-12 23:54:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

The standard for a forEach loop is

<c:forEach var="item" items="${myListOfItems}>

Given your description above myBean IS an List (extends ArrayList) of HashMaps. So the expression should be ${myBean} rather than ${myBean.arrayList}

<c:forEach var="hashMap" items="${myBean}" >

This loop is saying go through the list "myBean", and on each iteration store the value in the variable "hashMap"

Inside this loop, you no longer need to refer to "myBean" but just to the element "hashMap".

evnafetsa at 2007-7-12 23:54:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
That makes sense, thanks for the explanation. Do you know if i can access a constant say myJavaFile.COLUMN_A through EL? These constants are the keys of my HashMaps.
joebassila at 2007-7-12 23:54:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
> - Sorry for the dumb question, but how come> ${myBean.arrayList} isn't referring to the ArrayList> and NOT the hashMap?My example uses a real bean, not a class which extends ArrayList ;) I would just illustrate how things are supposed to work.
BalusCa at 2007-7-12 23:54:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Great. Thanks for the clarification. Can you suggest How to use JSTL if the keys in my HashMap are constants defined in a Java file?
joebassila at 2007-7-12 23:54:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
EL doesn't understand Java constants.There is an article on this issue and potential workarounds here: http://www.javaranch.com/journal/200601/Journal200601.jsp#a3And an old post on the subject here: http://forum.java.sun.com/thread.jspa?forumID=45&threadID=508847
evnafetsa at 2007-7-12 23:54:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...