Java 如何使用 JSTL 在 HashMap 中迭代 ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2117557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate an ArrayList inside a HashMap using JSTL?
提问by Rakesh Juyal
I have a map like this,
我有一张这样的地图
Map<Integer,ArrayList<Object>> myMap = new LinkedHashMap<Integer,ArrayList<Object>>();
Now I have to iterate this Map and then the ArrayList inside the map. How can I do this using JSTL?
现在我必须迭代这个 Map,然后迭代地图内的 ArrayList。我怎样才能使用 JSTL 做到这一点?
采纳答案by BalusC
You can use JSTL<c:forEach>
tag to iterate over arrays, collections and maps.
您可以使用JSTL<c:forEach>
标记迭代数组、集合和映射。
In case of arrays and collections, every iteration the var
will give you just the currently iterated item right away.
在数组和集合的情况下,每次迭代var
都会立即为您提供当前迭代的项目。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${collectionOrArray}" var="item">
Item = ${item}<br>
</c:forEach>
In case of maps, every iteration the var
will give you a Map.Entry
object which in turn has getKey()
and getValue()
methods.
在地图的情况下,每次迭代var
都会为您提供一个Map.Entry
对象,该对象又具有getKey()
和getValue()
方法。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${map}" var="entry">
Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
In your particular case, the ${entry.value}
is actually a List
, thus you need to iterate over it as well:
在您的特定情况下,${entry.value}
实际上是 a List
,因此您还需要对其进行迭代:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${map}" var="entry">
Key = ${entry.key}, values =
<c:forEach items="${entry.value}" var="item" varStatus="loop">
${item} ${!loop.last ? ', ' : ''}
</c:forEach><br>
</c:forEach>
The varStatus
is there just for convenience ;)
该varStatus
是那里只是为了方便;)
To understand better what's all going on here, here's a plain Java translation:
为了更好地理解这里发生了什么,这里有一个简单的 Java 翻译:
for (Entry<String, List<Object>> entry : map.entrySet()) {
out.print("Key = " + entry.getKey() + ", values = ");
for (Iterator<Object> iter = entry.getValue().iterator(); iter.hasNext();) {
Object item = iter.next();
out.print(item + (iter.hasNext() ? ", " : ""));
}
out.println();
}
See also:
也可以看看:
回答by dcp
Did you try something like this?
你有没有尝试过这样的事情?
<c:forEach var='item' items='${map}'>
<c:forEach var='arrayItem' items='${item.value}' />
...
</c:forEach>
</c:forEach>
回答by greeshma john
you haven't closed c tag.try out this
你还没有关闭 c 标签。试试这个
<c:forEach items="${logMap}" var="entry">
Key = ${entry.key}, values =
<c:forEach items="${entry.value}" var="item" varStatus="loop">
${item} ${!loop.last ? ', ' : ''}
</c:forEach><br>
</c:forEach>
回答by gordon m
You can also loop round just the map.value its self if you know the key
如果您知道密钥,您也可以只循环 map.value 本身
<c:forEach var="value" items="${myMap[myObject.someInteger]}">
${value}
</c:forEach>