原文地址: http://www.javaweb.cc/language/java/032291.shtml
在Java中:
第一种:
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
效率高,以后一定要使用此种方式!
第二种:
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
}
效率低,以后尽量少使用!
在JSTL中:
var data = [
<c:forEach var="up" items="${userProject}" varStatus="loop">
{name:"${up.key}",value:"${up.value}",color:getColor()}
<c:if test="${!loop.last}">,</c:if>
</c:forEach>
];
类似地,在JSTL中遍历ArrayList如下:
<c:forEach var="duration" items="${failureDuration}" varStatus="loop">
"${duration}"
<c:if test="${!loop.last}">,</c:if>
</c:forEach>
========以下为公司项目中的部分代码,不用管==========
List<Build> oneProjectBuilds = new ArrayList<>();
//find all the projects
HashMap<String, HashMap<String, List<? extends Object>>> allProjects = new HashMap<>();
allProjects = projectService.findAllProjects();
List<Integer> numberList = new ArrayList<Integer>();
int projectId = 0;
int successBuildNum = 0;
int failureBuildNum = 0;
Map<String,List<Integer>> projectBuild = new HashMap<>();
HashMap<String, ?> result = new HashMap<>();
for(Map.Entry<String, HashMap<String, List<? extends Object>>> project:allProjects.entrySet()){
for(Map.Entry<String, List<? extends Object>> projectValue: project.getValue().entrySet()){
if("projectId".equals(projectValue.getKey())){
projectId = Integer.parseInt(projectValue.getValue().get(0).toString());
oneProjectBuilds = projectService.findBuildsByProjectId(projectId);
//We count the number of successful builds and fail builds, then put them into a list
for(Build build:oneProjectBuilds){
result = projectService.buildSummary(HttpUtils.urlEncode(build.getBuildName()));
successBuildNum += ((ArrayList)(result.get("successDuration"))).size();
failureBuildNum += ((ArrayList)(result.get("failureDuration"))).size();
}
numberList.add(successBuildNum);
numberList.add(failureBuildNum);
//Find the project name then put the name and numberList into a map
projectBuild.put(projectService.findByprojectId(projectId).getProjectName(),numberList);
}
}
}