Java和JSTL标签中遍历HashMap的方法
2014-07-15 12:51
393 查看
原文地址: 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); } } }
相关文章推荐
- java中使用HashMap两种遍历方法
- java中遍历HashMap的方法
- 【java】遍历hashmap的两种方法及分析
- java遍历HashMap简单的方法
- Java: 遍历 HashMap 的两种方法
- Java的HashMap遍历方法 推荐
- java HashMap 循环遍历map的方法(转)
- JSTL标签调用JAVA类方法
- Java中Hashtable,HashMap,WeakHashMap遍历方法
- java 高效的hashmap遍历方法
- java中遍历HashMap的方法
- 【Java的HashMap遍历方法】
- java中HashMap遍历方法
- java遍历HashMap的方法
- java中的hashmap遍历方法
- java HashMap 循环遍历map的方法(转)
- java中Map集合的常用遍历方法及HashMap的应用实例
- java中Map集合的常用遍历方法及HashMap的应用实例
- Java遍历HashMap的两种方法
- 【java】遍历hashmap的两种方法及分析