Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Struts and the fly likes logic:iterate hashmap with key of hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "logic:iterate hashmap with key of hashmap" Watch "logic:iterate hashmap with key of hashmap" New topic

logic:iterate hashmap with key of hashmap

azhar bharat
Ranch Hand

Joined: Jul 17, 2006
Posts: 87
Here is the scenario.
I have a bean. Which contains 2 hashmaps.
First hashmap contains a key and a string as value.
Second hashmap contains a key and corresponding value is another hashmap.
---the bean methods

public void setCategories(HashMap categories)

public HashMap getCategories()
return categories;

//subcategories contains hashmap as value
public void setSubcategories(HashMap subcategories)

public HashMap getSubcategories()
return subcategories;
I need help in iterating throught the second hashmap.
the require the result like below:

Right now, i am able to list all categories, using code below
<logic:iterate id="listcat" scope="application" name="<%=IConstants.CLASSIFIEDSCATEGORYBEAN_KEY%>" property="categories" >
<bean:write name="listcat" property="value" />

As i said i want to list the subcategories listed along with the categories. I need to do this with nested "logic:iterate" tags.

NOTE: general idea of hashmap
catogories hashmap
key value
1 category1
2 category2
key value
Key Value
1 11 subcategory1
12 subcategory2
2 21 subcategory1
22 subcategory2

I will provide the scriptlet i use now.
ClassifiedsCategoryBean ccb=(ClassifiedsCategoryBean)application.getAttribute(IConstants.CLASSIFIEDSCATEGORYBEAN_KEY);
HashMap cats=new HashMap();
Iterator ic=cats.keySet().iterator();
HashMap subts=new HashMap();
HashMap subs=new HashMap();
Iterator is=null;
Object key =;
Object value = cats.get(key);
out.print("<font color=\'blue\'>"+value.toString()+"</font><br/>");
Object keys =;
Object values = subs.get(keys);
Now this works perfect. but this is temporary and i need replace the same using logic:iterator. I might have to use the indexId attribute to achieve this, but i could not get the desired result. Please help.
Vallidevi Appana

Joined: Jun 30, 2006
Posts: 25

You can try out the following logic.

But here the problem is we are iterating through the subcategory map for each iteration of category map. Obviously its not an optimal solution. Correct me if I am wrong.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd suggest you use JSTL tags rather than Struts tags for this. This is considered "best practice", as JSTL tags are more a part of the J2EE standard, and they are also more powerful.

Here's a possible solution:

<c:set var="beanName" value="<%=IConstants.CLASSIFIEDSCATEGORYBEAN_KEY%>" />

<c:forEach var="listcat" items='${applicationScope[beanName].categories}'>
<c:set var="subcategories" value='${applicationScope[beanName].subcategories[listcat.key]}' />
<c:forEach var="subcategory" items="${subcategories}">

Note: the above would work for JSP 2.0. It would require some modification for JSP 1.2.

Also, I'd recommend you use TreeMap rather than HashMap for your categories and subcategories. TreeMap always presents the map in order by key, while HashMap does not guarantee that the elements will be presented in any specific order.

Consultant, Sima Solutions
azhar bharat
Ranch Hand

Joined: Jul 17, 2006
Posts: 87
Thanks vallidevi and merrill!!!

your "struts:iterate" solutions worked out fine with little changes. had to specify "type" attribute in some of the "bean:define" tags without which a jsp compilation error occurs.

thanks for suggestion to use treemap. I am still using hashmap, but used a getter method which converts it to treemap before the collection is returned for use when i require sorted output.

thanx again
I agree. Here's the link:
subject: logic:iterate hashmap with key of hashmap
jQuery in Action, 3rd edition