Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over an HashMap<String, ArrayList<String>> with Struts 2

Tags:

I am currently facing some difficulties with Struts2 and the s:iterate tag.

I want to display a label, which is the key in the HashMap, followed by a table (the value in the HashMap) containing every elements in the ArrayList, for each elements in the HashMap.

For example,

     label   ----------   | test1  |   ----------   | test2  |   ----------        label2   ----------   | test1  |   ----------   | test2  |   ---------- 

I saw a lot of example for an HashMap but didn't find one for my case.

How can I do this ?

Thanks,

like image 975
Zeym Avatar asked Jun 04 '11 14:06

Zeym


People also ask

Can you Iterate over a HashMap?

In Java HashMap, we can iterate through its keys, values, and key/value mappings.

How to we Iterate Map?

Iterating over Map. Entry<K, V>>) of the mappings contained in this map. So we can iterate over key-value pair using getKey() and getValue() methods of Map. Entry<K, V>. This method is most common and should be used if you need both map keys and values in the loop.


1 Answers

<s:iterator value="map">   <h3><s:property value="key" /></h3>   <table>   <s:iterator value="value">     <tr><td><s:property /></td></tr>   </s:iterator>   </table> </s:iterator> 

The iterator of a map is Map.Entry which gets put on the value stack and has two accessors, getKey() and getValue(). Iterate over Entry printing the key, then iterate over the values printing the value. (The list item gets put on top of the value stack so s:property just prints the top.)

like image 131
NKijak Avatar answered Sep 19 '22 22:09

NKijak