Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

logic:iterate hashmap with key of hashmap

 
azhar bharat
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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)
{
this.categories=categories;
}

public HashMap getCategories()
{
return categories;
}

//subcategories contains hashmap as value
public void setSubcategories(HashMap subcategories)
{
this.subcategories=subcategories;
}

public HashMap getSubcategories()
{
return subcategories;
}
---
I need help in iterating throught the second hashmap.
the require the result like below:
category1
-subcategory1.1
-subcategory1.1
-subcategory1.1
category2
-subcategory2.1
-subcategory2.2
-subcategory2.3

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" />
</logic:iterate>

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
subcategories
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();
cats=(HashMap)ccb.getCategories();
Iterator ic=cats.keySet().iterator();
HashMap subts=new HashMap();
HashMap subs=new HashMap();
Iterator is=null;
while(ic.hasNext())
{
Object key = ic.next();
Object value = cats.get(key);
out.print("<font color=\'blue\'>"+value.toString()+"</font><br/>");
subts=(HashMap)ccb.getSubcategories();
subs=(HashMap)subts.get(key);
is=subs.keySet().iterator();
while(is.hasNext())
{
Object keys = is.next();
Object values = subs.get(keys);
out.print(values.toString()+"<br/>");
}
out.print("<br/><br/>");
}
%>
===
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
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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}'>
${listcat.value}<br>
<c:set var="subcategories" value='${applicationScope[beanName].subcategories[listcat.key]}' />
<c:forEach var="subcategory" items="${subcategories}">
${subcategory.value}<br>
</c:forEach>
</c:forEach>

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.
 
azhar bharat
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks vallidevi and merrill!!!

vallidevi,
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.

merrill,
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic