【发布时间】:2015-06-02 11:21:58
【问题描述】:
我有一个包含HashMap 的ArrayList,我正在尝试使用Struts s:iterator 标记遍历ArrayList 的HashMaps。我可以毫无问题地遍历List,但我无法让它遍历地图的条目。到目前为止,我得到了这个:
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class mapTest extends ActionSupport {
public List<Map> listmap;
public String execute() {
listmapObject = new ArrayList();
Map map = new HashMap();
map.put("stationId", "alpha");
map.put("stationName", "USA");
map.put("CustomerName", "charlie");
Map map2 = new HashMap();
map2.put("stationId", "Beta");
map2.put("stationName", "UK");
map2.put("CustomerName", "johnny");
listmapObject.add(map);
listmapObject.add(map2);
return SUCCESS;
}
}
我需要类似下面使用 struts s:iterator 等效的 Java 代码的结果
<table>
<thead>
<th> <Station id> </th>
<th> station Name </th>
<th> Name </th>
</thead>
<s:iterator value="listmapObject">
<tr>
<td> ((HashMap) listmapObject.get(i)).get("stationId") </td>
<td> ((HashMap) listmapObject.get(i)).get("stationName") </td>
<td> ((HashMap) listmapObject.get(i)).get("CustomerName") </td>
</tr>
</s:iterator>
</table>
感谢大家帮助我
【问题讨论】:
-
您是否尝试过遍历 hashmap 键?
-
是的,我试过了,使用这个解决方案stackoverflow.com/questions/4433357/…,但是我的要求不同,就像一个Hashmap值应该放在某个
中,但是上面的解决方案是随机获取key和value对,而且我不想使用 LinkedHashMap (这将保留键和值的顺序),因为我正在从第三方库访问 HashMap 的 ArrayList ..
标签: java arraylist struts2 hashmap dynamic-arrays